diff --git a/src/main/java/com/google/firebase/ImplFirebaseTrampolines.java b/src/main/java/com/google/firebase/ImplFirebaseTrampolines.java index 3a7f6499a..0d3008b18 100644 --- a/src/main/java/com/google/firebase/ImplFirebaseTrampolines.java +++ b/src/main/java/com/google/firebase/ImplFirebaseTrampolines.java @@ -22,6 +22,8 @@ import com.google.api.core.ApiFutures; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.firestore.FirestoreOptions; +import com.google.firebase.auth.internal.Utils; +import com.google.firebase.internal.EmulatorCredentials; import com.google.firebase.internal.FirebaseService; import com.google.firebase.internal.NonNull; @@ -41,6 +43,9 @@ public final class ImplFirebaseTrampolines { private ImplFirebaseTrampolines() {} public static GoogleCredentials getCredentials(@NonNull FirebaseApp app) { + if (Utils.isEmulatorMode()) { + return new EmulatorCredentials(); + } return app.getOptions().getCredentials(); } diff --git a/src/main/java/com/google/firebase/database/FirebaseDatabase.java b/src/main/java/com/google/firebase/database/FirebaseDatabase.java index 306ce9274..e59cd38fa 100644 --- a/src/main/java/com/google/firebase/database/FirebaseDatabase.java +++ b/src/main/java/com/google/firebase/database/FirebaseDatabase.java @@ -35,6 +35,7 @@ import com.google.firebase.database.utilities.ParsedUrl; import com.google.firebase.database.utilities.Utilities; import com.google.firebase.database.utilities.Validation; +import com.google.firebase.internal.EmulatorCredentials; import com.google.firebase.internal.FirebaseService; import com.google.firebase.internal.SdkUtils; @@ -406,26 +407,4 @@ public void destroy() { instance.destroy(); } } - - private static class EmulatorCredentials extends GoogleCredentials { - - EmulatorCredentials() { - super(newToken()); - } - - private static AccessToken newToken() { - return new AccessToken("owner", - new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1))); - } - - @Override - public AccessToken refreshAccessToken() { - return newToken(); - } - - @Override - public Map> getRequestMetadata() throws IOException { - return ImmutableMap.of(); - } - } } diff --git a/src/main/java/com/google/firebase/internal/EmulatorCredentials.java b/src/main/java/com/google/firebase/internal/EmulatorCredentials.java new file mode 100644 index 000000000..b13e971d0 --- /dev/null +++ b/src/main/java/com/google/firebase/internal/EmulatorCredentials.java @@ -0,0 +1,52 @@ +/* + * Copyright 2022 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.firebase.internal; + +import com.google.auth.oauth2.AccessToken; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.common.collect.ImmutableMap; + +import java.io.IOException; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * A Mock Credentials implementation that can be used with the Emulator Suite + */ +public final class EmulatorCredentials extends GoogleCredentials { + + public EmulatorCredentials() { + super(newToken()); + } + + private static AccessToken newToken() { + return new AccessToken("owner", + new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1))); + } + + @Override + public AccessToken refreshAccessToken() { + return newToken(); + } + + @Override + public Map> getRequestMetadata() throws IOException { + return ImmutableMap.of(); + } +} diff --git a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java index cd43c0147..f83c5f56f 100644 --- a/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java +++ b/src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java @@ -71,8 +71,13 @@ public class FirebaseUserManagerTest { private static final String TEST_TOKEN = "token"; + private static final String TEST_EMULATOR_TOKEN = "owner"; + private static final GoogleCredentials credentials = new MockGoogleCredentials(TEST_TOKEN); + private static final GoogleCredentials emulator_credentials = + new MockGoogleCredentials(TEST_EMULATOR_TOKEN); + private static final ActionCodeSettings ACTION_CODE_SETTINGS = ActionCodeSettings.builder() .setUrl("https://example.dynamic.link") .setHandleCodeInApp(true) @@ -2916,7 +2921,7 @@ private static void initializeAppWithResponses(String... responses) { } MockHttpTransport transport = new MultiRequestMockHttpTransport(mocks); FirebaseApp.initializeApp(FirebaseOptions.builder() - .setCredentials(credentials) + .setCredentials(isEmulatorMode() ? emulator_credentials : credentials) .setHttpTransport(transport) .setProjectId("test-project-id") .build()); @@ -3014,7 +3019,7 @@ private static void checkSamlProviderConfig(SamlProviderConfig config, String pr private static void checkRequestHeaders(TestResponseInterceptor interceptor) { HttpHeaders headers = interceptor.getResponse().getRequest().getHeaders(); - String auth = "Bearer " + TEST_TOKEN; + String auth = "Bearer " + (isEmulatorMode() ? TEST_EMULATOR_TOKEN : TEST_TOKEN); assertEquals(auth, headers.getFirstHeaderStringValue("Authorization")); String clientVersion = "Java/Admin/" + SdkUtils.getVersion(); @@ -3027,6 +3032,12 @@ private static void checkUrl(TestResponseInterceptor interceptor, String method, assertEquals(url, request.getUrl().toString().split("\\?")[0]); } + private static boolean isEmulatorMode() { + return !Strings.isNullOrEmpty( + FirebaseProcessEnvironment.getenv("FIREBASE_AUTH_EMULATOR_HOST") + ); + } + private interface UserManagerOp { void call(FirebaseAuth auth) throws Exception; } diff --git a/src/test/java/com/google/firebase/auth/multitenancy/FirebaseTenantClientTest.java b/src/test/java/com/google/firebase/auth/multitenancy/FirebaseTenantClientTest.java index 3e57dc3a1..eca55ee66 100644 --- a/src/test/java/com/google/firebase/auth/multitenancy/FirebaseTenantClientTest.java +++ b/src/test/java/com/google/firebase/auth/multitenancy/FirebaseTenantClientTest.java @@ -33,6 +33,7 @@ import com.google.api.client.testing.http.MockHttpTransport; import com.google.api.client.testing.http.MockLowLevelHttpResponse; import com.google.auth.oauth2.GoogleCredentials; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.firebase.ErrorCode; @@ -63,8 +64,13 @@ public class FirebaseTenantClientTest { private static final String TEST_TOKEN = "token"; + private static final String TEST_EMULATOR_TOKEN = "owner"; + private static final GoogleCredentials credentials = new MockGoogleCredentials(TEST_TOKEN); + private static final GoogleCredentials emulator_credentials = + new MockGoogleCredentials(TEST_EMULATOR_TOKEN); + private static final String PROJECT_BASE_URL = "https://identitytoolkit.googleapis.com/v2/projects/test-project-id"; @@ -342,7 +348,7 @@ private static void checkTenant(Tenant tenant, String tenantId) { private static void checkRequestHeaders(TestResponseInterceptor interceptor) { HttpHeaders headers = interceptor.getResponse().getRequest().getHeaders(); - String auth = "Bearer " + TEST_TOKEN; + String auth = "Bearer " + (isEmulatorMode() ? TEST_EMULATOR_TOKEN : TEST_TOKEN); assertEquals(auth, headers.getFirstHeaderStringValue("Authorization")); String clientVersion = "Java/Admin/" + SdkUtils.getVersion(); @@ -362,7 +368,7 @@ private static TestResponseInterceptor initializeAppForTenantManagement(String.. } MockHttpTransport transport = new MultiRequestMockHttpTransport(mocks); FirebaseApp.initializeApp(FirebaseOptions.builder() - .setCredentials(credentials) + .setCredentials(isEmulatorMode() ? emulator_credentials : credentials) .setHttpTransport(transport) .setProjectId("test-project-id") .build()); @@ -408,4 +414,10 @@ private static GenericJson parseRequestContent(TestResponseInterceptor intercept interceptor.getResponse().getRequest().getContent().writeTo(out); return JSON_FACTORY.fromString(new String(out.toByteArray()), GenericJson.class); } + + private static boolean isEmulatorMode() { + return !Strings.isNullOrEmpty( + FirebaseProcessEnvironment.getenv("FIREBASE_AUTH_EMULATOR_HOST") + ); + } }