feat(portal-bff): msal confidential client provider in AuthModule (#104)
Second step of ADR-0009 wiring. AuthModule now exposes the `@azure/msal-node` confidential client alongside the parsed Entra config — the building block the upcoming OIDC routes inject to issue the auth-code URL, exchange the callback code for tokens, and acquire downstream tokens on behalf of the user. What lands: - `@azure/msal-node` added as a direct dependency (^5.2.1). - `apps/portal-bff/src/auth/msal-client.token.ts` — `MSAL_CLIENT` string token + `ConfidentialClientApplication` type re-export. Mirrors the `ENTRA_CONFIG` token shape from PR #102. - AuthModule grows a factory provider for `MSAL_CLIENT`: - Injects `ENTRA_CONFIG` + nestjs-pino `Logger`. - Builds a `ConfidentialClientApplication` with `clientId`, `authority`, `clientSecret` from the parsed config. - Wires `system.loggerOptions.loggerCallback` to forward MSAL's internal log lines into the Pino stream (per ADR-0012) — Error → logger.error, Warning → logger.warn, Verbose / Trace → logger.debug, Info → logger.log. PII logging is disabled by default so tokens / user identifiers never leak into our structured log records. - Sets MSAL's `logLevel` to Info — Pino's own threshold re-filters from there. - All MSAL log lines carry the `msal` Pino context for easy isolation in log queries. - `AuthModule.exports` extended to include `MSAL_CLIENT`. Verification: - `nx run-many -t lint test build --projects=portal-bff` — green. - 30/30 specs (was 29; +1 covering MSAL client construction). - New spec imports `nestjs-pino`'s `LoggerModule.forRoot({ pinoHttp: { level: 'silent' } })` to provide the same Logger the production app supplies via `ObservabilityModule`, without flooding test stdout. Two tests assert the provider tree resolves correctly (ENTRA_CONFIG + MSAL_CLIENT) and one re-checks the missing-env failure mode still propagates through the new factory. Construction is cheap — MSAL Node defers authority discovery to the first auth call — so the client is built eagerly at module init. The factory is injection-only; no MSAL methods get invoked yet. Routes land in the next PR. <!-- PR title format — becomes the squash-merge subject on main, validated by commitlint. <type>(<scope>): <short description> Examples: feat(portal-shell): add user-preferences panel skeleton fix(portal-bff): correct env var bracket access docs(decisions): add ADR-0018 for security baseline chore(deps): bump @nx/* to 22.7.2 Imperative mood, lowercase, no trailing period, target ≤ 70 chars. See docs/development.md §5 for the full convention (types, scopes). --> ## Summary ## Motivation ## Implementation notes ## Verification - [ ] `pnpm ci:check` green locally - [ ] `pnpm ci:audit` green (or pre-existing drift acknowledged) - [ ] Tested manually: - [ ] Architecture diagram updated (if `docs/architecture.md` was affected) - [ ] ADR amended or added (if a decision changed) ## Related --------- Co-authored-by: Julien Gautier <julien.gautier@apf.asso.fr> Reviewed-on: #104
This commit was merged in pull request #104.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { ConfidentialClientApplication } from '@azure/msal-node';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { LoggerModule } from 'nestjs-pino';
|
||||
import { AuthModule } from './auth.module';
|
||||
import { ENTRA_CONFIG, type EntraConfig } from './entra-config.token';
|
||||
import { MSAL_CLIENT } from './msal-client.token';
|
||||
|
||||
const VALID = {
|
||||
ENTRA_INSTANCE_URL: 'https://login.microsoftonline.com/',
|
||||
@@ -11,6 +14,17 @@ const VALID = {
|
||||
ENTRA_POST_LOGOUT_REDIRECT_URI: 'http://localhost:4200/',
|
||||
};
|
||||
|
||||
// AuthModule's MSAL_CLIENT factory injects the Pino-backed `Logger`
|
||||
// (which the production app supplies through `ObservabilityModule`).
|
||||
// Importing `LoggerModule.forRoot({ pinoHttp: { level: 'silent' } })`
|
||||
// here registers the same provider for the testing module without
|
||||
// flooding test stdout.
|
||||
async function compile() {
|
||||
return Test.createTestingModule({
|
||||
imports: [LoggerModule.forRoot({ pinoHttp: { level: 'silent' } }), AuthModule],
|
||||
}).compile();
|
||||
}
|
||||
|
||||
describe('AuthModule', () => {
|
||||
const originalEnv: Record<string, string | undefined> = {};
|
||||
|
||||
@@ -33,16 +47,20 @@ describe('AuthModule', () => {
|
||||
});
|
||||
|
||||
it('provides EntraConfig via the ENTRA_CONFIG token', async () => {
|
||||
const ref = await Test.createTestingModule({ imports: [AuthModule] }).compile();
|
||||
const ref = await compile();
|
||||
const config = ref.get<EntraConfig>(ENTRA_CONFIG);
|
||||
expect(config.clientId).toBe(VALID.ENTRA_CLIENT_ID);
|
||||
expect(config.authority).toBe(`${VALID.ENTRA_INSTANCE_URL}${VALID.ENTRA_TENANT_ID}`);
|
||||
});
|
||||
|
||||
it('provides a ConfidentialClientApplication via the MSAL_CLIENT token', async () => {
|
||||
const ref = await compile();
|
||||
const client = ref.get<ConfidentialClientApplication>(MSAL_CLIENT);
|
||||
expect(client).toBeInstanceOf(ConfidentialClientApplication);
|
||||
});
|
||||
|
||||
it('fails to compile when an env var is missing', async () => {
|
||||
delete process.env['ENTRA_CLIENT_SECRET'];
|
||||
await expect(Test.createTestingModule({ imports: [AuthModule] }).compile()).rejects.toThrow(
|
||||
/ENTRA_CLIENT_SECRET/,
|
||||
);
|
||||
await expect(compile()).rejects.toThrow(/ENTRA_CLIENT_SECRET/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user