Skip to content

Commit f198c6f

Browse files
cynthiajoanCynthia Jiang
and
Cynthia Jiang
authored
feat(auth): Add emulator support (#1400)
* add auth emulator support * fix ios number parse * update the documentation part * reduce lint warning * more lint warnings * code format * use environment to decide using emulator or not * fix a typo * add readme entry for FirebaseApp.GetApps() * update for review comment * add missing ` --------- Co-authored-by: Cynthia Jiang <[email protected]>
1 parent 3002ff4 commit f198c6f

19 files changed

+156
-53
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ gcs_key_file.json
2828
*_build/
2929
cmake-build-*/
3030
testing/test_framework/external/
31+
CMakeFiles/
32+
CMakeCache.txt
3133

3234
# XCode user specific folders
3335
**/xcuserdata/

app/src/include/firebase/app.h

-3
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,8 @@ class App {
571571
/// Get the App with the given name, or nullptr if none have been created.
572572
static App* GetInstance(const char* name);
573573

574-
#if !defined(DOXYGEN)
575-
// Hidden from the public documentation for now
576574
/// Get all the apps, including the default one.
577575
static std::vector<App*> GetApps();
578-
#endif // !defined(DOXYGEN)
579576

580577
#ifndef SWIG
581578
// <SWIG>

auth/src/android/auth_android.cc

+30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <assert.h>
1818
#include <jni.h>
1919

20+
#include <string>
21+
2022
#include "app/src/assert.h"
2123
#include "app/src/embedded_file.h"
2224
#include "app/src/include/firebase/internal/mutex.h"
@@ -53,6 +55,7 @@ using util::JniStringToString;
5355
X(RemoveIdTokenListener, "removeIdTokenListener", \
5456
"(Lcom/google/firebase/auth/FirebaseAuth$IdTokenListener;)V"), \
5557
X(SignOut, "signOut", "()V"), \
58+
X(UseEmulator, "useEmulator", "(Ljava/lang/String;I)V"), \
5659
X(FetchSignInMethodsForEmail, "fetchSignInMethodsForEmail", \
5760
"(Ljava/lang/String;)" \
5861
"Lcom/google/android/gms/tasks/Task;"), \
@@ -185,6 +188,31 @@ void UpdateCurrentUser(AuthData* auth_data) {
185188
}
186189
}
187190

191+
const char* const kEmulatorLocalHost = "10.0.2.2";
192+
const char* const kEmulatorPort = "9099";
193+
void CheckEmulator(AuthData* auth_data) {
194+
JNIEnv* env = Env(auth_data);
195+
196+
// Use emulator as long as this env variable is set, regardless its value.
197+
if (std::getenv("USE_AUTH_EMULATOR") == nullptr) {
198+
LogDebug("Using Auth Prod for testing.");
199+
return;
200+
}
201+
202+
// Use AUTH_EMULATOR_PORT if it is set to non empty string,
203+
// otherwise use the default port.
204+
uint32_t port = std::stoi(kEmulatorPort);
205+
if (std::getenv("AUTH_EMULATOR_PORT") != nullptr) {
206+
port = std::stoi(std::getenv("AUTH_EMULATOR_PORT"));
207+
}
208+
209+
jstring j_host = env->NewStringUTF(kEmulatorLocalHost);
210+
env->CallVoidMethod(AuthImpl(auth_data),
211+
auth::GetMethodId(auth::kUseEmulator), j_host, port);
212+
env->DeleteLocalRef(j_host);
213+
firebase::util::CheckAndClearJniExceptions(env);
214+
}
215+
188216
// Release cached Java classes.
189217
static void ReleaseClasses(JNIEnv* env) {
190218
ReleaseAuthClasses(env);
@@ -269,6 +297,8 @@ void Auth::InitPlatformAuth(AuthData* auth_data) {
269297
// Ensure our User is in-line with underlying API's user.
270298
// It's possible for a user to already be logged-in on start-up.
271299
UpdateCurrentUser(auth_data);
300+
301+
CheckEmulator(auth_data);
272302
}
273303

274304
void Auth::DestroyPlatformAuth(AuthData* auth_data) {

auth/src/desktop/auth_desktop.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define FIREBASE_AUTH_SRC_DESKTOP_AUTH_DESKTOP_H_
1717

1818
#include <memory>
19+
#include <string>
1920

2021
#include "app/rest/request.h"
2122
#include "app/src/scheduler.h"

auth/src/desktop/rpcs/auth_request.cc

+38
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "app/src/heartbeat/heartbeat_controller_desktop.h"
2424
#include "app/src/include/firebase/app.h"
2525
#include "app/src/include/firebase/internal/mutex.h"
26+
#include "firebase/log.h"
2627

2728
namespace firebase {
2829
namespace auth {
@@ -37,6 +38,8 @@ AuthRequest::AuthRequest(::firebase::App& app, const char* schema,
3738
// dependencies upon other parts of this library. This complication is due to
3839
// the way the tests are currently configured where each library has minimal
3940
// dependencies.
41+
42+
CheckEmulator();
4043
static std::string auth_user_agent; // NOLINT
4144
static std::string extended_auth_user_agent; // NOLINT
4245
static Mutex* user_agent_mutex = new Mutex();
@@ -77,5 +80,40 @@ AuthRequest::AuthRequest(::firebase::App& app, const char* schema,
7780
}
7881
}
7982

83+
std::string AuthRequest::GetUrl() {
84+
if (emulator_url.empty()) {
85+
std::string url(kHttps);
86+
url += kServerURL;
87+
return url;
88+
} else {
89+
std::string url(kHttp);
90+
url += emulator_url;
91+
url += kServerURL;
92+
return url;
93+
}
94+
}
95+
96+
void AuthRequest::CheckEmulator() {
97+
if (!emulator_url.empty()) {
98+
LogDebug("Emulator Url already set: %s", emulator_url.c_str());
99+
return;
100+
}
101+
// Use emulator as long as this env variable is set, regardless its value.
102+
if (std::getenv("USE_AUTH_EMULATOR") == nullptr) {
103+
LogDebug("Using Auth Prod for testing.");
104+
return;
105+
}
106+
107+
emulator_url.append(kEmulatorLocalHost);
108+
emulator_url.append(":");
109+
// Use AUTH_EMULATOR_PORT if it is set to non empty string,
110+
// otherwise use the default port.
111+
if (std::getenv("AUTH_EMULATOR_PORT") == nullptr) {
112+
emulator_url.append(kEmulatorPort);
113+
} else {
114+
emulator_url.append(std::getenv("AUTH_EMULATOR_PORT"));
115+
}
116+
}
117+
80118
} // namespace auth
81119
} // namespace firebase

auth/src/desktop/rpcs/auth_request.h

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef FIREBASE_AUTH_SRC_DESKTOP_RPCS_AUTH_REQUEST_H_
1818
#define FIREBASE_AUTH_SRC_DESKTOP_RPCS_AUTH_REQUEST_H_
1919

20+
#include <string>
21+
2022
#include "app/rest/request_json.h"
2123
#include "app/src/include/firebase/app.h"
2224
#include "auth/request_generated.h"
@@ -28,6 +30,16 @@ namespace auth {
2830
// Key name for header when sending language code data.
2931
extern const char* kHeaderFirebaseLocale;
3032

33+
const char* const kHttps = "https://";
34+
35+
const char* const kHttp = "http://";
36+
37+
const char* const kServerURL =
38+
"www.googleapis.com/identitytoolkit/v3/relyingparty/";
39+
40+
const char* const kEmulatorLocalHost = "localhost";
41+
const char* const kEmulatorPort = "9099";
42+
3143
class AuthRequest
3244
: public firebase::rest::RequestJson<fbs::Request, fbs::RequestT> {
3345
public:
@@ -39,6 +51,12 @@ class AuthRequest
3951
bool deliver_heartbeat)
4052
: AuthRequest(app, reinterpret_cast<const char*>(schema),
4153
deliver_heartbeat) {}
54+
55+
std::string GetUrl();
56+
57+
private:
58+
void CheckEmulator();
59+
std::string emulator_url;
4260
};
4361

4462
} // namespace auth

auth/src/desktop/rpcs/create_auth_uri_request.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "auth/src/desktop/rpcs/create_auth_uri_request.h"
1616

17+
#include <string>
18+
1719
#include "app/src/assert.h"
1820
#include "app/src/include/firebase/app.h"
1921
#include "app/src/log.h"
@@ -27,11 +29,8 @@ CreateAuthUriRequest::CreateAuthUriRequest(::firebase::App& app,
2729
: AuthRequest(app, request_resource_data, true) {
2830
FIREBASE_ASSERT_RETURN_VOID(api_key);
2931

30-
const char api_host[] =
31-
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
32-
"createAuthUri?key=";
33-
std::string url;
34-
url.reserve(strlen(api_host) + strlen(api_key));
32+
const char api_host[] = "createAuthUri?key=";
33+
std::string url = GetUrl();
3534
url.append(api_host);
3635
url.append(api_key);
3736
set_url(url.c_str());

auth/src/desktop/rpcs/delete_account_request.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "auth/src/desktop/rpcs/delete_account_request.h"
1616

17+
#include <string>
18+
1719
#include "app/src/assert.h"
1820
#include "app/src/include/firebase/app.h"
1921

@@ -25,11 +27,8 @@ DeleteAccountRequest::DeleteAccountRequest(::firebase::App& app,
2527
: AuthRequest(app, request_resource_data, true) {
2628
FIREBASE_ASSERT_RETURN_VOID(api_key);
2729

28-
const char api_host[] =
29-
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
30-
"deleteAccount?key=";
31-
std::string url;
32-
url.reserve(strlen(api_host) + strlen(api_key));
30+
const char api_host[] = "deleteAccount?key=";
31+
std::string url = GetUrl();
3332
url.append(api_host);
3433
url.append(api_key);
3534
set_url(url.c_str());

auth/src/desktop/rpcs/get_account_info_request.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "auth/src/desktop/rpcs/get_account_info_request.h"
1616

17+
#include <string>
18+
1719
#include "app/src/assert.h"
1820
#include "app/src/include/firebase/app.h"
1921

@@ -39,11 +41,8 @@ GetAccountInfoRequest::GetAccountInfoRequest(::firebase::App& app,
3941
void GetAccountInfoRequest::SetUrl(const char* const api_key) {
4042
FIREBASE_ASSERT_RETURN_VOID(api_key);
4143

42-
const char api_host[] =
43-
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
44-
"getAccountInfo?key=";
45-
std::string url;
46-
url.reserve(strlen(api_host) + strlen(api_key));
44+
const char api_host[] = "getAccountInfo?key=";
45+
std::string url = GetUrl();
4746
url.append(api_host);
4847
url.append(api_key);
4948
set_url(url.c_str());

auth/src/desktop/rpcs/get_oob_confirmation_code_request.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "auth/src/desktop/rpcs/get_oob_confirmation_code_request.h"
1616

17+
#include <string>
18+
1719
#include "app/src/assert.h"
1820
#include "app/src/include/firebase/app.h"
1921

@@ -25,11 +27,8 @@ GetOobConfirmationCodeRequest::GetOobConfirmationCodeRequest(
2527
: AuthRequest(app, request_resource_data, true) {
2628
FIREBASE_ASSERT_RETURN_VOID(api_key);
2729

28-
const char api_host[] =
29-
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
30-
"getOobConfirmationCode?key=";
31-
std::string url;
32-
url.reserve(strlen(api_host) + strlen(api_key));
30+
const char api_host[] = "getOobConfirmationCode?key=";
31+
std::string url = GetUrl();
3332
url.append(api_host);
3433
url.append(api_key);
3534
set_url(url.c_str());

auth/src/desktop/rpcs/reset_password_request.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "auth/src/desktop/rpcs/reset_password_request.h"
1616

17+
#include <string>
18+
1719
#include "app/src/assert.h"
1820
#include "app/src/include/firebase/app.h"
1921
#include "app/src/log.h"
@@ -28,11 +30,8 @@ ResetPasswordRequest::ResetPasswordRequest(::firebase::App& app,
2830
: AuthRequest(app, request_resource_data, true) {
2931
FIREBASE_ASSERT_RETURN_VOID(api_key);
3032

31-
const char api_host[] =
32-
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
33-
"resetPassword?key=";
34-
std::string url;
35-
url.reserve(strlen(api_host) + strlen(api_key));
33+
const char api_host[] = "resetPassword?key=";
34+
std::string url = GetUrl();
3635
url.append(api_host);
3736
url.append(api_key);
3837
set_url(url.c_str());

auth/src/desktop/rpcs/set_account_info_request.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "auth/src/desktop/rpcs/set_account_info_request.h"
1616

17+
#include <string>
18+
1719
#include "app/src/assert.h"
1820
#include "app/src/include/firebase/app.h"
1921

@@ -25,11 +27,8 @@ SetAccountInfoRequest::SetAccountInfoRequest(::firebase::App& app,
2527
: AuthRequest(app, request_resource_data, true) {
2628
FIREBASE_ASSERT_RETURN_VOID(api_key);
2729

28-
const char api_host[] =
29-
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
30-
"setAccountInfo?key=";
31-
std::string url;
32-
url.reserve(strlen(api_host) + strlen(api_key));
30+
const char api_host[] = "setAccountInfo?key=";
31+
std::string url = GetUrl();
3332
url.append(api_host);
3433
url.append(api_key);
3534
set_url(url.c_str());

auth/src/desktop/rpcs/sign_up_new_user_request.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "auth/src/desktop/rpcs/sign_up_new_user_request.h"
1616

17+
#include <string>
18+
1719
#include "app/src/assert.h"
1820
#include "app/src/include/firebase/app.h"
1921

@@ -53,11 +55,8 @@ SignUpNewUserRequest::SignUpNewUserRequest(::firebase::App& app,
5355
void SignUpNewUserRequest::SetUrl(const char* api_key) {
5456
FIREBASE_ASSERT_RETURN_VOID(api_key);
5557

56-
const char api_host[] =
57-
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
58-
"signupNewUser?key=";
59-
std::string url;
60-
url.reserve(strlen(api_host) + strlen(api_key));
58+
const char api_host[] = "signupNewUser?key=";
59+
std::string url = GetUrl();
6160
url.append(api_host);
6261
url.append(api_key);
6362
set_url(url.c_str());

auth/src/desktop/rpcs/verify_assertion_request.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "auth/src/desktop/rpcs/verify_assertion_request.h"
1616

17+
#include <string>
18+
1719
#include "app/src/assert.h"
1820
#include "app/src/include/firebase/app.h"
1921

@@ -26,11 +28,8 @@ VerifyAssertionRequest::VerifyAssertionRequest(::firebase::App& app,
2628
: AuthRequest(app, request_resource_data, true) {
2729
FIREBASE_ASSERT_RETURN_VOID(api_key);
2830

29-
const char api_host[] =
30-
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
31-
"verifyAssertion?key=";
32-
std::string url;
33-
url.reserve(strlen(api_host) + strlen(api_key));
31+
const char api_host[] = "verifyAssertion?key=";
32+
std::string url = GetUrl();
3433
url.append(api_host);
3534
url.append(api_key);
3635
set_url(url.c_str());

auth/src/desktop/rpcs/verify_custom_token_request.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "auth/src/desktop/rpcs/verify_custom_token_request.h"
1616

17+
#include <string>
18+
1719
#include "app/src/assert.h"
1820
#include "app/src/include/firebase/app.h"
1921
#include "app/src/log.h"
@@ -27,11 +29,8 @@ VerifyCustomTokenRequest::VerifyCustomTokenRequest(::firebase::App& app,
2729
: AuthRequest(app, request_resource_data, true) {
2830
FIREBASE_ASSERT_RETURN_VOID(api_key);
2931

30-
const char api_host[] =
31-
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
32-
"verifyCustomToken?key=";
33-
std::string url;
34-
url.reserve(strlen(api_host) + strlen(api_key));
32+
const char api_host[] = "verifyCustomToken?key=";
33+
std::string url = GetUrl();
3534
url.append(api_host);
3635
url.append(api_key);
3736
set_url(url.c_str());

0 commit comments

Comments
 (0)