Skip to content

Commit

Permalink
feat(auth): Add emulator support (#1400)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
cynthiajoan and Cynthia Jiang authored Aug 6, 2023
1 parent 3002ff4 commit f198c6f
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ gcs_key_file.json
*_build/
cmake-build-*/
testing/test_framework/external/
CMakeFiles/
CMakeCache.txt

# XCode user specific folders
**/xcuserdata/
Expand Down
3 changes: 0 additions & 3 deletions app/src/include/firebase/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,8 @@ class App {
/// Get the App with the given name, or nullptr if none have been created.
static App* GetInstance(const char* name);

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

#ifndef SWIG
// <SWIG>
Expand Down
30 changes: 30 additions & 0 deletions auth/src/android/auth_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <assert.h>
#include <jni.h>

#include <string>

#include "app/src/assert.h"
#include "app/src/embedded_file.h"
#include "app/src/include/firebase/internal/mutex.h"
Expand Down Expand Up @@ -53,6 +55,7 @@ using util::JniStringToString;
X(RemoveIdTokenListener, "removeIdTokenListener", \
"(Lcom/google/firebase/auth/FirebaseAuth$IdTokenListener;)V"), \
X(SignOut, "signOut", "()V"), \
X(UseEmulator, "useEmulator", "(Ljava/lang/String;I)V"), \
X(FetchSignInMethodsForEmail, "fetchSignInMethodsForEmail", \
"(Ljava/lang/String;)" \
"Lcom/google/android/gms/tasks/Task;"), \
Expand Down Expand Up @@ -185,6 +188,31 @@ void UpdateCurrentUser(AuthData* auth_data) {
}
}

const char* const kEmulatorLocalHost = "10.0.2.2";
const char* const kEmulatorPort = "9099";
void CheckEmulator(AuthData* auth_data) {
JNIEnv* env = Env(auth_data);

// Use emulator as long as this env variable is set, regardless its value.
if (std::getenv("USE_AUTH_EMULATOR") == nullptr) {
LogDebug("Using Auth Prod for testing.");
return;
}

// Use AUTH_EMULATOR_PORT if it is set to non empty string,
// otherwise use the default port.
uint32_t port = std::stoi(kEmulatorPort);
if (std::getenv("AUTH_EMULATOR_PORT") != nullptr) {
port = std::stoi(std::getenv("AUTH_EMULATOR_PORT"));
}

jstring j_host = env->NewStringUTF(kEmulatorLocalHost);
env->CallVoidMethod(AuthImpl(auth_data),
auth::GetMethodId(auth::kUseEmulator), j_host, port);
env->DeleteLocalRef(j_host);
firebase::util::CheckAndClearJniExceptions(env);
}

// Release cached Java classes.
static void ReleaseClasses(JNIEnv* env) {
ReleaseAuthClasses(env);
Expand Down Expand Up @@ -269,6 +297,8 @@ void Auth::InitPlatformAuth(AuthData* auth_data) {
// Ensure our User is in-line with underlying API's user.
// It's possible for a user to already be logged-in on start-up.
UpdateCurrentUser(auth_data);

CheckEmulator(auth_data);
}

void Auth::DestroyPlatformAuth(AuthData* auth_data) {
Expand Down
1 change: 1 addition & 0 deletions auth/src/desktop/auth_desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define FIREBASE_AUTH_SRC_DESKTOP_AUTH_DESKTOP_H_

#include <memory>
#include <string>

#include "app/rest/request.h"
#include "app/src/scheduler.h"
Expand Down
38 changes: 38 additions & 0 deletions auth/src/desktop/rpcs/auth_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "app/src/heartbeat/heartbeat_controller_desktop.h"
#include "app/src/include/firebase/app.h"
#include "app/src/include/firebase/internal/mutex.h"
#include "firebase/log.h"

namespace firebase {
namespace auth {
Expand All @@ -37,6 +38,8 @@ AuthRequest::AuthRequest(::firebase::App& app, const char* schema,
// dependencies upon other parts of this library. This complication is due to
// the way the tests are currently configured where each library has minimal
// dependencies.

CheckEmulator();
static std::string auth_user_agent; // NOLINT
static std::string extended_auth_user_agent; // NOLINT
static Mutex* user_agent_mutex = new Mutex();
Expand Down Expand Up @@ -77,5 +80,40 @@ AuthRequest::AuthRequest(::firebase::App& app, const char* schema,
}
}

std::string AuthRequest::GetUrl() {
if (emulator_url.empty()) {
std::string url(kHttps);
url += kServerURL;
return url;
} else {
std::string url(kHttp);
url += emulator_url;
url += kServerURL;
return url;
}
}

void AuthRequest::CheckEmulator() {
if (!emulator_url.empty()) {
LogDebug("Emulator Url already set: %s", emulator_url.c_str());
return;
}
// Use emulator as long as this env variable is set, regardless its value.
if (std::getenv("USE_AUTH_EMULATOR") == nullptr) {
LogDebug("Using Auth Prod for testing.");
return;
}

emulator_url.append(kEmulatorLocalHost);
emulator_url.append(":");
// Use AUTH_EMULATOR_PORT if it is set to non empty string,
// otherwise use the default port.
if (std::getenv("AUTH_EMULATOR_PORT") == nullptr) {
emulator_url.append(kEmulatorPort);
} else {
emulator_url.append(std::getenv("AUTH_EMULATOR_PORT"));
}
}

} // namespace auth
} // namespace firebase
18 changes: 18 additions & 0 deletions auth/src/desktop/rpcs/auth_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef FIREBASE_AUTH_SRC_DESKTOP_RPCS_AUTH_REQUEST_H_
#define FIREBASE_AUTH_SRC_DESKTOP_RPCS_AUTH_REQUEST_H_

#include <string>

#include "app/rest/request_json.h"
#include "app/src/include/firebase/app.h"
#include "auth/request_generated.h"
Expand All @@ -28,6 +30,16 @@ namespace auth {
// Key name for header when sending language code data.
extern const char* kHeaderFirebaseLocale;

const char* const kHttps = "https://";

const char* const kHttp = "http://";

const char* const kServerURL =
"www.googleapis.com/identitytoolkit/v3/relyingparty/";

const char* const kEmulatorLocalHost = "localhost";
const char* const kEmulatorPort = "9099";

class AuthRequest
: public firebase::rest::RequestJson<fbs::Request, fbs::RequestT> {
public:
Expand All @@ -39,6 +51,12 @@ class AuthRequest
bool deliver_heartbeat)
: AuthRequest(app, reinterpret_cast<const char*>(schema),
deliver_heartbeat) {}

std::string GetUrl();

private:
void CheckEmulator();
std::string emulator_url;
};

} // namespace auth
Expand Down
9 changes: 4 additions & 5 deletions auth/src/desktop/rpcs/create_auth_uri_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

#include <string>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"
#include "app/src/log.h"
Expand All @@ -27,11 +29,8 @@ CreateAuthUriRequest::CreateAuthUriRequest(::firebase::App& app,
: AuthRequest(app, request_resource_data, true) {
FIREBASE_ASSERT_RETURN_VOID(api_key);

const char api_host[] =
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
"createAuthUri?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "createAuthUri?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
9 changes: 4 additions & 5 deletions auth/src/desktop/rpcs/delete_account_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

#include <string>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"

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

const char api_host[] =
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
"deleteAccount?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "deleteAccount?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
9 changes: 4 additions & 5 deletions auth/src/desktop/rpcs/get_account_info_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

#include <string>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"

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

const char api_host[] =
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
"getAccountInfo?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "getAccountInfo?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
9 changes: 4 additions & 5 deletions auth/src/desktop/rpcs/get_oob_confirmation_code_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

#include <string>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"

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

const char api_host[] =
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
"getOobConfirmationCode?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "getOobConfirmationCode?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
9 changes: 4 additions & 5 deletions auth/src/desktop/rpcs/reset_password_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

#include <string>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"
#include "app/src/log.h"
Expand All @@ -28,11 +30,8 @@ ResetPasswordRequest::ResetPasswordRequest(::firebase::App& app,
: AuthRequest(app, request_resource_data, true) {
FIREBASE_ASSERT_RETURN_VOID(api_key);

const char api_host[] =
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
"resetPassword?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "resetPassword?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
9 changes: 4 additions & 5 deletions auth/src/desktop/rpcs/set_account_info_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

#include <string>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"

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

const char api_host[] =
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
"setAccountInfo?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "setAccountInfo?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
9 changes: 4 additions & 5 deletions auth/src/desktop/rpcs/sign_up_new_user_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

#include <string>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"

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

const char api_host[] =
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
"signupNewUser?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "signupNewUser?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
9 changes: 4 additions & 5 deletions auth/src/desktop/rpcs/verify_assertion_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

#include <string>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"

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

const char api_host[] =
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
"verifyAssertion?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "verifyAssertion?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
9 changes: 4 additions & 5 deletions auth/src/desktop/rpcs/verify_custom_token_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

#include <string>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"
#include "app/src/log.h"
Expand All @@ -27,11 +29,8 @@ VerifyCustomTokenRequest::VerifyCustomTokenRequest(::firebase::App& app,
: AuthRequest(app, request_resource_data, true) {
FIREBASE_ASSERT_RETURN_VOID(api_key);

const char api_host[] =
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/"
"verifyCustomToken?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "verifyCustomToken?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
Loading

0 comments on commit f198c6f

Please sign in to comment.