Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth with credentials #816

Merged
merged 8 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/auth/src/lib/shared/auth.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -24,16 +25,27 @@ export class AuthInterceptor implements HttpInterceptor {
return trustHosts;
}

private get hostsWithCredentials(): WithCredentialsOptions[] {
return this.config.getConfig('auth.hostsWithCredentials') || [];
}

constructor(
private config: ConfigService,
private tokenService: TokenService,
private http: HttpClient
) {}

intercept(
req: HttpRequest<any>,
originalReq: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
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');
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion packages/auth/src/lib/shared/auth.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface AuthMicrosoftOptions {
}

export interface AuthOptions {
url: string;
url?: string;
tokenKey: string;
allowAnonymous?: boolean;
loginRoute?: string;
Expand All @@ -31,7 +31,12 @@ export interface AuthOptions {
microsoft?: AuthMicrosoftOptions;
trustHosts?: string[];
profilsGuard?: string[];
hostsWithCredentials?: WithCredentialsOptions[];
}
export interface WithCredentialsOptions {
withCredentials?: boolean;
domainRegFilters?: string;
}

export interface User {
source?: string;
Expand Down