From 61cc44abbd4315c11117c197657815c9f76085ba Mon Sep 17 00:00:00 2001 From: Pierre-Etienne Lord Date: Fri, 20 Nov 2020 08:16:46 -0500 Subject: [PATCH 1/3] feat(auth-interceptor) allow to pass credentials by domain, based on regex --- .../auth/src/lib/shared/auth.interceptor.ts | 32 ++++++++++++++++++- .../auth/src/lib/shared/auth.interface.ts | 7 ++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/auth/src/lib/shared/auth.interceptor.ts b/packages/auth/src/lib/shared/auth.interceptor.ts index 6fbdf6e935..8171fe92d1 100644 --- a/packages/auth/src/lib/shared/auth.interceptor.ts +++ b/packages/auth/src/lib/shared/auth.interceptor.ts @@ -11,6 +11,7 @@ import { Md5 } from 'ts-md5'; import { ConfigService } from '@igo2/core'; import { TokenService } from './token.service'; +import { WithCredentialsOptions } from './auth.interface'; @Injectable({ providedIn: 'root' @@ -24,6 +25,10 @@ export class AuthInterceptor implements HttpInterceptor { return trustHosts; } + private get hostsWithCredentials(): WithCredentialsOptions[] { + return this.config.getConfig('auth.hostsWithCredentials') || []; + } + constructor( private config: ConfigService, private tokenService: TokenService, @@ -31,9 +36,16 @@ export class AuthInterceptor implements HttpInterceptor { ) {} intercept( - req: HttpRequest, + originalReq: HttpRequest, next: HttpHandler ): Observable> { + const withCredentials = this.handleHostsWithCredentials(originalReq.url); + let req = originalReq.clone(); + if (withCredentials) { + req = originalReq.clone({ + withCredentials + }); + } this.refreshToken(); const token = this.tokenService.get(); const element = document.createElement('a'); @@ -72,6 +84,12 @@ export class AuthInterceptor implements HttpInterceptor { } interceptXhr(xhr, url: string): boolean { + const withCredentials = this.handleHostsWithCredentials(url); + if (withCredentials) { + xhr.withCredentials = withCredentials; + return true; + } + this.refreshToken(); const element = document.createElement('a'); element.href = url; @@ -84,6 +102,18 @@ export class AuthInterceptor implements HttpInterceptor { return true; } + private handleHostsWithCredentials(reqUrl: string) { + let withCredentials = false; + for (const hostWithCredentials of this.hostsWithCredentials) { + const domainRegex = new RegExp(hostWithCredentials.domainRegFilters); + if (domainRegex.test(reqUrl)) { + withCredentials = hostWithCredentials.withCredentials !== undefined ? hostWithCredentials.withCredentials : undefined; + break; + } + } + return withCredentials; + } + refreshToken() { const jwt = this.tokenService.decode(); const currentTime = new Date().getTime() / 1000; diff --git a/packages/auth/src/lib/shared/auth.interface.ts b/packages/auth/src/lib/shared/auth.interface.ts index 471905a135..13ca51b306 100644 --- a/packages/auth/src/lib/shared/auth.interface.ts +++ b/packages/auth/src/lib/shared/auth.interface.ts @@ -31,7 +31,14 @@ export interface AuthOptions { microsoft?: AuthMicrosoftOptions; trustHosts?: string[]; profilsGuard?: string[]; + hostsWithCredentials?: WithCredentialsOptions[]; + autoLoginAnonymous?: boolean; + loginButton?: boolean; } +export interface WithCredentialsOptions { + withCredentials?: boolean; + domainRegFilters?: string; + } export interface User { source?: string; From 62e5c42c011e23d165786e83fe2929e83e5fa822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-=C3=89tienne=20Lord?= Date: Wed, 7 Apr 2021 20:35:52 -0400 Subject: [PATCH 2/3] wip --- packages/auth/src/lib/shared/auth.interface.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/auth/src/lib/shared/auth.interface.ts b/packages/auth/src/lib/shared/auth.interface.ts index 13ca51b306..b77ebeae2b 100644 --- a/packages/auth/src/lib/shared/auth.interface.ts +++ b/packages/auth/src/lib/shared/auth.interface.ts @@ -19,7 +19,7 @@ export interface AuthMicrosoftOptions { } export interface AuthOptions { - url: string; + url?: string; tokenKey: string; allowAnonymous?: boolean; loginRoute?: string; @@ -32,7 +32,6 @@ export interface AuthOptions { trustHosts?: string[]; profilsGuard?: string[]; hostsWithCredentials?: WithCredentialsOptions[]; - autoLoginAnonymous?: boolean; loginButton?: boolean; } export interface WithCredentialsOptions { From fec61c24e2671be905a5ebaa1722a35276967fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-=C3=89tienne=20Lord?= Date: Wed, 7 Apr 2021 20:49:59 -0400 Subject: [PATCH 3/3] wip --- packages/auth/src/lib/shared/auth.interface.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/auth/src/lib/shared/auth.interface.ts b/packages/auth/src/lib/shared/auth.interface.ts index b77ebeae2b..34604935ef 100644 --- a/packages/auth/src/lib/shared/auth.interface.ts +++ b/packages/auth/src/lib/shared/auth.interface.ts @@ -32,7 +32,6 @@ export interface AuthOptions { trustHosts?: string[]; profilsGuard?: string[]; hostsWithCredentials?: WithCredentialsOptions[]; - loginButton?: boolean; } export interface WithCredentialsOptions { withCredentials?: boolean;