|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { OAuthService } from 'angular-oauth2-oidc'; |
| 3 | +import { Observable } from 'rxjs'; |
| 4 | +import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http'; |
| 5 | + |
| 6 | +/** |
| 7 | + * HTTP interceptor that automatically adds OAuth bearer tokens to outgoing requests. |
| 8 | + * Excludes certain URLs from token injection (e.g., static assets) and validates |
| 9 | + * token availability before attaching authorization headers. |
| 10 | + */ |
| 11 | +@Injectable() |
| 12 | +export class AuthTokenInterceptor implements HttpInterceptor { |
| 13 | + /** |
| 14 | + * List of URL patterns that should be excluded from token injection. |
| 15 | + */ |
| 16 | + private readonly excludedUrls = ['assets', '/assets']; |
| 17 | + |
| 18 | + /** |
| 19 | + * Pre-compiled regular expressions for URL matching. |
| 20 | + */ |
| 21 | + private readonly excludedUrlsRegEx = this.excludedUrls.map((url) => new RegExp('^' + url, 'i')); |
| 22 | + |
| 23 | + constructor(private oauthService: OAuthService) {} |
| 24 | + |
| 25 | + /** |
| 26 | + * Intercepts HTTP requests and conditionally adds Authorization header with bearer token. |
| 27 | + * @param req - The outgoing HTTP request to potentially modify |
| 28 | + * @param next - The next handler in the interceptor chain |
| 29 | + * @returns Observable of HTTP events from the modified or original request |
| 30 | + */ |
| 31 | + public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
| 32 | + const shouldExclude = this.excludedUrlsRegEx.some((regex) => regex.test(req.url)); |
| 33 | + if (shouldExclude) { |
| 34 | + return next.handle(req); |
| 35 | + } |
| 36 | + |
| 37 | + const modifiedRequest = this.attachTokenIfAvailable(req); |
| 38 | + return next.handle(modifiedRequest); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a cloned request with Authorization header when token exists. |
| 43 | + * @param req - The original HTTP request |
| 44 | + * @returns Modified request with token or original request if no token available |
| 45 | + */ |
| 46 | + private attachTokenIfAvailable(req: HttpRequest<any>): HttpRequest<any> { |
| 47 | + const token = this.getValidToken(); |
| 48 | + if (!token) { |
| 49 | + console.warn('No valid token found, request will no have Authorization header'); |
| 50 | + return req; |
| 51 | + } |
| 52 | + const header = `Bearer ${token}`; |
| 53 | + return req.clone({ |
| 54 | + setHeaders: { Authorization: header }, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Retrieves a valid, non-empty access token from the OAuth service. |
| 60 | + * @returns Access token string if valid, otherwise null |
| 61 | + */ |
| 62 | + private getValidToken(): string | null { |
| 63 | + const token = this.oauthService.getAccessToken(); |
| 64 | + |
| 65 | + if (token && typeof token === 'string' && this.oauthService.hasValidAccessToken()) { |
| 66 | + return token; |
| 67 | + } |
| 68 | + return null; |
| 69 | + } |
| 70 | +} |
0 commit comments