@@ -28,8 +28,7 @@ import {
28
28
SilentFlowClient ,
29
29
EndSessionRequest ,
30
30
BaseAuthRequest ,
31
- Logger ,
32
- ServerAuthorizationCodeResponse
31
+ Logger
33
32
} from "@azure/msal-common" ;
34
33
import { buildConfiguration , Configuration } from "../config/Configuration" ;
35
34
import { BrowserStorage } from "../cache/BrowserStorage" ;
@@ -38,14 +37,13 @@ import { RedirectHandler } from "../interaction_handler/RedirectHandler";
38
37
import { PopupHandler } from "../interaction_handler/PopupHandler" ;
39
38
import { SilentHandler } from "../interaction_handler/SilentHandler" ;
40
39
import { BrowserAuthError } from "../error/BrowserAuthError" ;
41
- import { BrowserConstants , TemporaryCacheKeys , DEFAULT_REQUEST , InteractionType } from "../utils/BrowserConstants" ;
40
+ import { BrowserConstants , TemporaryCacheKeys , DEFAULT_REQUEST } from "../utils/BrowserConstants" ;
42
41
import { BrowserUtils } from "../utils/BrowserUtils" ;
43
42
import { version } from "../../package.json" ;
44
43
import { IPublicClientApplication } from "./IPublicClientApplication" ;
45
44
import { RedirectRequest } from "../request/RedirectRequest" ;
46
45
import { PopupRequest } from "../request/PopupRequest" ;
47
46
import { SilentRequest } from "../request/SilentRequest" ;
48
- import { BrowserProtocolUtils , BrowserStateObject } from "../utils/BrowserProtocolUtils" ;
49
47
50
48
/**
51
49
* The PublicClientApplication class is the object exposed by the library to perform authentication and authorization functions in Single Page Applications
@@ -134,76 +132,45 @@ export class PublicClientApplication implements IPublicClientApplication {
134
132
* - if true, performs logic to cache and navigate
135
133
* - if false, handles hash string and parses response
136
134
*/
137
- private async handleRedirectResponse ( ) : Promise < AuthenticationResult | null > {
138
- const responseHash = this . getRedirectResponseHash ( ) ;
139
- if ( ! responseHash ) {
140
- // Not a recognized server response hash or hash not associated with a redirect request
141
- return null ;
142
- }
143
-
144
- // If navigateToLoginRequestUrl is true, get the url where the redirect request was initiated
135
+ private async handleRedirectResponse ( ) : Promise < AuthenticationResult > {
136
+ // Get current location hash from window or cache.
137
+ const { location : { hash } } = window ;
138
+ const cachedHash = this . browserStorage . getItem ( this . browserStorage . generateCacheKey ( TemporaryCacheKeys . URL_HASH ) , CacheSchemaType . TEMPORARY ) as string ;
139
+ const isResponseHash = UrlString . hashContainsKnownProperties ( hash ) ;
145
140
const loginRequestUrl = this . browserStorage . getItem ( this . browserStorage . generateCacheKey ( TemporaryCacheKeys . ORIGIN_URI ) , CacheSchemaType . TEMPORARY ) as string ;
146
- const loginRequestUrlNormalized = UrlString . removeHashFromUrl ( loginRequestUrl || "" ) ;
147
- const currentUrlNormalized = UrlString . removeHashFromUrl ( window . location . href ) ;
148
141
142
+ const currentUrlNormalized = UrlString . removeHashFromUrl ( window . location . href ) ;
143
+ const loginRequestUrlNormalized = UrlString . removeHashFromUrl ( loginRequestUrl || "" ) ;
149
144
if ( loginRequestUrlNormalized === currentUrlNormalized && this . config . auth . navigateToLoginRequestUrl ) {
150
- if ( loginRequestUrl . indexOf ( "#" ) > - 1 ) {
151
- // Replace current hash with non-msal hash, if present
152
- BrowserUtils . replaceHash ( loginRequestUrl ) ;
153
- }
154
145
// We are on the page we need to navigate to - handle hash
155
- return this . handleHash ( responseHash ) ;
156
- } else if ( ! this . config . auth . navigateToLoginRequestUrl ) {
157
- return this . handleHash ( responseHash ) ;
158
- } else if ( ! BrowserUtils . isInIframe ( ) ) {
146
+ // Replace current hash with non-msal hash, if present
147
+ BrowserUtils . replaceHash ( loginRequestUrl ) ;
148
+ return this . handleHash ( isResponseHash ? hash : cachedHash ) ;
149
+ }
150
+
151
+ if ( ! this . config . auth . navigateToLoginRequestUrl ) {
152
+ // We don't need to navigate - handle hash
153
+ BrowserUtils . clearHash ( ) ;
154
+ return this . handleHash ( isResponseHash ? hash : cachedHash ) ;
155
+ }
156
+
157
+ if ( isResponseHash && ! BrowserUtils . isInIframe ( ) ) {
159
158
// Returned from authority using redirect - need to perform navigation before processing response
160
- // Cache the hash to be retrieved after the next redirect
161
159
const hashKey = this . browserStorage . generateCacheKey ( TemporaryCacheKeys . URL_HASH ) ;
162
- this . browserStorage . setItem ( hashKey , responseHash , CacheSchemaType . TEMPORARY ) ;
163
- if ( ! loginRequestUrl || loginRequestUrl === "null" ) {
160
+ this . browserStorage . setItem ( hashKey , hash , CacheSchemaType . TEMPORARY ) ;
161
+ if ( StringUtils . isEmpty ( loginRequestUrl ) || loginRequestUrl === "null" ) {
164
162
// Redirect to home page if login request url is null (real null or the string null)
165
- const homepage = BrowserUtils . getHomepage ( ) ;
166
- // Cache the homepage under ORIGIN_URI to ensure cached hash is processed on homepage
167
- this . browserStorage . setItem ( this . browserStorage . generateCacheKey ( TemporaryCacheKeys . ORIGIN_URI ) , homepage , CacheSchemaType . TEMPORARY ) ;
168
163
this . logger . warning ( "Unable to get valid login request url from cache, redirecting to home page" ) ;
169
- BrowserUtils . navigateWindow ( homepage , true ) ;
164
+ BrowserUtils . navigateWindow ( "/" , true ) ;
170
165
} else {
171
- // Navigate to page that initiated the redirect request
166
+ // Navigate to target url
172
167
BrowserUtils . navigateWindow ( loginRequestUrl , true ) ;
173
168
}
174
169
}
175
170
176
171
return null ;
177
172
}
178
173
179
- /**
180
- * Gets the response hash for a redirect request
181
- * Returns null if interactionType in the state value is not "redirect" or the hash does not contain known properties
182
- * @returns {string }
183
- */
184
- private getRedirectResponseHash ( ) : string {
185
- // Get current location hash from window or cache.
186
- const { location : { hash } } = window ;
187
- const isResponseHash = UrlString . hashContainsKnownProperties ( hash ) ;
188
- const cachedHash = this . browserStorage . getItem ( this . browserStorage . generateCacheKey ( TemporaryCacheKeys . URL_HASH ) , CacheSchemaType . TEMPORARY ) as string ;
189
- this . browserStorage . removeItem ( this . browserStorage . generateCacheKey ( TemporaryCacheKeys . URL_HASH ) ) ;
190
-
191
- const responseHash = isResponseHash ? hash : cachedHash ;
192
- if ( responseHash ) {
193
- // Deserialize hash fragment response parameters.
194
- const serverParams : ServerAuthorizationCodeResponse = UrlString . getDeserializedHash ( responseHash ) ;
195
- const platformStateObj : BrowserStateObject = BrowserProtocolUtils . extractBrowserRequestState ( this . browserCrypto , serverParams . state ) ;
196
- if ( platformStateObj . interactionType !== InteractionType . REDIRECT ) {
197
- return null ;
198
- } else {
199
- BrowserUtils . clearHash ( ) ;
200
- return responseHash ;
201
- }
202
- }
203
-
204
- return null ;
205
- }
206
-
207
174
/**
208
175
* Checks if hash exists and handles in window.
209
176
* @param responseHash
@@ -248,7 +215,7 @@ export class PublicClientApplication implements IPublicClientApplication {
248
215
async acquireTokenRedirect ( request : RedirectRequest ) : Promise < void > {
249
216
try {
250
217
// Preflight request
251
- const validRequest : AuthorizationUrlRequest = this . preflightInteractiveRequest ( request , InteractionType . REDIRECT ) ;
218
+ const validRequest : AuthorizationUrlRequest = this . preflightInteractiveRequest ( request ) ;
252
219
253
220
// Create auth code request and generate PKCE params
254
221
const authCodeRequest : AuthorizationCodeRequest = await this . initializeAuthorizationCodeRequest ( validRequest ) ;
@@ -295,7 +262,7 @@ export class PublicClientApplication implements IPublicClientApplication {
295
262
async acquireTokenPopup ( request : PopupRequest ) : Promise < AuthenticationResult > {
296
263
try {
297
264
// Preflight request
298
- const validRequest : AuthorizationUrlRequest = this . preflightInteractiveRequest ( request , InteractionType . POPUP ) ;
265
+ const validRequest : AuthorizationUrlRequest = this . preflightInteractiveRequest ( request ) ;
299
266
300
267
// Create auth code request and generate PKCE params
301
268
const authCodeRequest : AuthorizationCodeRequest = await this . initializeAuthorizationCodeRequest ( validRequest ) ;
@@ -366,7 +333,7 @@ export class PublicClientApplication implements IPublicClientApplication {
366
333
const silentRequest : AuthorizationUrlRequest = this . initializeAuthorizationRequest ( {
367
334
...request ,
368
335
prompt : PromptValue . NONE
369
- } , InteractionType . SILENT ) ;
336
+ } ) ;
370
337
371
338
// Create auth code request and generate PKCE params
372
339
const authCodeRequest : AuthorizationCodeRequest = await this . initializeAuthorizationCodeRequest ( silentRequest ) ;
@@ -416,7 +383,7 @@ export class PublicClientApplication implements IPublicClientApplication {
416
383
...silentRequest ,
417
384
redirectUri : request . redirectUri ,
418
385
prompt : PromptValue . NONE
419
- } , InteractionType . SILENT ) ;
386
+ } ) ;
420
387
421
388
// Create auth code request and generate PKCE params
422
389
const authCodeRequest : AuthorizationCodeRequest = await this . initializeAuthorizationCodeRequest ( silentAuthUrlRequest ) ;
@@ -599,7 +566,7 @@ export class PublicClientApplication implements IPublicClientApplication {
599
566
/**
600
567
* Helper to validate app environment before making a request.
601
568
*/
602
- private preflightInteractiveRequest ( request : RedirectRequest | PopupRequest , interactionType : InteractionType ) : AuthorizationUrlRequest {
569
+ private preflightInteractiveRequest ( request : RedirectRequest | PopupRequest ) : AuthorizationUrlRequest {
603
570
// block the reload if it occurred inside a hidden iframe
604
571
BrowserUtils . blockReloadInHiddenIframes ( ) ;
605
572
@@ -608,7 +575,7 @@ export class PublicClientApplication implements IPublicClientApplication {
608
575
throw BrowserAuthError . createInteractionInProgressError ( ) ;
609
576
}
610
577
611
- return this . initializeAuthorizationRequest ( request , interactionType ) ;
578
+ return this . initializeAuthorizationRequest ( request ) ;
612
579
}
613
580
614
581
/**
@@ -644,7 +611,7 @@ export class PublicClientApplication implements IPublicClientApplication {
644
611
* Helper to initialize required request parameters for interactive APIs and ssoSilent()
645
612
* @param request
646
613
*/
647
- private initializeAuthorizationRequest ( request : AuthorizationUrlRequest | RedirectRequest | PopupRequest , interactionType : InteractionType ) : AuthorizationUrlRequest {
614
+ private initializeAuthorizationRequest ( request : AuthorizationUrlRequest | RedirectRequest | PopupRequest ) : AuthorizationUrlRequest {
648
615
let validatedRequest : AuthorizationUrlRequest = {
649
616
...request
650
617
} ;
@@ -664,20 +631,15 @@ export class PublicClientApplication implements IPublicClientApplication {
664
631
}
665
632
}
666
633
667
- const browserState : BrowserStateObject = {
668
- interactionType : interactionType
669
- } ;
670
-
671
634
validatedRequest . state = ProtocolUtils . setRequestState (
672
- this . browserCrypto ,
673
635
( request && request . state ) || "" ,
674
- browserState
636
+ this . browserCrypto
675
637
) ;
676
638
677
639
if ( StringUtils . isEmpty ( validatedRequest . nonce ) ) {
678
640
validatedRequest . nonce = this . browserCrypto . createNewGuid ( ) ;
679
641
}
680
-
642
+
681
643
validatedRequest . responseMode = ResponseMode . FRAGMENT ;
682
644
683
645
validatedRequest = {
0 commit comments