auth

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

http-login.test.js (8161B)


      1 import test from 'node:test';
      2 import assert from 'node:assert/strict';
      3 
      4 import {
      5   maxAge,
      6   mergeCookies,
      7   responseCookies,
      8   createHttpHarness,
      9 } from './helpers/http-harness.js';
     10 
     11 test('HTTP password login and session cookies', async (t) => {
     12   const harness = await createHttpHarness();
     13   const {
     14     identity,
     15     acme,
     16     globex,
     17     app,
     18   } = harness;
     19 
     20   try {
     21     await t.test('identifier resolution selects password or upstream OIDC', async () => {
     22       const password = await acme({
     23         method: 'POST',
     24         path: '/api/v1/login/resolve',
     25         body: { client_id: 'internal-app', identifier: 'alice@acme.test' },
     26       });
     27       assert.equal(password.status, 200);
     28       assert.equal(password.json.next, 'password');
     29 
     30       const upstream = await acme({
     31         method: 'POST',
     32         path: '/api/v1/login/resolve',
     33         body: { client_id: 'internal-app', identifier: 'someone@sso.acme.test' },
     34       });
     35       assert.equal(upstream.status, 200);
     36       assert.equal(upstream.json.next, 'oidc');
     37       assert.equal(upstream.json.provider.display_name, 'Example SSO');
     38     });
     39 
     40     await t.test('password login creates a realm session with the short cookie', async () => {
     41       const response = await acme({
     42         method: 'POST',
     43         path: '/api/v1/login/password',
     44         body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' },
     45       });
     46       assert.equal(response.status, 200);
     47       assert.equal(response.json.user.username, 'alice');
     48       assert.equal(maxAge(response), 12 * 60 * 60);
     49       assert.ok(responseCookies(response)[0].startsWith('auth_session='));
     50 
     51       const cookie = mergeCookies('', response);
     52       const session = await acme({ path: '/api/v1/session', cookie });
     53       assert.equal(session.status, 200);
     54       assert.equal(session.json.user.username, 'alice');
     55       assert.equal(session.json.scope.type, 'realm');
     56       assert.equal(session.json.scope.host, 'acme.localhost');
     57       assert.equal(session.json.realm.slug, 'acme');
     58     });
     59 
     60     await t.test('canonical and client hosts create their own session scopes', async () => {
     61       const canonical = await identity({
     62         method: 'POST',
     63         path: '/api/v1/login/password',
     64         body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' },
     65       });
     66       assert.equal(canonical.status, 200);
     67       const canonicalCookie = mergeCookies('', canonical);
     68       const canonicalSession = await identity({
     69         path: '/api/v1/session',
     70         cookie: canonicalCookie,
     71       });
     72       assert.equal(canonicalSession.status, 200);
     73       assert.equal(canonicalSession.json.scope.type, 'global');
     74       assert.equal(canonicalSession.json.scope.host, 'identity.test');
     75       assert.equal(canonicalSession.json.realm, null);
     76 
     77       const client = await app({
     78         method: 'POST',
     79         path: '/api/v1/login/password',
     80         body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' },
     81       });
     82       assert.equal(client.status, 200);
     83       const clientSession = await app({
     84         path: '/api/v1/session',
     85         cookie: mergeCookies('', client),
     86       });
     87       assert.equal(clientSession.status, 200);
     88       assert.equal(clientSession.json.scope.type, 'client');
     89       assert.equal(clientSession.json.scope.host, 'app.acme.localhost');
     90       assert.equal(clientSession.json.scope.client_id, 'internal-app');
     91       assert.equal(clientSession.json.realm.slug, 'acme');
     92     });
     93 
     94     await t.test('sessions are scoped to their host and old cookies are rejected', async () => {
     95       const login = await acme({
     96         method: 'POST',
     97         path: '/api/v1/login/password',
     98         body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' },
     99       });
    100       const cookie = mergeCookies('', login);
    101       const token = cookie.slice('auth_session='.length);
    102 
    103       const wrongHost = await globex({ path: '/api/v1/session', cookie });
    104       assert.equal(wrongHost.status, 401);
    105 
    106       const oldCookie = `auth_session_acme=${token}`;
    107       const oldStyle = await acme({ path: '/api/v1/session', cookie: oldCookie });
    108       assert.equal(oldStyle.status, 401);
    109     });
    110 
    111     await t.test('long sessions are only issued when requested', async () => {
    112       const response = await acme({
    113         method: 'POST',
    114         path: '/api/v1/login/password',
    115         body: {
    116           client_id: 'internal-app',
    117           identifier: 'alice@acme.test',
    118           password: 'demo',
    119           long_session: true,
    120         },
    121       });
    122       assert.equal(response.status, 200);
    123       assert.equal(maxAge(response), 7 * 24 * 60 * 60);
    124     });
    125 
    126     await t.test('bad credentials are generic', async () => {
    127       const response = await acme({
    128         method: 'POST',
    129         path: '/api/v1/login/password',
    130         body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'wrong' },
    131       });
    132       assert.equal(response.status, 401);
    133       assert.equal(response.json.error.code, 'invalid_credentials');
    134     });
    135 
    136     await t.test('membership is required where the client demands it', async () => {
    137       const denied = await acme({
    138         method: 'POST',
    139         path: '/api/v1/login/password',
    140         body: {
    141           client_id: 'acme-members-app',
    142           identifier: 'contractor@personal.test',
    143           password: 'demo',
    144         },
    145       });
    146       assert.equal(denied.status, 401);
    147 
    148       const allowed = await acme({
    149         method: 'POST',
    150         path: '/api/v1/login/password',
    151         body: {
    152           client_id: 'acme-public-app',
    153           identifier: 'guest@personal.test',
    154           password: 'demo',
    155         },
    156       });
    157       assert.equal(allowed.status, 200);
    158     });
    159 
    160     await t.test('password login is rejected for upstream-mapped domains', async () => {
    161       const response = await acme({
    162         method: 'POST',
    163         path: '/api/v1/login/password',
    164         body: { client_id: 'internal-app', identifier: 'someone@sso.acme.test', password: 'demo' },
    165       });
    166       assert.equal(response.status, 403);
    167       assert.equal(response.json.error.code, 'provider_required');
    168     });
    169 
    170     await t.test('guests can hold realm-admin roles', async () => {
    171       const response = await globex({
    172         method: 'POST',
    173         path: '/api/v1/login/password',
    174         body: {
    175           client_id: 'globex-members-app',
    176           identifier: 'contractor@personal.test',
    177           password: 'demo',
    178         },
    179       });
    180       assert.equal(response.status, 200);
    181       const session = await globex({
    182         path: '/api/v1/session',
    183         cookie: mergeCookies('', response),
    184       });
    185       assert.equal(session.status, 200);
    186       assert.ok(session.json.roles.includes('realm-admin'));
    187     });
    188 
    189     await t.test('logout removes only the current host session', async () => {
    190       const acmeLogin = await acme({
    191         method: 'POST',
    192         path: '/api/v1/login/password',
    193         body: { client_id: 'internal-app', identifier: 'alice@acme.test', password: 'demo' },
    194       });
    195       const acmeCookie = mergeCookies('', acmeLogin);
    196 
    197       const globexLogin = await globex({
    198         method: 'POST',
    199         path: '/api/v1/login/password',
    200         body: {
    201           client_id: 'globex-members-app',
    202           identifier: 'contractor@personal.test',
    203           password: 'demo',
    204         },
    205       });
    206       const globexCookie = mergeCookies('', globexLogin);
    207 
    208       const logout = await acme({
    209         method: 'POST',
    210         path: '/api/v1/logout',
    211         body: {},
    212         cookie: acmeCookie,
    213       });
    214       assert.equal(logout.status, 200);
    215 
    216       const acmeSession = await acme({ path: '/api/v1/session', cookie: acmeCookie });
    217       assert.equal(acmeSession.status, 401);
    218 
    219       const globexSession = await globex({ path: '/api/v1/session', cookie: globexCookie });
    220       assert.equal(globexSession.status, 200);
    221     });
    222 
    223     await t.test('sessions require a cookie', async () => {
    224       const response = await acme({ path: '/api/v1/session' });
    225       assert.equal(response.status, 401);
    226       assert.equal(response.json.error.code, 'not_authenticated');
    227     });
    228 
    229   } finally {
    230     await harness.close();
    231   }
    232 });