Skip to content

Commit 9d9229f

Browse files
chkuang-ga-maurice
authored andcommitted
Make sure id_token in IdTokenRefreshListener is refreshed immediately during callback.
id_token should be available during OnIdTokenChanged() callback. This also guarantee that the token is available right after signin. PiperOrigin-RevId: 249672187
1 parent 790c6da commit 9d9229f

File tree

2 files changed

+8
-32
lines changed

2 files changed

+8
-32
lines changed

auth/src/desktop/auth_desktop.cc

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,9 @@ void* CreatePlatformAuth(App* const app, void* const /*app_impl*/) {
9191
return auth;
9292
}
9393

94-
IdTokenRefreshListener::IdTokenRefreshListener()
95-
: token_timestamp_(0), get_token_semaphore_(1) {}
94+
IdTokenRefreshListener::IdTokenRefreshListener() : token_timestamp_(0) {}
9695

97-
IdTokenRefreshListener::~IdTokenRefreshListener() {
98-
// Wait for get token to complete.
99-
get_token_semaphore_.Wait();
100-
}
96+
IdTokenRefreshListener::~IdTokenRefreshListener() {}
10197

10298
void IdTokenRefreshListener::OnIdTokenChanged(Auth* auth) {
10399
// Note: Make sure to always make future_impl.mutex the innermost lock,
@@ -107,30 +103,13 @@ void IdTokenRefreshListener::OnIdTokenChanged(Auth* auth) {
107103
if (auth->current_user()) {
108104
ResetTokenRefreshCounter(auth->auth_data_);
109105

110-
if (get_token_semaphore_.TryWait()) {
111-
// Grab the current token, now that the token has been changed.
112-
// Since the system's token is fairly fresh (since we're in the callback
113-
// from having received it!) this should be a near-instant call, and
114-
// the system should never try to refresh the token. (Since we're not
115-
// asking it to force-refresh)
116-
Future<std::string> token_future = auth->current_user()->GetTokenInternal(
117-
false, kInternalFn_GetTokenForRefresher);
118-
119-
token_future.OnCompletion(
120-
[](const Future<std::string>& result, void* ptr) {
121-
auto listener = static_cast<IdTokenRefreshListener*>(ptr);
122-
MutexLock lock(listener->mutex_);
123-
if (result.status() == kFutureStatusComplete) {
124-
listener->current_token_ = *result.result();
125-
listener->token_timestamp_ = internal::GetTimestampEpoch();
126-
}
127-
listener->get_token_semaphore_.Post();
128-
},
129-
this);
130-
// Wait for the future to complete before we exit.
131-
// (Should be fast, since the token listener fired, so it should just
132-
// be cached.)
106+
// Retrieve id_token from auth_data
107+
{
108+
UserView::Reader reader = UserView::GetReader(auth->auth_data_);
109+
assert(reader.IsValid());
110+
current_token_ = reader->id_token;
133111
}
112+
token_timestamp_ = internal::GetTimestampEpoch();
134113
} else {
135114
current_token_ = "";
136115
}

auth/src/desktop/auth_desktop.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ class IdTokenRefreshListener : public IdTokenListener {
4646
Mutex mutex_;
4747
uint64_t token_timestamp_;
4848
std::string current_token_;
49-
// Acquired (decremented) when a GetToken() is in progress, signaled
50-
// incremented when GetToken() completes().
51-
Semaphore get_token_semaphore_;
5249
};
5350

5451
// This class handles the full lifecycle of the token refresh thread. It

0 commit comments

Comments
 (0)