Skip to content

Commit 9948e24

Browse files
Refactored a bit
1 parent aead962 commit 9948e24

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

projects/angular-auth-oidc-client/src/lib/interceptor/closest-matching-route.service.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff 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

3146
export interface ClosestMatchingRouteResult {

projects/angular-auth-oidc-client/src/lib/utils/regex/regex.helper.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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'

projects/angular-auth-oidc-client/src/lib/utils/regex/regex.helper.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
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
}

0 commit comments

Comments
 (0)