Modul server/fork/auth (Config, Session-Cookie, OIDC-Flow, Kontozuordnung), Einhängen in server/app.js, Client-Helfer für Login/Logout, PUT /api/user/rename für OIDC-Sessions gesperrt (A01). Tests gegen Mock-IdP. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
86 lines
3.5 KiB
JavaScript
86 lines
3.5 KiB
JavaScript
// Auth-Konfiguration ausschließlich aus Umgebungsvariablen (ADR 0001, "Konfiguration").
|
|
const VALID_PROVIDERS = ['oidc', 'naturalcrit'];
|
|
const MIN_SECRET_BYTES = 32;
|
|
|
|
const bool = (v, def = false)=>{
|
|
if(v === undefined || v === null || v === '') return def;
|
|
return ['1', 'true', 'yes', 'on'].includes(String(v).trim().toLowerCase());
|
|
};
|
|
|
|
// "7d", "12h", "30m", "90s", "604800" (Sekunden) -> Sekunden
|
|
export const parseTtl = (value, def = 7 * 24 * 3600)=>{
|
|
if(value === undefined || value === '') return def;
|
|
const m = /^(\d+)\s*([smhdw]?)$/i.exec(String(value).trim());
|
|
if(!m) throw new Error(`HB_SESSION_TTL ungültig: "${value}" (erwartet z. B. 7d, 12h, 3600)`);
|
|
const mult = { '': 1, s: 1, m: 60, h: 3600, d: 86400, w: 604800 }[m[2].toLowerCase()];
|
|
const seconds = Number(m[1]) * mult;
|
|
if(seconds <= 0) throw new Error('HB_SESSION_TTL muss größer als 0 sein');
|
|
return seconds;
|
|
};
|
|
|
|
export const readAuthConfig = (env = process.env)=>{
|
|
const issuer = env.HB_OIDC_ISSUER || undefined;
|
|
|
|
let providers;
|
|
if(env.HB_AUTH_PROVIDERS && env.HB_AUTH_PROVIDERS.trim()) {
|
|
providers = [...new Set(env.HB_AUTH_PROVIDERS.split(',').map((p)=>p.trim().toLowerCase()).filter(Boolean))];
|
|
const unknown = providers.filter((p)=>!VALID_PROVIDERS.includes(p));
|
|
if(unknown.length) throw new Error(`HB_AUTH_PROVIDERS: unbekannte Werte ${unknown.join(', ')} (erlaubt: ${VALID_PROVIDERS.join(', ')})`);
|
|
} else {
|
|
providers = issuer ? ['oidc', 'naturalcrit'] : ['naturalcrit'];
|
|
}
|
|
|
|
const publicUrl = (env.HB_PUBLIC_URL || '').replace(/\/+$/, '');
|
|
const config = {
|
|
providers,
|
|
publicUrl,
|
|
secureCookies : publicUrl.startsWith('https://'),
|
|
sessionSecret : env.HB_SESSION_SECRET || undefined,
|
|
sessionTtl : parseTtl(env.HB_SESSION_TTL),
|
|
oidc : {
|
|
issuer,
|
|
clientId : env.HB_OIDC_CLIENT_ID || undefined,
|
|
clientSecret : env.HB_OIDC_CLIENT_SECRET || undefined,
|
|
scopes : (env.HB_OIDC_SCOPES || 'openid profile email').trim(),
|
|
usernameClaim : env.HB_OIDC_USERNAME_CLAIM || 'preferred_username',
|
|
linkByEmail : bool(env.HB_OIDC_LINK_BY_EMAIL),
|
|
endSession : bool(env.HB_OIDC_END_SESSION),
|
|
label : env.HB_OIDC_LABEL || 'Anmelden'
|
|
}
|
|
};
|
|
|
|
if(providers.includes('oidc')) {
|
|
const required = {
|
|
HB_OIDC_ISSUER : config.oidc.issuer,
|
|
HB_OIDC_CLIENT_ID : config.oidc.clientId,
|
|
HB_OIDC_CLIENT_SECRET : config.oidc.clientSecret,
|
|
HB_PUBLIC_URL : config.publicUrl,
|
|
HB_SESSION_SECRET : config.sessionSecret
|
|
};
|
|
const missing = Object.entries(required).filter(([, v])=>!v).map(([k])=>k);
|
|
if(missing.length) throw new Error(`Auth-Provider "oidc" ist aktiv, aber es fehlen: ${missing.join(', ')}`);
|
|
if(Buffer.byteLength(config.sessionSecret) < MIN_SECRET_BYTES) {
|
|
throw new Error(`HB_SESSION_SECRET muss mindestens ${MIN_SECRET_BYTES} Byte lang sein`);
|
|
}
|
|
if(!/^https?:\/\//.test(config.publicUrl)) throw new Error('HB_PUBLIC_URL muss mit http:// oder https:// beginnen');
|
|
try { new URL(config.oidc.issuer); } catch { throw new Error('HB_OIDC_ISSUER ist keine gültige URL'); }
|
|
}
|
|
|
|
return config;
|
|
};
|
|
|
|
export const hasProvider = (config, name)=>config.providers.includes(name);
|
|
|
|
// Nur das, was der Client wissen darf (kein Secret)
|
|
const loginUrlFor = (providers)=>{
|
|
if(!providers.includes('oidc')) return null;
|
|
return providers.includes('naturalcrit') ? '/auth/login' : '/auth/oidc/login';
|
|
};
|
|
|
|
export const clientConfig = (config)=>({
|
|
providers : config.providers,
|
|
loginUrl : loginUrlFor(config.providers),
|
|
logoutUrl : '/auth/logout',
|
|
oidcLabel : config.oidc.label
|
|
});
|