Zusaetzlicher Anmeldeweg per OIDC neben dem bestehenden Passwort-Login. Ohne gesetztes OIDC_PROVIDERS aendert sich nichts: /api/auth/oidc/providers meldet enabled:false und der Login-Screen bleibt unveraendert. Backend - oidc_config.py: Provider-Registry rein aus ENV. Ein zweiter Provider ist reine Konfiguration (OIDC_PROVIDERS + OIDC_<KEY>_* Block), keine Codeaenderung. - oidc_client.py: Discovery, JWKS, Token-Tausch, ID-Token-Pruefung ueber authlib.jose mit fest gepinnter Signatur-Allowlist (nur asymmetrisch, damit alg:none und Algorithmus-Confusion ausgeschlossen sind). - oidc_identity.py: Zuordnung ueber (provider_key, sub). Ein E-Mail-Treffer auf ein bestehendes Konto wird standardmaessig abgelehnt statt verknuepft — sonst erbt jeder, der im IdP eine beliebige E-Mail setzen kann, das passende Konto. Verknuepfen ist eine explizite, authentifizierte Aktion. Auto-Provisioning nur per ENV-Flag, standardmaessig aus, optional mit Domain-Allowlist. - routers/oidc_router.py: /providers, /start, /callback, /complete, /exchange, /link, /identities. PKCE S256; Flow-State als kurzlebiges signiertes JWT in einem HttpOnly-Cookie mit jti==state statt eines prozesslokalen Dicts, damit der Flow Neustarts und mehrere Worker uebersteht. Das Calendarr-JWT wird ueber ein Einmal-Cookie uebergeben, nie ueber URL oder Fragment. - /exchange als Kontrakt fuer die AppAuth-Clients: die App fuehrt den Flow als Public Client selbst aus und tauscht das ID-Token; kein Secret im Request. - models.py: neue Tabelle oidc_identities, users.auth_source; Migration in main.py im dortigen try/except-Stil. Nebenbefund, mitbehoben: auth.verify_password liess ValueError aus bcrypt.checkpw durch. Ein nicht wohlgeformter password_hash wurde damit zu HTTP 500 auf /api/auth/login und bei jedem CalDAV-Basic-Auth. SSO-Nutzer bekommen einen echten bcrypt-Hash ueber ein zufaelliges, nirgends gespeichertes Geheimnis; fuer CalDAV nutzen sie App-Passwoerter wie 2FA-Nutzer. Web - SSO-Block auf dem Login-Screen, aus /providers befuellt; Buttons sind Links, weil /start eine Top-Level-Navigation braucht. - consumeSsoHandoff() laeuft vor den Setup-/Token-Pruefungen in boot() und raeumt die Query per replaceState ab, bevor es awaited. - 401-Auto-Logout um die SSO-Pfade erweitert, sonst dreht ein fehlgeschlagener Handoff eine Reload-Schleife. - i18n-Keys in de + en (Fallback-Kette deckt die uebrigen acht Sprachen). - Service Worker laesst /api/auth/oidc/ unangetastet durch. 37 neue Tests (Gesamtsuite 60 gruen): Signaturpruefung mit echten RSA-Tokens, alg:none, falscher iss/aud/nonce, unbekannte client_id, Provisioning-Policies, Username-Kollision, Einmaligkeit des Handoff-Cookies, unerreichbarer Provider. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
122 lines
4.1 KiB
JavaScript
122 lines
4.1 KiB
JavaScript
import { t } from './i18n.js';
|
|
const BASE = '/api';
|
|
|
|
// Paths where a 401 is a normal answer rather than an expired session, so the
|
|
// auto-logout reload must not fire (it would loop on a failed SSO handoff).
|
|
const NO_RELOAD_ON_401 = ['/auth/login', '/auth/oidc/complete', '/auth/oidc/exchange'];
|
|
|
|
async function request(method, path, body = null, formEncoded = false) {
|
|
const token = localStorage.getItem('token');
|
|
const headers = {};
|
|
|
|
if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
|
|
let bodyStr = null;
|
|
if (body !== null) {
|
|
if (formEncoded) {
|
|
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
bodyStr = new URLSearchParams(body).toString();
|
|
} else {
|
|
headers['Content-Type'] = 'application/json';
|
|
bodyStr = JSON.stringify(body);
|
|
}
|
|
}
|
|
|
|
const res = await fetch(`${BASE}${path}`, { method, headers, body: bodyStr });
|
|
|
|
if (res.status === 401 && !NO_RELOAD_ON_401.some(p => path.startsWith(p))) {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
window.location.reload();
|
|
return null;
|
|
}
|
|
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({ detail: t('unknown_error') }));
|
|
throw new Error(err.detail || `HTTP ${res.status}`);
|
|
}
|
|
|
|
if (res.status === 204) return null;
|
|
return res.json();
|
|
}
|
|
|
|
async function uploadRequest(path, formData) {
|
|
const token = localStorage.getItem('token');
|
|
const headers = {};
|
|
if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
|
|
const res = await fetch(`${BASE}${path}`, { method: 'POST', headers, body: formData });
|
|
|
|
if (res.status === 401) {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
window.location.reload();
|
|
return null;
|
|
}
|
|
if (!res.ok) {
|
|
// Upload errors may be non-JSON (e.g. an nginx 413/502 HTML page); fall back
|
|
// to the HTTP status so the message is diagnostic, not "unknown error".
|
|
const err = await res.json().catch(() => null);
|
|
const detail = (err && err.detail)
|
|
? err.detail
|
|
: (res.status === 413 ? t('upload_too_large') : `HTTP ${res.status} ${res.statusText || ''}`.trim());
|
|
throw new Error(detail);
|
|
}
|
|
if (res.status === 204) return null;
|
|
return res.json();
|
|
}
|
|
|
|
async function downloadRequest(path, fallbackName) {
|
|
const token = localStorage.getItem('token');
|
|
const headers = {};
|
|
if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
|
|
const res = await fetch(`${BASE}${path}`, { method: 'GET', headers });
|
|
if (res.status === 401) {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
window.location.reload();
|
|
return null;
|
|
}
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({ detail: t('unknown_error') }));
|
|
throw new Error(err.detail || `HTTP ${res.status}`);
|
|
}
|
|
// Derive filename from Content-Disposition if present.
|
|
let filename = fallbackName || 'calendar.ics';
|
|
const cd = res.headers.get('Content-Disposition') || '';
|
|
const m = cd.match(/filename="?([^"]+)"?/);
|
|
if (m) filename = m[1];
|
|
|
|
const blob = await res.blob();
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = filename;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
URL.revokeObjectURL(url);
|
|
}
|
|
|
|
export const api = {
|
|
get: (path) => request('GET', path),
|
|
post: (path, body) => request('POST', path, body),
|
|
put: (path, body) => request('PUT', path, body),
|
|
delete: (path) => request('DELETE', path),
|
|
upload: (path, form) => uploadRequest(path, form),
|
|
download: (path, name) => downloadRequest(path, name),
|
|
|
|
login: (username, password, totp_code = null, remember_me = false) =>
|
|
request('POST', '/auth/login', { username, password, totp_code, remember_me }),
|
|
|
|
setupRequired: () => request('GET', '/auth/setup-required'),
|
|
setup: (data) => request('POST', '/auth/setup', data),
|
|
|
|
// Single sign-on. oidcComplete() trades the one-time HttpOnly handoff cookie
|
|
// set by the OIDC callback for the access token.
|
|
oidcProviders: () => request('GET', '/auth/oidc/providers'),
|
|
oidcComplete: () => request('POST', '/auth/oidc/complete', {}),
|
|
oidcIdentities: () => request('GET', '/auth/oidc/identities'),
|
|
};
|