Files
homebrewery-oidc/server/fork/auth/config.spec.js
T
dev-bot BotandClaude Sonnet 5.5 66bb91cb3e
CI / App (npm ci + Build, ESLint, Stylelint, Jest) (pull_request) Successful in 1m42s
fix(auth): Review-Findings M1, M2, S1, S2
- M1: Start bricht ab, wenn oidc+naturalcrit aktiv, nicht lokal und Upstream-secret Standard/leer
- M2: README.md wieder mit CRLF (Diff nur +4)
- S1: /auth/oidc/login zeigt interne Fehlertexte nicht mehr an
- S2: http://-Issuer nur bei http://-HB_PUBLIC_URL

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 09:27:50 +00:00

86 lines
3.8 KiB
JavaScript

import { readAuthConfig, parseTtl, clientConfig } from './config.js';
const base = {
HB_PUBLIC_URL : 'https://brew.example.org/', HB_OIDC_ISSUER : 'https://idp.example/realms/x',
HB_OIDC_CLIENT_ID : 'id', HB_OIDC_CLIENT_SECRET : 's', HB_SESSION_SECRET : 'x'.repeat(32)
};
const own = { upstreamSecret: 'eigenes-secret' };
describe('Auth-Konfiguration', ()=>{
test('Standard ohne Angaben: nur naturalcrit (Upstream-Verhalten)', ()=>{
const c = readAuthConfig({});
expect(c.providers).toEqual(['naturalcrit']);
expect(clientConfig(c).loginUrl).toBeNull();
});
test('Issuer gesetzt, Provider leer: oidc,naturalcrit mit Auswahlseite', ()=>{
const c = readAuthConfig(base, own);
expect(c.providers).toEqual(['oidc', 'naturalcrit']);
expect(c.publicUrl).toBe('https://brew.example.org');
expect(c.secureCookies).toBe(true);
expect(clientConfig(c).loginUrl).toBe('/auth/login');
expect(c.oidc).toMatchObject({ scopes: 'openid profile email', usernameClaim: 'preferred_username', linkByEmail: false, endSession: false, label: 'Anmelden' });
});
test('nur oidc: direkter Login-Link', ()=>{
const c = readAuthConfig({ ...base, HB_AUTH_PROVIDERS: 'oidc' });
expect(c.providers).toEqual(['oidc']);
expect(clientConfig(c).loginUrl).toBe('/auth/oidc/login');
});
test('Startprüfung nennt fehlende Variablen', ()=>{
expect(()=>readAuthConfig({ HB_AUTH_PROVIDERS: 'oidc' })).toThrow(/HB_OIDC_ISSUER.*HB_OIDC_CLIENT_ID.*HB_OIDC_CLIENT_SECRET.*HB_PUBLIC_URL.*HB_SESSION_SECRET/);
});
test('zu kurzes Session-Secret und unbekannter Provider brechen ab', ()=>{
expect(()=>readAuthConfig({ ...base, HB_SESSION_SECRET: 'kurz' }, own)).toThrow(/32 Byte/);
expect(()=>readAuthConfig({ HB_AUTH_PROVIDERS: 'ldap' })).toThrow(/unbekannte/);
});
test('http-URL: Cookies nicht Secure', ()=>{
expect(readAuthConfig({ ...base, HB_PUBLIC_URL: 'http://localhost:8000' }, own).secureCookies).toBe(false);
});
test('TTL-Formate', ()=>{
expect(parseTtl('7d')).toBe(604800);
expect(parseTtl('12h')).toBe(43200);
expect(parseTtl('3600')).toBe(3600);
expect(parseTtl(undefined)).toBe(604800);
expect(()=>parseTtl('bald')).toThrow();
expect(()=>parseTtl('0')).toThrow();
});
test('Secret taucht nicht in der Client-Konfiguration auf', ()=>{
expect(JSON.stringify(clientConfig(readAuthConfig(base, own)))).not.toMatch(/secret|"s"/i);
});
describe('Upstream-secret (fälschbare nc_session)', ()=>{
test('Abbruch bei oidc+naturalcrit, Standard-secret, nicht lokal', ()=>{
expect(()=>readAuthConfig(base, { upstreamSecret: 'secret', isLocal: false })).toThrow(/Upstream-"secret".*HB_AUTH_PROVIDERS=oidc/);
expect(()=>readAuthConfig(base, { upstreamSecret: '', isLocal: false })).toThrow(/Upstream-"secret"/);
expect(()=>readAuthConfig(base)).toThrow(/Upstream-"secret"/);
});
test('kein Abbruch: lokal, nur oidc oder eigenes secret', ()=>{
expect(()=>readAuthConfig(base, { upstreamSecret: 'secret', isLocal: true })).not.toThrow();
expect(()=>readAuthConfig({ ...base, HB_AUTH_PROVIDERS: 'oidc' }, { upstreamSecret: 'secret', isLocal: false })).not.toThrow();
expect(()=>readAuthConfig(base, { upstreamSecret: 'eigenes-secret', isLocal: false })).not.toThrow();
});
test('reiner Upstream-Modus bleibt unverändert', ()=>{
expect(()=>readAuthConfig({}, { upstreamSecret: 'secret', isLocal: false })).not.toThrow();
});
});
describe('Issuer-Schema', ()=>{
test('http-Issuer mit https-HB_PUBLIC_URL bricht ab', ()=>{
expect(()=>readAuthConfig({ ...base, HB_OIDC_ISSUER: 'http://idp.example' }, own)).toThrow(/https:\/\//);
});
test('http-Issuer mit http-HB_PUBLIC_URL ist erlaubt (Dev/Compose)', ()=>{
expect(()=>readAuthConfig({ ...base, HB_OIDC_ISSUER: 'http://idp:8080', HB_PUBLIC_URL: 'http://localhost:8000' }, own)).not.toThrow();
});
});
});