I'm facing an issue with validating the AccessToken received from Microsoft in my Angular 16 frontend application using MSAL 3 for authentication. Although I can successfully obtain the tokens, I'm encountering problems when it comes to validating the AccessToken. Strangely, I can validate the idToken without any issues, but the AccessToken appears to be invalid, even when checked on jwt.io.
To address this problem, I have implemented a custom HTTP interceptor that utilizes msal.instance.silentAcquire(...) to retrieve the idToken and create a new HTTP header with the token. While this approach works, it doesn't feel clean and results in messy code.
I would appreciate any insights or explanations as to why I'm unable to validate the AccessToken.
Thank you in advance for your help!
For a little bit more unterstanding: Angular Frontend gets Token via MSAL, calls Spring Boot API with Token, Backend validate Token and give response if token is valid.
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpErrorResponse,
HttpStatusCode,
} from '@angular/common/http';
import { Observable, catchError, from, mergeMap, throwError } from 'rxjs';
import { MsalService } from '@azure/msal-angular';
import { SilentRequest } from '@azure/msal-browser';
@Injectable()
export class HttpInterceptor implements HttpInterceptor {
constructor(private authService: MsalService) { }
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
if (request.url.includes('localhost:8080')) {
const headerConf = {
Authorization: '',
'Content-Type': 'application/json',
Accept: 'application/json',
};
const silentRequest: SilentRequest = {
scopes: ['user.read'],
};
return from(this.authService.instance.acquireTokenSilent(silentRequest)).pipe(
mergeMap((data) => {
const idToken = data.idToken;
headerConf.Authorization = `Bearer ${idToken}`;
const req = request.clone({ setHeaders: headerConf });
return next.handle(req);
}),
catchError((err) => {
const error = err as HttpErrorResponse;
if (error.status == HttpStatusCode.Forbidden) {
console.error("Fehler beim request", error);
}
return throwError(() => new Error(err));
})
);
} else {
return next.handle(request);
}
}
}
Validate AccessToken on jwt.io, created an api expose in azure, tried several key and issuer configs in spring boot