Skip to content

Commit ca7b3b1

Browse files
cynthiajianga-maurice
authored andcommitted
[Auth] refresh token after load
+ After load user data, call EnsureRefresh, which will refresh the token once it expires. + Modify the EnsureRefresh logic to not notify listener if it's called from persistence, since we will always notify listeners after load. + the static scheduler for secure manager will cancel all queued task before final destruct. + Replace Auth desktop async call from using thread to use scheduler, keep the async call counter to make sure all calls are done before auth desctruction + fix auth_test's desctruction, delete configSet last PiperOrigin-RevId: 258621117
1 parent 05ac8c3 commit ca7b3b1

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

auth/src/desktop/auth_desktop.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <memory>
1919

2020
#include "app/rest/request.h"
21+
#include "app/src/scheduler.h"
2122
#include "app/src/semaphore.h"
2223
#include "app/src/thread.h"
2324
#include "app/src/time.h"
@@ -117,6 +118,9 @@ struct AuthImpl {
117118
Semaphore async_sem;
118119
Mutex async_mutex;
119120
int active_async_calls;
121+
122+
// Serializes all REST call from this object.
123+
scheduler::Scheduler scheduler_;
120124
};
121125

122126
// Constant, describing how often we automatically fetch a new auth token.

auth/src/desktop/auth_util.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
#include "app/rest/request.h"
2222
#include "app/rest/transport_builder.h"
2323
#include "app/src/assert.h"
24+
#include "app/src/callback.h"
2425
#include "app/src/log.h"
26+
#include "app/src/scheduler.h"
2527
#include "app/src/semaphore.h"
26-
#include "app/src/thread.h"
2728
#include "auth/src/common.h"
2829
#include "auth/src/data.h"
2930
#include "auth/src/desktop/auth_constants.h"
@@ -39,6 +40,8 @@
3940
namespace firebase {
4041
namespace auth {
4142

43+
using firebase::callback::NewCallback;
44+
4245
const char* GetApiKey(const AuthData& auth_data);
4346

4447
// Completes the given promise by translating the AuthenticationResult. If the
@@ -99,14 +102,17 @@ inline Future<ResultT> CallAsync(
99102

100103
StartAsyncFunction(auth_data->auth_impl);
101104
typedef AuthDataHandle<ResultT, RequestT> HandleT;
102-
firebase::Thread(
105+
106+
auto scheduler_callback = NewCallback(
103107
[](HandleT* const raw_auth_data_handle) {
104108
std::unique_ptr<HandleT> handle(raw_auth_data_handle);
105109
handle->callback(handle.get());
106110
EndAsyncFunction(handle->auth_data->auth_impl);
107111
},
108-
new HandleT(auth_data, promise, std::move(request), callback))
109-
.Detach();
112+
new HandleT(auth_data, promise, std::move(request), callback));
113+
auto auth_impl = static_cast<AuthImpl*>(auth_data->auth_impl);
114+
auth_impl->scheduler_.Schedule(scheduler_callback);
115+
110116
return promise.future();
111117
}
112118

auth/src/desktop/user_desktop.cc

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
#include "app/rest/transport_builder.h"
2121
#include "app/rest/util.h"
22+
#include "app/src/callback.h"
2223
#include "app/src/include/firebase/future.h"
24+
#include "app/src/scheduler.h"
2325
#include "app/src/secure/user_secure_manager.h"
24-
#include "app/src/thread.h"
2526
#include "auth/src/common.h"
2627
#include "auth/src/data.h"
2728
#include "auth/src/desktop/auth_data_handle.h"
@@ -54,6 +55,7 @@ namespace firebase {
5455
namespace auth {
5556

5657
using firebase::app::secure::UserSecureManager;
58+
using firebase::callback::NewCallback;
5759

5860
namespace {
5961

@@ -112,7 +114,8 @@ GetTokenResult GetTokenIfFresh(const UserView::Reader& user,
112114
// Note: this is a blocking call! The caller is supposed to call this function
113115
// on the appropriate thread.
114116
GetTokenResult EnsureFreshToken(AuthData* const auth_data,
115-
const bool force_refresh) {
117+
const bool force_refresh,
118+
const bool notify_listener) {
116119
FIREBASE_ASSERT_RETURN(GetTokenResult(kAuthErrorFailure), auth_data);
117120

118121
GetTokenResult old_token(kAuthErrorFailure);
@@ -149,13 +152,18 @@ GetTokenResult EnsureFreshToken(AuthData* const auth_data,
149152
return GetTokenResult(kAuthErrorNoSignedInUser);
150153
}
151154
}
152-
if (has_token_changed) {
155+
if (has_token_changed && notify_listener) {
153156
NotifyIdTokenListeners(auth_data);
154157
}
155158

156159
return GetTokenResult(response.id_token());
157160
}
158161

162+
GetTokenResult EnsureFreshToken(AuthData* const auth_data,
163+
const bool force_refresh) {
164+
return EnsureFreshToken(auth_data, force_refresh, true);
165+
}
166+
159167
// Checks whether there is a currently logged in user. If no user is signed in,
160168
// fails the given promise and returns false. Otherwise, doesn't touch the
161169
// promise and returns true.
@@ -181,7 +189,8 @@ Future<ResultT> CallAsyncWithFreshToken(
181189
StartAsyncFunction(auth_data->auth_impl);
182190

183191
typedef AuthDataHandle<ResultT, RequestT> HandleT;
184-
firebase::Thread(
192+
193+
auto scheduler_callback = NewCallback(
185194
[](HandleT* const raw_auth_data_handle) {
186195
std::unique_ptr<HandleT> handle(raw_auth_data_handle);
187196

@@ -197,8 +206,10 @@ Future<ResultT> CallAsyncWithFreshToken(
197206
handle->callback(handle.get());
198207
EndAsyncFunction(handle->auth_data->auth_impl);
199208
},
200-
new HandleT(auth_data, promise, std::move(request), callback))
201-
.Detach();
209+
new HandleT(auth_data, promise, std::move(request), callback));
210+
auto auth_impl = static_cast<AuthImpl*>(auth_data->auth_impl);
211+
auth_impl->scheduler_.Schedule(scheduler_callback);
212+
202213
return promise.LastResult();
203214
}
204215

@@ -472,14 +483,37 @@ void AssignLoadedData(const Future<std::string>& future, AuthData* auth_data) {
472483
void HandleLoadedData(const Future<std::string>& future, void* auth_data) {
473484
auto cast_auth_data = static_cast<AuthData*>(auth_data);
474485
AssignLoadedData(future, cast_auth_data);
475-
LoadFinishTriggerListeners(cast_auth_data);
486+
487+
// End async call for LoadUserData
488+
EndAsyncFunction(cast_auth_data->auth_impl);
489+
490+
auto scheduler_callback = NewCallback(
491+
[](AuthData* callback_auth_data) {
492+
// Don't trigger token listeners if token get refreshed, since
493+
// it will get triggered inside LoadFinishTriggerListeners anyways.
494+
const GetTokenResult get_token_result =
495+
EnsureFreshToken(callback_auth_data, false, false);
496+
LoadFinishTriggerListeners(callback_auth_data);
497+
EndAsyncFunction(callback_auth_data->auth_impl);
498+
},
499+
cast_auth_data);
500+
// Start Async call for EnsureFreshToken
501+
StartAsyncFunction(cast_auth_data->auth_impl);
502+
auto auth_impl = static_cast<AuthImpl*>(cast_auth_data->auth_impl);
503+
auth_impl->scheduler_.Schedule(scheduler_callback);
504+
}
505+
506+
void HandlePersistenceComplete(const Future<void>& future, void* auth_data) {
507+
auto cast_auth_data = static_cast<AuthData*>(auth_data);
508+
EndAsyncFunction(cast_auth_data->auth_impl);
476509
}
477510

478511
Future<std::string> UserDataPersist::LoadUserData(AuthData* auth_data) {
479512
if (auth_data == nullptr) {
480513
return Future<std::string>();
481514
}
482515

516+
StartAsyncFunction(auth_data->auth_impl);
483517
Future<std::string> future =
484518
user_secure_manager_->LoadUserData(auth_data->app->name());
485519
future.OnCompletion(HandleLoadedData, auth_data);
@@ -531,11 +565,11 @@ Future<void> UserDataPersist::SaveUserData(AuthData* auth_data) {
531565
}
532566

533567
Future<void> UserDataPersist::DeleteUserData(AuthData* auth_data) {
534-
return user_secure_manager_->DeleteUserData(auth_data->app->name());
535-
}
536-
537-
Future<void> UserDataPersist::DeleteAllData() {
538-
return user_secure_manager_->DeleteAllData();
568+
StartAsyncFunction(auth_data->auth_impl);
569+
Future<void> future =
570+
user_secure_manager_->DeleteUserData(auth_data->app->name());
571+
future.OnCompletion(HandlePersistenceComplete, auth_data);
572+
return future;
539573
}
540574

541575
User::~User() {

auth/src/desktop/user_desktop.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ class UserDataPersist : public firebase::auth::AuthStateListener {
111111
Future<std::string> LoadUserData(AuthData* auth_data);
112112
Future<void> DeleteUserData(AuthData* auth_data);
113113

114-
Future<void> DeleteAllData();
115-
116114
private:
117115
UniquePtr<firebase::app::secure::UserSecureManager> user_secure_manager_;
118116
};

0 commit comments

Comments
 (0)