@@ -101,8 +101,12 @@ class MsalAuthorizeService implements AuthorizeService {
101
101
scopes : scopes
102
102
} ;
103
103
104
- const response = await this . _msalApplication . acquireTokenSilent ( silentRequest ) ;
105
- return response . idTokenClaims ;
104
+ try {
105
+ const response = await this . _msalApplication . acquireTokenSilent ( silentRequest ) ;
106
+ return response . idTokenClaims ;
107
+ } catch ( e ) {
108
+ await this . signInCore ( silentRequest ) ;
109
+ }
106
110
}
107
111
108
112
async getAccessToken ( request ?: AccessTokenRequestOptions ) : Promise < AccessTokenResult > {
@@ -224,10 +228,9 @@ class MsalAuthorizeService implements AuthorizeService {
224
228
async completeSignIn ( ) {
225
229
// Make sure that the redirect handler has completed execution before
226
230
// completing sign in.
227
- await this . _redirectCallback ;
228
- const account = this . getAccount ( ) ;
229
- if ( account ) {
230
- return this . success ( account ) ;
231
+ var authenticationResult = await this . _redirectCallback ;
232
+ if ( authenticationResult ) {
233
+ return authenticationResult ;
231
234
}
232
235
return this . operationCompleted ( ) ;
233
236
}
@@ -253,7 +256,7 @@ class MsalAuthorizeService implements AuthorizeService {
253
256
const logoutStateId = sessionStorage . getItem ( `${ AuthenticationService . _infrastructureKey } .LogoutState` ) ;
254
257
const updatedUrl = new URL ( url ) ;
255
258
updatedUrl . search = `?state=${ logoutStateId } ` ;
256
- const logoutState = await this . retrieveState ( updatedUrl . href , /*isLogout*/ true ) ;
259
+ const logoutState = await this . retrieveState ( updatedUrl . href , null , /*isLogout*/ true ) ;
257
260
258
261
sessionStorage . removeItem ( `${ AuthenticationService . _infrastructureKey } .LogoutState` ) ;
259
262
@@ -285,25 +288,22 @@ class MsalAuthorizeService implements AuthorizeService {
285
288
return base64UrlIdentifier ;
286
289
}
287
290
288
- async retrieveState < T > ( url : string , isLogout : boolean = false ) : Promise < T | undefined > {
289
- const parsedUrl = new URL ( url ) ;
290
- const fromHash = parsedUrl . hash && parsedUrl . hash . length > 0 && new URLSearchParams ( parsedUrl . hash . substring ( 1 ) ) ;
291
- let state = fromHash && fromHash . getAll ( 'state' ) ;
292
- if ( state && state . length > 1 ) {
291
+ retrieveState < T > ( url : string | null , providedState : string | null = null , isLogout : boolean = false ) : T | undefined {
292
+ let stateFromUrl ;
293
+ // Parse the state key from the `search` query parameter in the URL if provided
294
+ if ( url ) {
295
+ const parsedUrl = new URL ( url ) ;
296
+ stateFromUrl = parsedUrl . searchParams && parsedUrl . searchParams . getAll ( 'state' ) ;
297
+ }
298
+
299
+ // Chose the provided state from MSAL. Otherwise, choose the state computed from the URL
300
+ const state = providedState || stateFromUrl ;
301
+
302
+ if ( ! state ) {
293
303
return undefined ;
294
- } else if ( ! state || state . length == 0 ) {
295
- state = parsedUrl . searchParams && parsedUrl . searchParams . getAll ( 'state' ) ;
296
- if ( ! state || state . length !== 1 ) {
297
- return undefined ;
298
- }
299
304
}
300
305
301
- // We need to calculate the state key in two different ways. The reason for it is that
302
- // msal.js doesn't support the state parameter on logout flows, which forces us to shim our own logout state.
303
- // The format then is different, as msal follows the pattern state=<<guid>>|<<user_state>> and our format
304
- // simple uses <<base64urlIdentifier>>.
305
- const appState = ! isLogout ? this . getAccountState ( state [ 0 ] ) : state [ 0 ] ;
306
- const stateKey = `${ AuthenticationService . _infrastructureKey } .AuthorizeService.${ appState } ` ;
306
+ const stateKey = `${ AuthenticationService . _infrastructureKey } .AuthorizeService.${ state } ` ;
307
307
const stateString = sessionStorage . getItem ( stateKey ) ;
308
308
if ( stateString ) {
309
309
sessionStorage . removeItem ( stateKey ) ;
@@ -336,9 +336,9 @@ class MsalAuthorizeService implements AuthorizeService {
336
336
}
337
337
338
338
private handleResult ( result : Msal . AuthenticationResult | null ) {
339
- if ( result != null ) {
339
+ if ( result ) {
340
340
this . _account = result . account ;
341
- return this . success ( result . state ) ;
341
+ return this . success ( this . retrieveState ( null , result . state ) ) ;
342
342
} else {
343
343
return this . operationCompleted ( ) ;
344
344
}
0 commit comments