53 lines
4.1 KiB
JavaScript
Executable File
53 lines
4.1 KiB
JavaScript
Executable File
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { generateKeyPairSync, createSign } from 'node:crypto';
|
|
import { Auth } from '../auth.mjs';
|
|
|
|
test('Dex adapter verifies signed tokens, PKCE, state, nonce, audience, and stable account identity', async t => {
|
|
const { privateKey, publicKey } = generateKeyPairSync('rsa', { modulusLength: 2048 });
|
|
const jwk = { ...publicKey.export({ format: 'jwk' }), kid: 'test-key', use: 'sig', alg: 'RS256' };
|
|
const issuer = 'https://dex.test/', origin = 'https://game.test';
|
|
let nonce, wrongAudience = false, wrongNonce = false, wrongIssuer = false, unverified = false, missingConnector = false, forged = false, tokenCalls = 0;
|
|
const identities = [];
|
|
const auth = new Auth({ mode: 'dex', issuer, origin, clientId: 'wikirpg', secret: 'test-only-secret', connector: 'ldap' }, {
|
|
accountForIdentity(...args) { identities.push(args); return 'player-account'; },
|
|
});
|
|
t.mock.method(globalThis, 'fetch', async (url, options) => {
|
|
const address = String(url);
|
|
if (address.endsWith('.well-known/openid-configuration')) return Response.json({ issuer, authorization_endpoint: issuer + 'auth', token_endpoint: issuer + 'token', jwks_uri: issuer + 'keys', response_types_supported: ['code'], subject_types_supported: ['public'], id_token_signing_alg_values_supported: ['RS256'], token_endpoint_auth_methods_supported: ['client_secret_post'] });
|
|
if (address === issuer + 'keys') return Response.json({ keys: [jwk] });
|
|
if (address === issuer + 'token') {
|
|
tokenCalls++;
|
|
assert.ok(new URLSearchParams(options.body).get('code_verifier'));
|
|
const encode = v => Buffer.from(JSON.stringify(v)).toString('base64url');
|
|
const payload = { iss: wrongIssuer ? 'https://wrong.test/' : issuer, sub: 'ldap-uid-42', aud: wrongAudience ? 'other-client' : 'wikirpg', exp: Math.floor(Date.now() / 1000) + 300, iat: Math.floor(Date.now() / 1000), nonce: wrongNonce ? 'wrong' : nonce, email_verified: !unverified, name: 'Dex Traveler', ...(missingConnector ? {} : { federated_claims: { connector_id: 'ldap', user_id: 'uid-42' } }) };
|
|
const unsigned = `${encode({ alg: 'RS256', kid: 'test-key' })}.${encode(payload)}`;
|
|
const signer = createSign('RSA-SHA256'); signer.update(unsigned); signer.end();
|
|
const signature = signer.sign(privateKey); if (forged) signature[0] ^= 255;
|
|
return Response.json({ access_token: 'test-access-token', token_type: 'Bearer', expires_in: 300, id_token: `${unsigned}.${signature.toString('base64url')}` });
|
|
}
|
|
throw new Error('Unexpected provider request');
|
|
});
|
|
await auth.initialize();
|
|
async function attempt(overrides = {}) {
|
|
const login = await auth.login(), authorization = new URL(login.url);
|
|
nonce = authorization.searchParams.get('nonce');
|
|
assert.equal(authorization.searchParams.get('code_challenge_method'), 'S256');
|
|
assert.ok(authorization.searchParams.get('code_challenge'));
|
|
const callback = new URL(origin + '/auth/oidc/callback'); callback.searchParams.set('code', 'test-code'); callback.searchParams.set('state', overrides.state || authorization.searchParams.get('state'));
|
|
return auth.callback({ headers: { cookie: `id=${login.token}` } }, callback);
|
|
}
|
|
const token = await attempt();
|
|
assert.equal(auth.sessions.get(token).account, 'player-account');
|
|
assert.deepEqual(identities[0], [issuer, 'ldap-uid-42', 'Dex Traveler']);
|
|
assert.match(auth.cookie(token), /Secure/); assert.match(auth.cookie(token), /HttpOnly/);
|
|
const calls = tokenCalls; await assert.rejects(() => attempt({ state: 'wrong-state' })); assert.equal(tokenCalls, calls);
|
|
wrongAudience = true; await assert.rejects(() => attempt()); wrongAudience = false;
|
|
wrongNonce = true; await assert.rejects(() => attempt()); wrongNonce = false;
|
|
wrongIssuer = true; await assert.rejects(() => attempt()); wrongIssuer = false;
|
|
missingConnector = true; await assert.rejects(() => attempt()); missingConnector = false;
|
|
forged = true; await assert.rejects(() => attempt()); forged = false;
|
|
unverified = true; await assert.rejects(() => attempt());
|
|
assert.equal(identities.length, 1);
|
|
});
|