diff --git a/src/scenarios/authorization-server/authorization-code-grant.test.ts b/src/scenarios/authorization-server/authorization-code-grant.test.ts index e2eaf06a..5c1273d2 100644 --- a/src/scenarios/authorization-server/authorization-code-grant.test.ts +++ b/src/scenarios/authorization-server/authorization-code-grant.test.ts @@ -410,6 +410,112 @@ describe('AuthorizationCodeGrantScenario', () => { expect(withoutResource[0].status).toBe('SUCCESS'); }); + it('authenticates with client_secret_post when the AS also offers none and a secret is configured', async () => { + const scenario = new AuthorizationCodeGrantScenario(); + + mockCallbackServer( + scenario, + (state) => `http://127.0.0.1:3000/callback?code=abc&state=${state}` + ); + + const mixedMetadata = { + ...METADATA, + token_endpoint_auth_methods_supported: ['none', 'client_secret_post'] + }; + const details = { + 'authorization-server-metadata-endpoint': { body: mixedMetadata } + }; + + mockTokenResponse({ + access_token: 'access-token', + token_type: 'Bearer' + }); + + const checks = await scenario.run(OPTIONS, details); + + expect(checks[0].status).toBe('SUCCESS'); + const tokenCall = mockedRequest.mock.calls[0]; + const params = new URLSearchParams((tokenCall[1] as any).body); + expect(params.get('client_id')).toBe('client'); + expect(params.get('client_secret')).toBe('secret'); + expect((tokenCall[1] as any).headers.authorization).toBeUndefined(); + }); + + it('authenticates with client_secret_basic when the AS also offers none and a secret is configured', async () => { + const scenario = new AuthorizationCodeGrantScenario(); + + mockCallbackServer( + scenario, + (state) => `http://127.0.0.1:3000/callback?code=abc&state=${state}` + ); + + const mixedMetadata = { + ...METADATA, + token_endpoint_auth_methods_supported: ['none', 'client_secret_basic'] + }; + const details = { + 'authorization-server-metadata-endpoint': { body: mixedMetadata } + }; + + mockTokenResponse({ + access_token: 'access-token', + token_type: 'Bearer' + }); + + const checks = await scenario.run(OPTIONS, details); + + expect(checks[0].status).toBe('SUCCESS'); + const tokenCall = mockedRequest.mock.calls[0]; + const params = new URLSearchParams((tokenCall[1] as any).body); + expect(params.get('client_secret')).toBeNull(); + const expected = Buffer.from( + `${encodeURIComponent('client')}:${encodeURIComponent('secret')}` + ).toString('base64'); + expect((tokenCall[1] as any).headers.authorization).toBe( + `Basic ${expected}` + ); + }); + + it('still uses none when no client secret is configured even if the AS offers secret methods', async () => { + const scenario = new AuthorizationCodeGrantScenario(); + + mockCallbackServer( + scenario, + (state) => `http://127.0.0.1:3000/callback?code=abc&state=${state}` + ); + + const mixedMetadata = { + ...METADATA, + token_endpoint_auth_methods_supported: [ + 'none', + 'client_secret_post', + 'client_secret_basic' + ] + }; + const details = { + 'authorization-server-metadata-endpoint': { body: mixedMetadata } + }; + + mockTokenResponse({ + access_token: 'access-token', + token_type: 'Bearer' + }); + + const publicOptions = { + url: OPTIONS.url, + clientId: OPTIONS.clientId, + port: OPTIONS.port + }; + const checks = await scenario.run(publicOptions, details); + + expect(checks[0].status).toBe('SUCCESS'); + const tokenCall = mockedRequest.mock.calls[0]; + const params = new URLSearchParams((tokenCall[1] as any).body); + expect(params.get('client_id')).toBe('client'); + expect(params.get('client_secret')).toBeNull(); + expect((tokenCall[1] as any).headers.authorization).toBeUndefined(); + }); + it('returns SKIPPED when client_secret_post and client_secret_basic are missing', async () => { const scenario = new AuthorizationCodeGrantScenario(); diff --git a/src/scenarios/authorization-server/authorization-code-grant.ts b/src/scenarios/authorization-server/authorization-code-grant.ts index 13f3ba04..8e92ed71 100644 --- a/src/scenarios/authorization-server/authorization-code-grant.ts +++ b/src/scenarios/authorization-server/authorization-code-grant.ts @@ -231,15 +231,19 @@ export class AuthorizationCodeGrantScenario implements ClientScenarioForAuthoriz const authMethods: string[] = metadata.token_endpoint_auth_methods_supported ?? ['client_secret_basic']; + // When a secret is configured, prefer a confidential-client method over + // `none` so servers supporting both are exercised on the confidential path. + if (options.clientSecret) { + if (authMethods.includes('client_secret_post')) { + return 'client_secret_post'; + } + if (authMethods.includes('client_secret_basic')) { + return 'client_secret_basic'; + } + } if (!options.clientSecret || authMethods.includes('none')) { return 'none'; } - if (authMethods.includes('client_secret_post')) { - return 'client_secret_post'; - } - if (authMethods.includes('client_secret_basic')) { - return 'client_secret_basic'; - } // client_secret_jwt / private_key_jwt / tls_client_auth are not yet // implemented; skip rather than fail. return null; diff --git a/src/scenarios/authorization-server/dpop-token-auth.test.ts b/src/scenarios/authorization-server/dpop-token-auth.test.ts new file mode 100644 index 00000000..9e7a7a35 --- /dev/null +++ b/src/scenarios/authorization-server/dpop-token-auth.test.ts @@ -0,0 +1,133 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { DPoPAuthorizationServerScenario } from './dpop'; +import { request } from 'undici'; + +vi.mock('undici', () => ({ + request: vi.fn() +})); + +vi.mock('./auth/helpers/createCallbackServer', () => ({ + startCallbackServer: vi.fn() +})); + +const mockedRequest = vi.mocked(request); + +const SERVER_URL = 'https://as.example.com'; +const AUTHORIZATION_ENDPOINT = `${SERVER_URL}/authorize`; +const TOKEN_ENDPOINT = `${SERVER_URL}/token`; + +// The scenario builds its redirect_uri from a fixed origin + options.port. +const PORT = 3000; +const REDIRECT_URI = `http://127.0.0.1:${PORT}/callback`; + +function metadataWith(authMethods: string[]) { + return { + issuer: SERVER_URL, + authorization_endpoint: AUTHORIZATION_ENDPOINT, + token_endpoint: TOKEN_ENDPOINT, + dpop_signing_alg_values_supported: ['ES256'], + token_endpoint_auth_methods_supported: authMethods + }; +} + +interface CapturedTokenRequest { + headers: Record; + body: string; +} + +function jsonResponse(body: Record) { + return { + statusCode: 200, + headers: { 'content-type': 'application/json' }, + body: { + json: async () => body, + text: async () => JSON.stringify(body) + } + } as any; +} + +/** + * Routes the mocked undici requests the way a compliant auto-approving DPoP AS + * would: metadata at the well-known URL, a direct 302 to the redirect_uri at + * the authorization endpoint (the headless path — no callback server needed), + * and a capture-and-answer token endpoint. + */ +function mockAs(authMethods: string[], captured: CapturedTokenRequest[]) { + mockedRequest.mockImplementation(async (url: any, opts: any) => { + const u = url.toString(); + if (u.includes('/.well-known/')) { + return jsonResponse(metadataWith(authMethods)); + } + if (u.startsWith(AUTHORIZATION_ENDPOINT)) { + const state = new URL(u).searchParams.get('state'); + return { + statusCode: 302, + headers: { + location: `${REDIRECT_URI}?code=test-code&state=${state}` + }, + body: { text: async () => '', json: async () => ({}) } + } as any; + } + if (u.startsWith(TOKEN_ENDPOINT)) { + captured.push({ headers: opts.headers, body: opts.body.toString() }); + return jsonResponse({ + access_token: 'test-token', + token_type: 'DPoP', + expires_in: 3600 + }); + } + throw new Error(`unexpected request to ${u}`); + }); +} + +async function runScenario(authMethods: string[], clientSecret?: string) { + const captured: CapturedTokenRequest[] = []; + mockAs(authMethods, captured); + await new DPoPAuthorizationServerScenario().run( + { + url: SERVER_URL, + port: PORT, + clientId: 'test-client-id', + ...(clientSecret && { clientSecret }) + }, + {} + ); + return captured; +} + +describe('DPoPAuthorizationServerScenario — token endpoint auth method selection', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it('uses client_secret_basic over none when the AS offers both and a secret is configured', async () => { + const captured = await runScenario( + ['none', 'client_secret_basic'], + 'test-secret' + ); + expect(captured.length).toBeGreaterThan(0); + const expected = Buffer.from('test-client-id:test-secret').toString( + 'base64' + ); + expect(captured[0].headers.authorization).toBe(`Basic ${expected}`); + }); + + it('uses client_secret_post over none when the AS offers both and a secret is configured', async () => { + const captured = await runScenario( + ['none', 'client_secret_post'], + 'test-secret' + ); + expect(captured.length).toBeGreaterThan(0); + const params = new URLSearchParams(captured[0].body); + expect(params.get('client_secret')).toBe('test-secret'); + expect(captured[0].headers.authorization).toBeUndefined(); + }); + + it('sends no client credentials when no secret is configured, even if the AS offers secret methods', async () => { + const captured = await runScenario(['none', 'client_secret_basic']); + expect(captured.length).toBeGreaterThan(0); + const params = new URLSearchParams(captured[0].body); + expect(captured[0].headers.authorization).toBeUndefined(); + expect(params.get('client_secret')).toBeNull(); + }); +}); diff --git a/src/scenarios/authorization-server/dpop.ts b/src/scenarios/authorization-server/dpop.ts index a7a10894..a4e6056d 100644 --- a/src/scenarios/authorization-server/dpop.ts +++ b/src/scenarios/authorization-server/dpop.ts @@ -441,11 +441,16 @@ browser login + callback for login-gated servers.`; ): 'none' | 'client_secret_post' | 'client_secret_basic' | null { const authMethods: string[] = metadata.token_endpoint_auth_methods_supported ?? ['client_secret_basic']; - if (!options.clientSecret || authMethods.includes('none')) return 'none'; - if (authMethods.includes('client_secret_post')) return 'client_secret_post'; - if (authMethods.includes('client_secret_basic')) { - return 'client_secret_basic'; + // When a secret is configured, prefer a confidential-client method over + // `none` so servers supporting both are exercised on the confidential path. + if (options.clientSecret) { + if (authMethods.includes('client_secret_post')) + return 'client_secret_post'; + if (authMethods.includes('client_secret_basic')) { + return 'client_secret_basic'; + } } + if (!options.clientSecret || authMethods.includes('none')) return 'none'; return null; }