Skip to content

feat(auth): Add emulator support #1400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions auth/src/android/auth_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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 @@ -774,6 +775,18 @@ void Auth::SignOut() {
SetImplFromLocalRef(env, nullptr, &auth_data_->user_impl);
}

void Auth::UseEmulator(const std::string host, uint32_t port) {
JNIEnv* env = Env(auth_data_);
jstring j_host = env->NewStringUTF(host.c_str());
env->CallVoidMethod(AuthImpl(auth_data_), auth::GetMethodId(auth::kUseEmulator), j_host, port);
env->DeleteLocalRef(j_host);
firebase::util::CheckAndClearJniExceptions(env);
}

std::string Auth::GetEmulatorUrl() {
return "";
}

Future<void> Auth::SendPasswordResetEmail(const char* email) {
ReferenceCountedFutureImpl& futures = auth_data_->future_impl;
const auto handle = futures.SafeAlloc<void>(kAuthFn_SendPasswordResetEmail);
Expand Down
14 changes: 14 additions & 0 deletions auth/src/desktop/auth_desktop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
namespace firebase {
namespace auth {

static std::string g_emulator_host;
static uint32_t g_emulator_port;
namespace {

template <typename ResultT>
Expand Down Expand Up @@ -644,6 +646,18 @@ void Auth::SignOut() {
AuthenticationResult::SignOut(auth_data_);
}

void Auth::UseEmulator(const std::string host, uint32_t port) {
g_emulator_host = host;
g_emulator_port = port;
}

std::string Auth::GetEmulatorUrl() {
if (g_emulator_host.empty()) {
return "";
}
return g_emulator_host + ":" + std::to_string(g_emulator_port) + "/";
}

// AuthStateListener to wait for current_user_DEPRECATED() until persistent
// cache load is finished.
class CurrentUserBlockListener : public firebase::auth::AuthStateListener {
Expand Down
20 changes: 20 additions & 0 deletions auth/src/desktop/rpcs/auth_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
#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/auth.h"
#include "firebase/log.h"

namespace firebase {
namespace auth {

using ::firebase::auth::Auth;

// Key name for header when sending language code data.
const char* kHeaderFirebaseLocale = "X-Firebase-Locale";

Expand Down Expand Up @@ -77,5 +81,21 @@ AuthRequest::AuthRequest(::firebase::App& app, const char* schema,
}
}

std::string AuthRequest::GetUrl() {
std::string emulator_url = ::firebase::auth::Auth::GetEmulatorUrl();
if (emulator_url.empty()) {
std::string url(kHttps);
url += kServerURL;
LogDebug("AuthRequest::GetUrl(Prod): %s", url.c_str());
return url;
} else {
std::string url(kHttp);
url += emulator_url;
url += kServerURL;
LogDebug("AuthRequest::GetUrl(Emulator): %s", url.c_str());
return url;
}
}

} // namespace auth
} // namespace firebase
8 changes: 8 additions & 0 deletions auth/src/desktop/rpcs/auth_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ 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/";

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

std::string GetUrl();
};

} // namespace auth
Expand Down
7 changes: 2 additions & 5 deletions auth/src/desktop/rpcs/create_auth_uri_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,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
4 changes: 1 addition & 3 deletions auth/src/desktop/rpcs/delete_account_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ DeleteAccountRequest::DeleteAccountRequest(::firebase::App& app,
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));
std::string url = GetUrl();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix, elsewhere too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
7 changes: 2 additions & 5 deletions auth/src/desktop/rpcs/get_account_info_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,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
7 changes: 2 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 @@ -25,11 +25,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
7 changes: 2 additions & 5 deletions auth/src/desktop/rpcs/reset_password_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,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
7 changes: 2 additions & 5 deletions auth/src/desktop/rpcs/set_account_info_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,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
7 changes: 2 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 @@ -53,11 +53,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
7 changes: 2 additions & 5 deletions auth/src/desktop/rpcs/verify_assertion_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,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
7 changes: 2 additions & 5 deletions auth/src/desktop/rpcs/verify_custom_token_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,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
7 changes: 2 additions & 5 deletions auth/src/desktop/rpcs/verify_password_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ VerifyPasswordRequest::VerifyPasswordRequest(::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/"
"verifyPassword?key=";
std::string url;
url.reserve(strlen(api_host) + strlen(api_key));
const char api_host[] = "verifyPassword?key=";
std::string url = GetUrl();
url.append(api_host);
url.append(api_key);
set_url(url.c_str());
Expand Down
10 changes: 10 additions & 0 deletions auth/src/include/firebase/auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,16 @@ class Auth {
/// to be called explicitly.
void RemoveIdTokenListener(IdTokenListener* listener);
#endif // not SWIG

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run format_code.py!

#if !defined(DOXYGEN)
// Hidden from the public documentation for now

/// Configures Firebase Auth to connect to an emulated host instead of the remote backend.
void UseEmulator(const std::string host, uint32_t port);

/// Get the emulator url
static std::string GetEmulatorUrl();
#endif // !defined(DOXYGEN)

/// Gets the App this auth object is connected to.
App& app();
Expand Down
11 changes: 11 additions & 0 deletions auth/src/ios/auth_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,17 @@ void SignInResultCallback(FIRAuthDataResult *_Nullable auth_result, NSError *_Nu
SetUserImpl(auth_data_, NULL);
}

void Auth::UseEmulator(const std::string host, uint32_t port) {
NSUInteger ns_port = port;
[AuthImpl(auth_data_) useEmulatorWithHost:@(host.c_str())
port:ns_port
];
}

std::string Auth::GetEmulatorUrl() {
return "";
}

Future<void> Auth::SendPasswordResetEmail(const char *email) {
ReferenceCountedFutureImpl &futures = auth_data_->future_impl;
const auto handle = futures.SafeAlloc<void>(kAuthFn_SendPasswordResetEmail);
Expand Down