File tree Expand file tree Collapse file tree 3 files changed +32
-11
lines changed
projects/angular-auth-oidc-client/src/lib Expand file tree Collapse file tree 3 files changed +32
-11
lines changed Original file line number Diff line number Diff line change @@ -11,13 +11,15 @@ export class ClosestMatchingRouteService {
1111 for ( const config of configurations ) {
1212 const { secureRoutes } = config ;
1313
14- for ( const configuredRoute of secureRoutes ?? [ ] ) {
15- if ( route . startsWith ( configuredRoute ) || wildcardToRegExp ( configuredRoute ) . test ( route ) ) {
16- return {
17- matchingRoute : configuredRoute ,
18- matchingConfig : config ,
19- } ;
20- }
14+ const matchingRoute = ( secureRoutes ?? [ ] ) . find ( ( secureRoute ) =>
15+ this . routeMatches ( secureRoute , route )
16+ ) ;
17+
18+ if ( matchingRoute ) {
19+ return {
20+ matchingRoute,
21+ matchingConfig : config ,
22+ } ;
2123 }
2224 }
2325
@@ -26,6 +28,19 @@ export class ClosestMatchingRouteService {
2628 matchingConfig : null ,
2729 } ;
2830 }
31+
32+ private routeMatches ( configuredRoute : string , route : string ) : boolean {
33+ return (
34+ route . startsWith ( configuredRoute ) ||
35+ this . matchesRoute ( configuredRoute , route )
36+ ) ;
37+ }
38+
39+ private matchesRoute ( configuredRoute : string , route : string ) : boolean {
40+ const regex = wildcardToRegExp ( configuredRoute ) ;
41+
42+ return regex . test ( route ) ;
43+ }
2944}
3045
3146export interface ClosestMatchingRouteResult {
Original file line number Diff line number Diff line change @@ -8,21 +8,25 @@ describe('RegexHelper', () => {
88 'https://third-route.com/test2'
99 )
1010 ) . toBeFalsy ( ) ;
11+
1112 expect (
1213 wildcardToRegExp ( 'https://third-route.com/*/test' ) . test (
1314 'https://third-route.com/test'
1415 )
1516 ) . toBeFalsy ( ) ;
17+
1618 expect (
1719 wildcardToRegExp ( 'https://third-route.com/*/test' ) . test (
1820 'https://third-route.com/foo/test'
1921 )
2022 ) . toBeTruthy ( ) ;
23+
2124 expect (
2225 wildcardToRegExp ( 'https://third-route.com/*/test' ) . test (
2326 'https://third-route.com/foo/test/bar'
2427 )
2528 ) . toBeFalsy ( ) ;
29+
2630 expect (
2731 wildcardToRegExp ( 'https://third-route.com/*/test/*' ) . test (
2832 'https://third-route.com/foo/test/bar'
Original file line number Diff line number Diff line change 22 * Creates a RegExp from the given string, converting asterisks to .* expressions,
33 * and escaping all other characters.
44 */
5- export function wildcardToRegExp ( s : string ) : RegExp {
6- return new RegExp ( '^' + s . split ( / \* + / ) . map ( regExpEscape ) . join ( '.*' ) + '$' ) ;
5+ export function wildcardToRegExp ( value : string ) : RegExp {
6+ const regexPattern = regExpEscape ( value ) ;
7+
8+ return new RegExp ( `^${ regexPattern } $` ) ;
79}
810
911/**
1012 * RegExp-escapes all characters in the given string.
1113 */
12- function regExpEscape ( s : string ) : string {
13- return s . replace ( / [ | \\ { } ( ) [ \] ^ $ + * ? . ] / g, '\\$&' ) ;
14+ function regExpEscape ( value : string ) : string {
15+ return value . replace ( / [ . + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) . replace ( / \* / g , '.* ') ;
1416}
You can’t perform that action at this time.
0 commit comments