Skip to content

Commit d7fbaa1

Browse files
chkuang-ga-maurice
authored andcommitted
Several change to Auth Function Registry for desktop
1. Add a new FnAuthGetTokenAsync which take a force_refresh(bool) and returns a Future<std::string>. 2. Add FindAuth(app) which only return existing Auth or nullptr. PiperOrigin-RevId: 257889774
1 parent 042cca6 commit d7fbaa1

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

app/src/function_registry.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ namespace internal {
3333
enum FunctionId {
3434
FnAuthGetCurrentToken,
3535
FnAuthStartTokenListener,
36-
FnAuthStopTokenListener
36+
FnAuthStopTokenListener,
37+
FnAuthGetTokenAsync
3738
};
3839

3940
// Class for providing a generic way for firebase libraries to expose their

auth/src/auth.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ Mutex g_auths_mutex; // NOLINT
6868
Auth* Auth::GetAuth(App* app, InitResult* init_result_out) {
6969
MutexLock lock(g_auths_mutex);
7070
// Return the Auth if it already exists.
71-
std::map<App*, Auth*>::iterator it = g_auths.find(app);
72-
if (it != g_auths.end()) {
71+
Auth* existing_auth = FindAuth(app);
72+
if (existing_auth) {
7373
if (init_result_out != nullptr) *init_result_out = kInitResultSuccess;
74-
return it->second;
74+
return existing_auth;
7575
}
76+
7677
FIREBASE_UTIL_RETURN_NULL_IF_GOOGLE_PLAY_UNAVAILABLE(*app, init_result_out);
7778

7879
// Create the platform dependent version of Auth.
@@ -91,6 +92,17 @@ Auth* Auth::GetAuth(App* app, InitResult* init_result_out) {
9192
return auth;
9293
}
9394

95+
// static
96+
Auth* Auth::FindAuth(App* app) {
97+
MutexLock lock(g_auths_mutex);
98+
// Return the Auth if it already exists.
99+
std::map<App*, Auth*>::iterator it = g_auths.find(app);
100+
if (it != g_auths.end()) {
101+
return it->second;
102+
}
103+
return nullptr;
104+
}
105+
94106
// Auth uses the pimpl mechanism to hide internal data types from the interface.
95107
Auth::Auth(App* app, void* auth_impl) : auth_data_(new AuthData) {
96108
FIREBASE_ASSERT(app != nullptr && auth_impl != nullptr);

auth/src/data.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ enum AuthApiFunction {
5858

5959
// Internal functions that are still handles, but are only used internally:
6060
kInternalFn_GetTokenForRefresher,
61+
kInternalFn_GetTokenForFunctionRegistry,
6162

6263
kNumAuthFunctions
6364
};

auth/src/desktop/auth_desktop.cc

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ uint64_t IdTokenRefreshListener::GetTokenTimestamp() {
138138
// GetAuthToken function on the current auth object.
139139
bool Auth::GetAuthTokenForRegistry(App* app, void* /*unused*/, void* out) {
140140
if (!app) return false;
141-
InitResult init_result;
142-
Auth* auth = Auth::GetAuth(app, &init_result);
141+
Auth* auth = Auth::FindAuth(app);
143142
if (auth) {
143+
// Make sure the persistent cache is loaded.
144+
auth->current_user();
145+
144146
auto result = static_cast<std::string*>(out);
145147
MutexLock lock(auth->auth_data_->token_listener_mutex);
146148
auto auth_impl = static_cast<AuthImpl*>(auth->auth_data_->auth_impl);
@@ -150,11 +152,37 @@ bool Auth::GetAuthTokenForRegistry(App* app, void* /*unused*/, void* out) {
150152
return false;
151153
}
152154

155+
// It basically just calls the public GetToken function on the current user and
156+
// output the Future.
157+
bool Auth::GetAuthTokenAsyncForRegistry(App* app, void* force_refresh,
158+
void* out) {
159+
Future<std::string>* out_future = static_cast<Future<std::string>*>(out);
160+
// Reset the future
161+
if (out_future) *out_future = Future<std::string>();
162+
bool* in_force_refresh = static_cast<bool*>(force_refresh);
163+
164+
if (!app) return false;
165+
assert(force_refresh);
166+
167+
Auth* auth = Auth::FindAuth(app);
168+
if (auth) {
169+
User* user = auth->current_user();
170+
if (user) {
171+
Future<std::string> future = user->GetTokenInternal(
172+
*in_force_refresh, kInternalFn_GetTokenForFunctionRegistry);
173+
if (out_future) {
174+
*out_future = future;
175+
}
176+
return true;
177+
}
178+
}
179+
return false;
180+
}
181+
153182
bool Auth::StartTokenRefreshThreadForRegistry(App* app, void* /*unused*/,
154183
void* /*unused*/) {
155184
if (!app) return false;
156-
InitResult init_result;
157-
Auth* auth = Auth::GetAuth(app, &init_result);
185+
Auth* auth = Auth::FindAuth(app);
158186
if (auth) {
159187
EnableTokenAutoRefresh(auth->auth_data_);
160188
return true;
@@ -165,8 +193,7 @@ bool Auth::StartTokenRefreshThreadForRegistry(App* app, void* /*unused*/,
165193
bool Auth::StopTokenRefreshThreadForRegistry(App* app, void* /*unused*/,
166194
void* /*unused*/) {
167195
if (!app) return false;
168-
InitResult init_result;
169-
Auth* auth = Auth::GetAuth(app, &init_result);
196+
Auth* auth = Auth::FindAuth(app);
170197
if (auth) {
171198
DisableTokenAutoRefresh(auth->auth_data_);
172199
return true;
@@ -184,6 +211,8 @@ void Auth::InitPlatformAuth(AuthData* const auth_data) {
184211
auth_data->app->function_registry()->RegisterFunction(
185212
internal::FnAuthStopTokenListener,
186213
Auth::StopTokenRefreshThreadForRegistry);
214+
auth_data->app->function_registry()->RegisterFunction(
215+
internal::FnAuthGetTokenAsync, Auth::GetAuthTokenAsyncForRegistry);
187216

188217
// Load existing UserData
189218
InitializeUserDataPersist(auth_data);
@@ -201,6 +230,9 @@ void Auth::DestroyPlatformAuth(AuthData* const auth_data) {
201230
internal::FnAuthStartTokenListener);
202231
auth_data->app->function_registry()->UnregisterFunction(
203232
internal::FnAuthStopTokenListener);
233+
auth_data->app->function_registry()->UnregisterFunction(
234+
internal::FnAuthGetTokenAsync);
235+
204236
DestroyTokenRefresher(auth_data);
205237

206238
{

auth/src/include/firebase/auth.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,21 @@ class Auth {
494494
friend void ResetTokenRefreshCounter(AuthData* authData);
495495
/// @endcond
496496

497+
// Find Auth instance using App. Return null if the instance does not exist.
498+
static Auth* FindAuth(App* app);
499+
497500
// Provides access to the auth token for the current user. Returns the
498501
// current user's auth token, or an empty string, if there isn't one.
502+
// Note that this can potentially return an expired token from the cache.
499503
static bool GetAuthTokenForRegistry(App* app, void* /*unused*/, void* out);
500504

505+
// Provides asynchronous access to the auth token for the current user. Allow
506+
// the caller to force-refresh the token. Even without force-refresh, this
507+
// ensure the future contain a fresh current user's auth token. This function
508+
// returns invalid future if user data is not available.
509+
static bool GetAuthTokenAsyncForRegistry(App* app, void* force_refresh,
510+
void* out_future);
511+
501512
// Starts and stops a thread to ensure that the cached auth token is never
502513
// kept long enough for it to expire. Refcounted, so multiple classes can
503514
// register this without causing problems.

auth/src/include/firebase/auth/user.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ class User : public UserInfoInterface {
453453
/// @cond FIREBASE_APP_INTERNAL
454454
friend class IdTokenRefreshThread;
455455
friend class IdTokenRefreshListener;
456+
friend class Auth;
456457
Future<std::string> GetTokenInternal(const bool force_refresh,
457458
const int future_identifier);
458459
/// @endcond

0 commit comments

Comments
 (0)