fix(portal-bff): drop strict amr check — flow blocked when Entra omits the claim #108

Merged
julien merged 1 commits from fix/portal-bff/auth-relax-amr-check into main 2026-05-12 15:31:57 +02:00
3 changed files with 25 additions and 13 deletions
-1
View File
@@ -9,7 +9,6 @@
export type AuthCodeFlowError =
| { kind: 'state-mismatch' }
| { kind: 'flow-expired' }
| { kind: 'amr-missing' }
| { kind: 'token-exchange-failed'; cause: string };
export class AuthCodeFlowException extends Error {
+11 -4
View File
@@ -129,12 +129,19 @@ describe('AuthService.completeAuthCodeFlow', () => {
).rejects.toMatchObject({ failure: { kind: 'flow-expired' } });
});
it('throws amr-missing when the ID token has no amr claim', async () => {
it('returns the user even when the ID token has no `amr` claim (Entra optional)', async () => {
const acquireTokenByCode = jest.fn().mockResolvedValue(makeAuthResult({ amr: [] }));
const { service } = makeService({ acquireTokenByCode });
await expect(
service.completeAuthCodeFlow('code', PRE_AUTH_OK.state, PRE_AUTH_OK, PRE_AUTH_OK.createdAt),
).rejects.toMatchObject({ failure: { kind: 'amr-missing' } });
const user = await service.completeAuthCodeFlow(
'code',
PRE_AUTH_OK.state,
PRE_AUTH_OK,
PRE_AUTH_OK.createdAt,
);
// `amr` flows through as an empty array; MFA enforcement is
// Conditional Access's job per ADR-0011, not this code path.
expect(user.amr).toEqual([]);
expect(user.oid).toBe('user-oid');
});
it('throws token-exchange-failed when MSAL throws', async () => {
+14 -8
View File
@@ -150,18 +150,24 @@ export class AuthService {
private toAuthenticatedUser(result: AuthenticationResult): AuthenticatedUser {
const claims = result.idTokenClaims as Record<string, unknown>;
// `amr` is an optional claim. Entra includes it when it has
// explicit auth-method tracking to report (fresh interactive
// sign-in with MFA, etc.) and may omit it for SSO / refresh
// flows or when no Conditional Access policy requires the
// method to be surfaced. The BFF surfaces whatever is there in
// the audit log / future `@RequireMfa` guard — but it does NOT
// reject the token on empty / missing `amr`. Per ADR-0011, MFA
// enforcement is org-side via Conditional Access, and the
// BFF's "sanity check" is that MSAL accepted the token at all
// (signature + issuer + audience + exp validated). Sensitive
// routes that need a fresh MFA event will assert it explicitly
// through the `@RequireMfa({ freshness: 600 })` decorator once
// it lands.
const amr = Array.isArray(claims['amr'])
? (claims['amr'] as unknown[]).filter((v): v is string => typeof v === 'string')
: [];
// ADR-0011 sanity-check: Conditional Access on the org side is
// the actual enforcement layer; the BFF refuses tokens that
// lack any `amr` evidence (an empty array would indicate a
// policy misconfiguration where MFA never fired).
if (amr.length === 0) {
throw new AuthCodeFlowException({ kind: 'amr-missing' });
}
return {
oid: requireString(claims['oid'], 'oid'),
tid: requireString(claims['tid'], 'tid'),