10abd36340
Authentication previously implied full authorization: any valid token could manage every host, DNS provider, domain, and user. This adds SSO login and a per-domain rights model. OIDC login (authorization_code + PKCE): - conf.oidc + conf.auth blocks; clientSecret in (gitignored) secrets.js. - utils/oidc.js (state/PKCE, code exchange, userinfo) using global fetch. - models/oidc_state.js: short-lived state store, auto-expiring via model-redis 1.5 per-key TTL. - routes/auth.js: GET /auth/oidc/start + /auth/oidc/callback; JIT-provisions a local user, mints an AuthToken carrying the SSO groups, hands the token to the browser via a URL fragment. "Log in with SSO" button on the login page. Authorization (groups + app overrides, per-domain, with ownership): - models/grant.js + utils/roles.js (pure, unit-tested): effective rights from conf.auth (admin users/groups, group->role map), Grant records (user|group -> global|domain -> viewer|manager|admin), and ownership (created_by). Roles rank admin > manager(owner) > viewer. - AuthToken stores session groups; middleware/auth.js exposes req.groups. - middleware/authz.js: requireAdmin, requireDomainRole(minRole, resolveDomain), filterViewable. Applied across routes: host mutations need manager on the host's domain; reads are filtered to visible domains; DNS providers, user management, and grant management are global-admin-only; certs need viewer. - routes/grant.js: admin CRUD for grants. Anti-lockout via conf.auth.adminUsers plus migrations/grant_bootstrap.js. Frontend: /me returns effective rights; nav gates Users/Grants to admins; grants management page; OIDC token-fragment handling in app-base.js. Tests: utils/roles and utils/oidc unit-tested (no redis); wired into the test scripts. Full suite 89 pass. Also verified end-to-end against redis (grant resolution, middleware allow/deny/403, list filtering) and the OIDC pure flow. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
78 lines
2.7 KiB
JavaScript
78 lines
2.7 KiB
JavaScript
'use strict';
|
|
|
|
const {describe, test} = require('node:test');
|
|
const assert = require('node:assert');
|
|
const crypto = require('crypto');
|
|
|
|
const oidc = require('../../utils/oidc');
|
|
const conf = require('@simpleworkjs/conf');
|
|
|
|
/**
|
|
* Tests for the pure parts of the OIDC client (utils/oidc): PKCE/state
|
|
* generation, authorize-URL construction, and claim mapping. Network calls
|
|
* (exchangeCode/fetchUserInfo) are not exercised here.
|
|
*/
|
|
|
|
describe('oidc PKCE / state', () => {
|
|
test('createAuthRequest returns distinct high-entropy state and verifier', () => {
|
|
const a = oidc.createAuthRequest();
|
|
assert.ok(a.state.length >= 20);
|
|
assert.ok(a.codeVerifier.length >= 20);
|
|
assert.notStrictEqual(a.state, a.codeVerifier);
|
|
|
|
const b = oidc.createAuthRequest();
|
|
assert.notStrictEqual(a.state, b.state);
|
|
});
|
|
|
|
test('code challenge is the base64url S256 of the verifier', () => {
|
|
const {codeVerifier, codeChallenge} = oidc.createAuthRequest();
|
|
const expected = crypto.createHash('sha256').update(codeVerifier).digest('base64')
|
|
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
assert.strictEqual(codeChallenge, expected);
|
|
});
|
|
|
|
test('challenge is base64url (no +, /, or = padding)', () => {
|
|
const {codeChallenge} = oidc.createAuthRequest();
|
|
assert.ok(!/[+/=]/.test(codeChallenge));
|
|
});
|
|
});
|
|
|
|
describe('oidc buildAuthUrl', () => {
|
|
test('includes required authorization-code + PKCE params', () => {
|
|
const url = new URL(oidc.buildAuthUrl('the-state', 'the-challenge'));
|
|
assert.strictEqual(url.origin + url.pathname, conf.oidc.authorizationEndpoint);
|
|
const p = url.searchParams;
|
|
assert.strictEqual(p.get('response_type'), 'code');
|
|
assert.strictEqual(p.get('client_id'), conf.oidc.clientId);
|
|
assert.strictEqual(p.get('redirect_uri'), conf.oidc.redirectUri);
|
|
assert.strictEqual(p.get('state'), 'the-state');
|
|
assert.strictEqual(p.get('code_challenge'), 'the-challenge');
|
|
assert.strictEqual(p.get('code_challenge_method'), 'S256');
|
|
assert.ok(p.get('scope').includes('openid'));
|
|
assert.ok(p.get('scope').includes('groups'));
|
|
});
|
|
});
|
|
|
|
describe('oidc claimsToIdentity', () => {
|
|
test('maps preferred_username and groups', () => {
|
|
const id = oidc.claimsToIdentity({
|
|
sub: 'abc',
|
|
preferred_username: 'jane',
|
|
groups: ['dns-team', 'proxy-admins'],
|
|
});
|
|
assert.strictEqual(id.username, 'jane');
|
|
assert.deepStrictEqual(id.groups, ['dns-team', 'proxy-admins']);
|
|
});
|
|
|
|
test('falls back to sub when no preferred_username', () => {
|
|
const id = oidc.claimsToIdentity({sub: 'abc'});
|
|
assert.strictEqual(id.username, 'abc');
|
|
assert.deepStrictEqual(id.groups, []);
|
|
});
|
|
|
|
test('coerces a single group value to an array', () => {
|
|
const id = oidc.claimsToIdentity({sub: 'abc', groups: 'solo'});
|
|
assert.deepStrictEqual(id.groups, ['solo']);
|
|
});
|
|
});
|