initial release
This commit is contained in:
77
bizmatch/src/app/interceptors/keycloak-bearer.interceptor.ts
Normal file
77
bizmatch/src/app/interceptors/keycloak-bearer.interceptor.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import {
|
||||
HttpInterceptor,
|
||||
HttpRequest,
|
||||
HttpHandler,
|
||||
HttpEvent,
|
||||
HttpInterceptorFn,
|
||||
HttpHandlerFn,
|
||||
} from '@angular/common/http';
|
||||
|
||||
import { Observable, combineLatest, from, of } from 'rxjs';
|
||||
import { mergeMap } from 'rxjs/operators';
|
||||
|
||||
import { KeycloakService } from '../services/keycloak.service';
|
||||
import { ExcludedUrlRegex } from '../models/keycloak-options';
|
||||
|
||||
export const keycloakBearerInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
//return next(req);
|
||||
const keycloak = inject(KeycloakService);
|
||||
const { enableBearerInterceptor, excludedUrls } = keycloak;
|
||||
if (!enableBearerInterceptor) {
|
||||
return next(req);
|
||||
}
|
||||
|
||||
const shallPass: boolean =
|
||||
!keycloak.shouldAddToken(req) ||
|
||||
excludedUrls.findIndex((item) => isUrlExcluded(req, item)) > -1;
|
||||
if (shallPass) {
|
||||
return next(req);
|
||||
}
|
||||
|
||||
return combineLatest([
|
||||
from(conditionallyUpdateToken(req)),
|
||||
of(keycloak.isLoggedIn()),
|
||||
]).pipe(
|
||||
mergeMap(([_, isLoggedIn]) =>
|
||||
isLoggedIn ? handleRequestWithTokenHeader(req, next) : next(req)
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
function isUrlExcluded(
|
||||
{ method, url }: HttpRequest<unknown>,
|
||||
{ urlPattern, httpMethods }: ExcludedUrlRegex
|
||||
): boolean {
|
||||
const httpTest =
|
||||
httpMethods.length === 0 ||
|
||||
httpMethods.join().indexOf(method.toUpperCase()) > -1;
|
||||
|
||||
const urlTest = urlPattern.test(url);
|
||||
|
||||
return httpTest && urlTest;
|
||||
}
|
||||
|
||||
function handleRequestWithTokenHeader(
|
||||
req: HttpRequest<unknown>,
|
||||
next: HttpHandlerFn
|
||||
): Observable<HttpEvent<unknown>> {
|
||||
return this.keycloak.addTokenToHeader(req.headers).pipe(
|
||||
mergeMap((headersWithBearer:string) => {
|
||||
const kcReq = req.clone({
|
||||
headers: req.headers.set('Authorization', headersWithBearer)
|
||||
});//req.clone({ headers: headersWithBearer });
|
||||
return next(kcReq);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async function conditionallyUpdateToken(
|
||||
req: HttpRequest<unknown>
|
||||
): Promise<boolean> {
|
||||
if (this.keycloak.shouldUpdateToken(req)) {
|
||||
return await this.keycloak.updateToken();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
21
bizmatch/src/app/interceptors/loading.interceptor.ts
Normal file
21
bizmatch/src/app/interceptors/loading.interceptor.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { HttpInterceptorFn } from '@angular/common/http';
|
||||
import { inject } from '@angular/core';
|
||||
import { tap } from 'rxjs';
|
||||
import { v4 } from 'uuid';
|
||||
import { LoadingService } from '../services/loading.service';
|
||||
|
||||
export const loadingInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
const loadingService = inject(LoadingService);
|
||||
|
||||
const requestId = `HTTP-${v4()}`;
|
||||
|
||||
loadingService.startLoading(requestId);
|
||||
|
||||
return next(req).pipe(
|
||||
tap({
|
||||
finalize: () => loadingService.stopLoading(requestId),
|
||||
error: () => loadingService.stopLoading(requestId),
|
||||
complete: () => loadingService.stopLoading(requestId),
|
||||
})
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user