Skip to content

Commit

Permalink
fix: refactor FakeCredentials (#325)
Browse files Browse the repository at this point in the history
This change allows users who want to try to manually configured FirestoreOptions for an emulator to be able to leverage the credentials we use when boostrapping via environment variable.

* Rename FakeCredentials to EmulatorCredentials
* Make EmulatorCredentials static
* Move from inner class of FirestoreOptions.Builder to inner class of FirestoreOptions

Related to #190
  • Loading branch information
BenWhitehead committed Aug 11, 2020
1 parent 854968f commit 269e62c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
10 changes: 10 additions & 0 deletions google-cloud-firestore/clirr-ignored-differences.xml
Expand Up @@ -162,4 +162,14 @@
<to>com.google.cloud.firestore.CollectionGroup</to>
</difference>

<!--
FakeCredentials Refactor
com.google.cloud.firestore.FirestoreOptions$Builder$FakeCredentials -> com.google.cloud.firestore.FirestoreOptions$EmulatorCredentials
-->
<difference>
<differenceType>8001</differenceType>
<className>com/google/cloud/firestore/FirestoreOptions$Builder$FakeCredentials</className>
<to>*</to>
</difference>

</differences>
Expand Up @@ -226,39 +226,39 @@ public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
.build());
// Use a `CredentialProvider` to match the Firebase Admin SDK, which prevents the Admin SDK
// from overwriting the Emulator credentials.
this.setCredentialsProvider(FixedCredentialsProvider.create(new FakeCredentials()));
this.setCredentialsProvider(FixedCredentialsProvider.create(new EmulatorCredentials()));
}

return new FirestoreOptions(this);
}
}

public class FakeCredentials extends Credentials {
private final Map<String, List<String>> HEADERS =
ImmutableMap.of("Authorization", Arrays.asList("Bearer owner"));

@Override
public String getAuthenticationType() {
throw new IllegalArgumentException("Not supported");
}
public static class EmulatorCredentials extends Credentials {
private final Map<String, List<String>> HEADERS =
ImmutableMap.of("Authorization", Arrays.asList("Bearer owner"));

@Override
public Map<String, List<String>> getRequestMetadata(URI uri) {
return HEADERS;
}
@Override
public String getAuthenticationType() {
throw new IllegalArgumentException("Not supported");
}

@Override
public boolean hasRequestMetadata() {
return true;
}
@Override
public Map<String, List<String>> getRequestMetadata(URI uri) {
return HEADERS;
}

@Override
public boolean hasRequestMetadataOnly() {
return true;
}
@Override
public boolean hasRequestMetadata() {
return true;
}

@Override
public void refresh() {}
@Override
public boolean hasRequestMetadataOnly() {
return true;
}

@Override
public void refresh() {}
}

@InternalApi("This class should only be extended within google-cloud-java")
Expand Down

0 comments on commit 269e62c

Please sign in to comment.