auth

Main authentication module for my projects
git clone git://git.finwo.net/app/auth
Log | Files | Refs

http-account-chooser.test.js (5323B)


      1 import test from 'node:test';
      2 import assert from 'node:assert/strict';
      3 import { createLocalJWKSet, jwtVerify } from 'jose';
      4 
      5 import {
      6   locationQuery,
      7   mergeCookies,
      8   pkce,
      9   responseCookies,
     10   createHttpHarness,
     11 } from './helpers/http-harness.js';
     12 
     13 test('HTTP host account groups and the account chooser', async (t) => {
     14   const harness = await createHttpHarness();
     15   const {
     16     acme,
     17     app,
     18     origin,
     19     publicUrl,
     20     completeAuthorization,
     21   } = harness;
     22 
     23   try {
     24     await t.test('a host group holds multiple accounts and can switch', async () => {
     25       const aliceLogin = await acme({
     26         method: 'POST',
     27         path: '/api/v1/login/password',
     28         body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' },
     29       });
     30       assert.equal(aliceLogin.status, 200);
     31       const aliceCookie = mergeCookies('', aliceLogin);
     32 
     33       const itLogin = await acme({
     34         method: 'POST',
     35         path: '/api/v1/login/password',
     36         body: { client_id: 'internal-app', identifier: 'it@acme.test', password: 'demo' },
     37         cookie: aliceCookie,
     38       });
     39       assert.equal(itLogin.status, 200);
     40       assert.equal(responseCookies(itLogin)[0], responseCookies(aliceLogin)[0]);
     41       const cookie = mergeCookies(aliceCookie, itLogin);
     42 
     43       const session = await acme({ path: '/api/v1/session', cookie });
     44       assert.equal(session.status, 200);
     45       assert.equal(session.json.user.username, 'acme-it');
     46       assert.deepEqual(
     47         session.json.accounts.map((account) => account.user.username),
     48         ['alice', 'acme-it']
     49       );
     50       assert.equal(
     51         session.json.accounts.find((account) => account.user.username === 'acme-it').active,
     52         true
     53       );
     54 
     55       const aliceAccount = session.json.accounts.find(
     56         (account) => account.user.username === 'alice'
     57       );
     58       const selected = await acme({
     59         method: 'POST',
     60         path: '/api/v1/session/select',
     61         body: { public_id: aliceAccount.public_id, client_id: 'internal-app' },
     62         cookie,
     63       });
     64       assert.equal(selected.status, 200);
     65       assert.equal(selected.json.user.username, 'alice');
     66       assert.equal(selected.json.pending_auth_request, false);
     67 
     68       const switched = await acme({ path: '/api/v1/session', cookie });
     69       assert.equal(switched.status, 200);
     70       assert.equal(switched.json.user.username, 'alice');
     71     });
     72 
     73     await t.test('authorize asks which account to use when the group has multiple accounts', async () => {
     74       const aliceLogin = await app({
     75         method: 'POST',
     76         path: '/api/v1/login/password',
     77         body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' },
     78       });
     79       let jar = mergeCookies('', aliceLogin);
     80       const itLogin = await app({
     81         method: 'POST',
     82         path: '/api/v1/login/password',
     83         body: { client_id: 'internal-app', identifier: 'it@acme.test', password: 'demo' },
     84         cookie: jar,
     85       });
     86       jar = mergeCookies(jar, itLogin);
     87 
     88       const { verifier, challenge } = pkce();
     89       const redirectUri = `${origin('app.acme.localhost')}/callback`;
     90       const authorizePath =
     91         `/oidc/authorize?response_type=code&client_id=internal-app` +
     92         `&redirect_uri=${encodeURIComponent(redirectUri)}&scope=openid%20email` +
     93         `&state=state-select&code_challenge=${challenge}&code_challenge_method=S256`;
     94 
     95       const first = await app({ path: authorizePath, cookie: jar });
     96       assert.equal(first.status, 302);
     97       assert.equal(locationQuery(first).pathname, '/oidc/login');
     98       jar = mergeCookies(jar, first);
     99 
    100       const session = await app({ path: '/api/v1/session', cookie: jar });
    101       const aliceAccount = session.json.accounts.find(
    102         (account) => account.user.username === 'alice'
    103       );
    104       const selected = await app({
    105         method: 'POST',
    106         path: '/api/v1/session/select',
    107         body: { public_id: aliceAccount.public_id, client_id: 'internal-app' },
    108         cookie: jar,
    109       });
    110       assert.equal(selected.status, 200);
    111       assert.equal(selected.json.pending_auth_request, true);
    112       assert.equal(selected.json.user.username, 'alice');
    113 
    114       const completed = await completeAuthorization(app, {
    115         path: authorizePath,
    116         cookie: jar,
    117         clientId: 'internal-app',
    118       });
    119       assert.equal(completed.response.status, 302);
    120       const redirected = locationQuery(completed.response);
    121       assert.equal(redirected.searchParams.get('state'), 'state-select');
    122       const code = redirected.searchParams.get('code');
    123       assert.ok(code);
    124 
    125       const tokenResponse = await app({
    126         method: 'POST',
    127         path: '/oidc/token',
    128         form: {
    129           grant_type: 'authorization_code',
    130           code,
    131           redirect_uri: redirectUri,
    132           client_id: 'internal-app',
    133           code_verifier: verifier,
    134         },
    135       });
    136       assert.equal(tokenResponse.status, 200);
    137 
    138       const jwks = await app({ path: '/oidc/jwks' });
    139       const keySet = createLocalJWKSet(jwks.json);
    140       const { payload } = await jwtVerify(tokenResponse.json.id_token, keySet, {
    141         issuer: `${publicUrl}/oidc/acme`,
    142         audience: 'internal-app',
    143       });
    144       assert.equal(payload.preferred_username, 'alice');
    145     });
    146 
    147   } finally {
    148     await harness.close();
    149   }
    150 });