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>
82 lines
3.0 KiB
JavaScript
82 lines
3.0 KiB
JavaScript
import crypto from 'crypto';
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
export const SESSION_COOKIE = 'hb_session';
|
|
export const TX_COOKIE = 'hb_oidc_tx';
|
|
const TX_MAX_AGE = 600; // Sekunden
|
|
|
|
const cookieBase = (config)=>({ httpOnly: true, secure: config.secureCookies, sameSite: 'lax' });
|
|
|
|
// ---- hb_session: JWT HS256, feste Laufzeit ----
|
|
export const issueSession = (res, config, user, { iss, sub })=>{
|
|
const token = jwt.sign(
|
|
{ v: 1, uid: user.id, username: user.username, iss, sub },
|
|
config.sessionSecret,
|
|
{ algorithm: 'HS256', expiresIn: config.sessionTtl }
|
|
);
|
|
res.cookie(SESSION_COOKIE, token, { ...cookieBase(config), path: '/', maxAge: config.sessionTtl * 1000 });
|
|
return token;
|
|
};
|
|
|
|
export const readSession = (req, config)=>{
|
|
const token = req.cookies?.[SESSION_COOKIE];
|
|
if(!token || !config.sessionSecret) return null;
|
|
try {
|
|
const p = jwt.verify(token, config.sessionSecret, { algorithms: ['HS256'] });
|
|
if(p.v !== 1 || !p.username || !p.uid) return null;
|
|
return p;
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
export const clearSession = (res, config)=>{
|
|
res.clearCookie(SESSION_COOKIE, { ...cookieBase(config), path: '/' });
|
|
};
|
|
|
|
// ---- hb_oidc_tx: AES-256-GCM, Schlüssel per HKDF aus HB_SESSION_SECRET ----
|
|
const txKey = (config)=>Buffer.from(crypto.hkdfSync('sha256', config.sessionSecret, '', 'oidc-tx', 32));
|
|
|
|
export const sealTx = (config, payload)=>{
|
|
const iv = crypto.randomBytes(12);
|
|
const cipher = crypto.createCipheriv('aes-256-gcm', txKey(config), iv);
|
|
const enc = Buffer.concat([cipher.update(JSON.stringify(payload), 'utf8'), cipher.final()]);
|
|
return Buffer.concat([iv, cipher.getAuthTag(), enc]).toString('base64url');
|
|
};
|
|
|
|
export const openTx = (config, value)=>{
|
|
try {
|
|
const buf = Buffer.from(value, 'base64url');
|
|
if(buf.length < 29) return null;
|
|
const decipher = crypto.createDecipheriv('aes-256-gcm', txKey(config), buf.subarray(0, 12));
|
|
decipher.setAuthTag(buf.subarray(12, 28));
|
|
const dec = Buffer.concat([decipher.update(buf.subarray(28)), decipher.final()]);
|
|
return JSON.parse(dec.toString('utf8'));
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
export const setTx = (res, config, tx)=>{
|
|
res.cookie(TX_COOKIE, sealTx(config, tx), { ...cookieBase(config), path: '/auth/oidc', maxAge: TX_MAX_AGE * 1000 });
|
|
};
|
|
|
|
// Liest und löscht die Transaktion; null bei fehlend/manipuliert/abgelaufen
|
|
export const takeTx = (req, res, config, now = Date.now())=>{
|
|
const raw = req.cookies?.[TX_COOKIE];
|
|
res.clearCookie(TX_COOKIE, { ...cookieBase(config), path: '/auth/oidc' });
|
|
if(!raw) return null;
|
|
const tx = openTx(config, raw);
|
|
if(!tx || typeof tx.createdAt !== 'number' || now - tx.createdAt > TX_MAX_AGE * 1000) return null;
|
|
return tx;
|
|
};
|
|
|
|
export const randomToken = ()=>crypto.randomBytes(32).toString('base64url');
|
|
|
|
// Nur relative Pfade zulassen (kein "//", kein "\")
|
|
export const safeRedirect = (value)=>{
|
|
if(typeof value !== 'string' || !value.startsWith('/')) return '/';
|
|
if(value.startsWith('//') || value.includes('\\') || /[\u0000-\u001f]/.test(value)) return '/';
|
|
return value;
|
|
};
|