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 5 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
16 changes: 16 additions & 0 deletions auth/src/desktop/auth_desktop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,22 @@ void Auth::SignOut() {
AuthenticationResult::SignOut(auth_data_);
}

void Auth::UseEmulator(const std::string host, uint32_t port) {
if (!auth_data_) return;
auto auth_impl = static_cast<AuthImpl*>(auth_data_->auth_impl);
auth_impl->emulator_host = host;
auth_impl->emulator_port = port;
}

std::string Auth::GetEmulatorUrl() {
if (!auth_data_) return "";
auto auth_impl = static_cast<AuthImpl*>(auth_data_->auth_impl);
if (auth_impl->emulator_host.empty()) {
return "";
}
return auth_impl->emulator_host + ":" + std::to_string(auth_impl->emulator_port) + "/";
}

// AuthStateListener to wait for current_user_DEPRECATED() until persistent
// cache load is finished.
class CurrentUserBlockListener : public firebase::auth::AuthStateListener {
Expand Down
3 changes: 3 additions & 0 deletions auth/src/desktop/auth_desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ struct AuthImpl {
// The current user language code. This can be set to the app’s current
// language by calling SetLanguageCode.
std::string language_code;

std::string emulator_host;
int32_t emulator_port;
};

// Constant, describing how often we automatically fetch a new auth token.
Expand Down
23 changes: 23 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 @@ -75,6 +79,25 @@ AuthRequest::AuthRequest(::firebase::App& app, const char* schema,
}
}
}

// Get emulator url
Auth* auth = Auth::GetAuth(&app);
emulator_url = auth->GetEmulatorUrl();
}

std::string AuthRequest::GetUrl() {
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
Expand Down
14 changes: 13 additions & 1 deletion 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,9 +30,15 @@ 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:
public:
// App is a non-const parameter because this constructor might modify App's
// internal HeartbeatController by logging or fetching heartbeats.
AuthRequest(::firebase::App& app, const char* schema, bool deliver_heartbeat);
Expand All @@ -39,6 +47,10 @@ class AuthRequest
bool deliver_heartbeat)
: AuthRequest(app, reinterpret_cast<const char*>(schema),
deliver_heartbeat) {}

std::string GetUrl();
private:
std::string emulator_url;
};

} // 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
11 changes: 11 additions & 0 deletions auth/src/include/firebase/auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define FIREBASE_AUTH_SRC_INCLUDE_FIREBASE_AUTH_H_

#include <vector>
#include <string>

#include "firebase/app.h"
#include "firebase/auth/user.h"
Expand Down Expand Up @@ -629,6 +630,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
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