Skip to content

Commit c9bef96

Browse files
authored
V16.1: Never reject a token response (#19651)
* fix: never reject a token response If a token response is rejected, then the pipeline will also fail because it does not understand that error. Let the API interceptors do their job instead and simply return the old, now-invalid token which will prompt the API interceptors to store the request states and retry them afterwards. * chore: removes unused timeoutsignal * chore: captures the stale token before potentially clearing it
1 parent d10ba42 commit c9bef96

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

src/Umbraco.Web.UI.Client/src/packages/core/auth/auth-flow.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ export class UmbAuthFlow {
9393
readonly #postLogoutRedirectUri: string;
9494
readonly #clientId: string;
9595
readonly #scope: string;
96-
readonly #timeoutSignal;
9796

9897
// tokens
9998
#tokenResponse?: TokenResponse;
@@ -113,13 +112,11 @@ export class UmbAuthFlow {
113112
openIdConnectUrl: string,
114113
redirectUri: string,
115114
postLogoutRedirectUri: string,
116-
timeoutSignal: Subject<void>,
117115
clientId = 'umbraco-back-office',
118116
scope = 'offline_access',
119117
) {
120118
this.#redirectUri = redirectUri;
121119
this.#postLogoutRedirectUri = postLogoutRedirectUri;
122-
this.#timeoutSignal = timeoutSignal;
123120
this.#clientId = clientId;
124121
this.#scope = scope;
125122

@@ -305,25 +302,25 @@ export class UmbAuthFlow {
305302
/**
306303
* This method will check if the token needs to be refreshed and if so, it will refresh it and return the new access token.
307304
* If the token does not need to be refreshed, it will return the current access token.
308-
* @returns The access token for the user.
305+
* @returns {Promise<string>} The access token for the user.
309306
*/
310307
async performWithFreshTokens(): Promise<string> {
311308
// if the access token is valid, return it
312309
if (this.#tokenResponse?.isValid()) {
313310
return Promise.resolve(this.#tokenResponse.accessToken);
314311
}
315312

313+
// if the access token is not valid, try to refresh it
316314
const success = await this.makeRefreshTokenRequest();
315+
const newToken = this.#tokenResponse?.accessToken ?? '';
317316

318317
if (!success) {
318+
// if the refresh token request failed, we need to clear the token state
319319
this.clearTokenStorage();
320-
this.#timeoutSignal.next();
321-
return Promise.reject('Missing tokenResponse.');
322320
}
323321

324-
return this.#tokenResponse
325-
? Promise.resolve(this.#tokenResponse.accessToken)
326-
: Promise.reject('Missing tokenResponse.');
322+
// if the refresh token request was successful, return the new access token
323+
return Promise.resolve(newToken);
327324
}
328325

329326
/**

src/Umbraco.Web.UI.Client/src/packages/core/auth/auth.context.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ export class UmbAuthContext extends UmbContextBase {
7373
this.#serverUrl = serverUrl;
7474
this.#backofficePath = backofficePath;
7575

76-
this.#authFlow = new UmbAuthFlow(
77-
serverUrl,
78-
this.getRedirectUrl(),
79-
this.getPostLogoutRedirectUrl(),
80-
this.#isTimeout,
81-
);
76+
this.#authFlow = new UmbAuthFlow(serverUrl, this.getRedirectUrl(), this.getPostLogoutRedirectUrl());
8277

8378
// Observe the authorization signal and close the auth window
8479
this.observe(

0 commit comments

Comments
 (0)