diff --git a/libs/angular-auth/src/lib/auth-service-wrapper.ts b/libs/angular-auth/src/lib/auth-service-wrapper.ts index 3a3c4966..9de6451f 100644 --- a/libs/angular-auth/src/lib/auth-service-wrapper.ts +++ b/libs/angular-auth/src/lib/auth-service-wrapper.ts @@ -39,6 +39,9 @@ export class AuthServiceWrapper { getHeaderValues(): Record { return this.authService?.getHeaderValues() ?? {} } + updateTokenIfNeeded(): Promise { + return this.authService?.updateTokenIfNeeded() ?? Promise.reject() + } async initializeAuthService(): Promise { const serviceTypeConfig = this.configService.getProperty(CONFIG_KEY.AUTH_SERVICE) ?? 'keycloak' diff --git a/libs/angular-auth/src/lib/auth.service.ts b/libs/angular-auth/src/lib/auth.service.ts index 27413c09..718c4fcf 100644 --- a/libs/angular-auth/src/lib/auth.service.ts +++ b/libs/angular-auth/src/lib/auth.service.ts @@ -4,6 +4,8 @@ export interface AuthService { getHeaderValues(): Record logout(): void + + updateTokenIfNeeded(): Promise } export enum Injectables { diff --git a/libs/angular-auth/src/lib/auth_services/keycloak-auth.service.ts b/libs/angular-auth/src/lib/auth_services/keycloak-auth.service.ts index 1070fd87..3c18afb5 100644 --- a/libs/angular-auth/src/lib/auth_services/keycloak-auth.service.ts +++ b/libs/angular-auth/src/lib/auth_services/keycloak-auth.service.ts @@ -143,6 +143,14 @@ export class KeycloakAuthService implements AuthService { this.keycloakService.logout() } + async updateTokenIfNeeded(): Promise { + if (!(await this.keycloakService.isLoggedIn())) { + return this.keycloakService.login().then(() => false) + } else { + return this.keycloakService.updateToken() + } + } + getAuthProviderName(): string { return 'keycloak-auth' } diff --git a/libs/angular-auth/src/lib/token.interceptor.ts b/libs/angular-auth/src/lib/token.interceptor.ts index 500786e7..557f27ee 100644 --- a/libs/angular-auth/src/lib/token.interceptor.ts +++ b/libs/angular-auth/src/lib/token.interceptor.ts @@ -1,6 +1,6 @@ import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http' import { Injectable } from '@angular/core' -import { Observable } from 'rxjs' +import { Observable, from, mergeMap } from 'rxjs' import { AuthServiceWrapper } from './auth-service-wrapper' const WHITELIST = ['assets'] @@ -14,14 +14,19 @@ export class TokenInterceptor implements HttpInterceptor { if (skip) { return next.handle(request) } - const headerValues = this.authService.getHeaderValues() - let headers = request.headers - for (const header in headerValues) { - headers = headers.set(header, headerValues[header]) - } - const authenticatedReq: HttpRequest = request.clone({ - headers: headers, - }) - return next.handle(authenticatedReq) + + return from(this.authService.updateTokenIfNeeded()).pipe( + mergeMap(() => { + const headerValues = this.authService.getHeaderValues() + let headers = request.headers + for (const header in headerValues) { + headers = headers.set(header, headerValues[header]) + } + const authenticatedReq: HttpRequest = request.clone({ + headers: headers, + }) + return next.handle(authenticatedReq) + }) + ) } }