@@ -18,7 +18,6 @@ import {
18
18
InteractionStatus ,
19
19
InteractionType ,
20
20
StringUtils ,
21
- UrlString ,
22
21
} from "@azure/msal-browser" ;
23
22
import { Observable , EMPTY , of } from "rxjs" ;
24
23
import { switchMap , catchError , take , filter } from "rxjs/operators" ;
@@ -27,7 +26,6 @@ import {
27
26
MsalInterceptorAuthRequest ,
28
27
MsalInterceptorConfiguration ,
29
28
ProtectedResourceScopes ,
30
- MatchingResources ,
31
29
} from "./msal.interceptor.config" ;
32
30
import { MsalBroadcastService } from "./msal.broadcast.service" ;
33
31
import { MSAL_INTERCEPTOR_CONFIG } from "./constants" ;
@@ -239,17 +237,10 @@ export class MsalInterceptor implements HttpInterceptor {
239
237
normalizedEndpoint
240
238
) ;
241
239
242
- // Check absolute urls of resources first before checking relative to prevent incorrect matching where multiple resources have similar relative urls
243
- if ( matchingProtectedResources . absoluteResources . length > 0 ) {
240
+ if ( matchingProtectedResources . length > 0 ) {
244
241
return this . matchScopesToEndpoint (
245
242
this . msalInterceptorConfig . protectedResourceMap ,
246
- matchingProtectedResources . absoluteResources ,
247
- httpMethod
248
- ) ;
249
- } else if ( matchingProtectedResources . relativeResources . length > 0 ) {
250
- return this . matchScopesToEndpoint (
251
- this . msalInterceptorConfig . protectedResourceMap ,
252
- matchingProtectedResources . relativeResources ,
243
+ matchingProtectedResources ,
253
244
httpMethod
254
245
) ;
255
246
}
@@ -266,46 +257,53 @@ export class MsalInterceptor implements HttpInterceptor {
266
257
private matchResourcesToEndpoint (
267
258
protectedResourcesEndpoints : string [ ] ,
268
259
endpoint : string
269
- ) : MatchingResources {
270
- const matchingResources : MatchingResources = {
271
- absoluteResources : [ ] ,
272
- relativeResources : [ ] ,
273
- } ;
260
+ ) : Array < string > {
261
+ const matchingResources : Array < string > = [ ] ;
274
262
275
263
protectedResourcesEndpoints . forEach ( ( key ) => {
276
- // Normalizes and adds resource to matchingResources.absoluteResources if key matches endpoint. StringUtils.matchPattern accounts for wildcards
277
264
const normalizedKey = this . location . normalize ( key ) ;
278
- if ( StringUtils . matchPattern ( normalizedKey , endpoint ) ) {
279
- matchingResources . absoluteResources . push ( key ) ;
280
- }
281
265
282
- // Get url components for relative urls
283
- const absoluteKey = this . getAbsoluteUrl ( key ) ;
284
- const keyComponents = new UrlString ( absoluteKey ) . getUrlComponents ( ) ;
266
+ // Get url components
267
+ const absoluteKey = this . getAbsoluteUrl ( normalizedKey ) ;
268
+ const keyComponents = new URL ( absoluteKey ) ;
285
269
const absoluteEndpoint = this . getAbsoluteUrl ( endpoint ) ;
286
- const endpointComponents = new UrlString (
287
- absoluteEndpoint
288
- ) . getUrlComponents ( ) ;
289
-
290
- // Normalized key should include query strings if applicable
291
- const relativeNormalizedKey = keyComponents . QueryString
292
- ? `${ keyComponents . AbsolutePath } ?${ keyComponents . QueryString } `
293
- : this . location . normalize ( keyComponents . AbsolutePath ) ;
294
-
295
- // Add resource to matchingResources.relativeResources if same origin, relativeKey matches endpoint, and is not empty
296
- if (
297
- keyComponents . HostNameAndPort === endpointComponents . HostNameAndPort &&
298
- StringUtils . matchPattern ( relativeNormalizedKey , absoluteEndpoint ) &&
299
- relativeNormalizedKey !== "" &&
300
- relativeNormalizedKey !== "/*"
301
- ) {
302
- matchingResources . relativeResources . push ( key ) ;
270
+ const endpointComponents = new URL ( absoluteEndpoint ) ;
271
+
272
+ if ( this . checkUrlComponents ( keyComponents , endpointComponents ) ) {
273
+ matchingResources . push ( key ) ;
303
274
}
304
275
} ) ;
305
276
306
277
return matchingResources ;
307
278
}
308
279
280
+ /**
281
+ * Compares URL segments between key and endpoint
282
+ * @param key
283
+ * @param endpoint
284
+ * @returns
285
+ */
286
+ private checkUrlComponents (
287
+ keyComponents : URL ,
288
+ endpointComponents : URL
289
+ ) : boolean {
290
+ // URL properties from https://developer.mozilla.org/en-US/docs/Web/API/URL
291
+ const urlProperties = [ "protocol" , "host" , "pathname" , "search" , "hash" ] ;
292
+
293
+ for ( const property of urlProperties ) {
294
+ if ( keyComponents [ property ] ) {
295
+ const decodedInput = decodeURIComponent ( keyComponents [ property ] ) ;
296
+ if (
297
+ ! StringUtils . matchPattern ( decodedInput , endpointComponents [ property ] )
298
+ ) {
299
+ return false ;
300
+ }
301
+ }
302
+ }
303
+
304
+ return true ;
305
+ }
306
+
309
307
/**
310
308
* Transforms relative urls to absolute urls
311
309
* @param url
0 commit comments