feat(auth): accept Bearer tokens for headless API clients

`requireAuth()` keeps the existing cookie-session path as the primary check;
when no session cookie is present it now falls back to validating
`Authorization: Bearer hlk_*` against the existing `ApiToken` table
(HMAC-SHA-256 hash, expiry + revocation enforced).

Behaviour for browser clients is unchanged — the cookie path runs first
and short-circuits before the header lookup. `requireAdmin()` stays
cookie-only by design.

Wide-Event annotation tags `auth_method: "bearer"` so the source is
distinguishable in Loki. `auth.bearer.success` and `auth.bearer.failure`
audit entries record the token id + reason.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-03 21:51:54 +02:00
1 parent 7507c29c4c
commit 54f689247e
3 files changed
+392 -26

No files matched your search

+1 -1
View File
@@ -51,7 +51,7 @@ export interface WideEvent {
auth?: {
user_id?: string;
user_role?: string;
auth_method?: "session" | "api_key" | "cron_secret" | "telegram_webhook" | "webhook_secret";
auth_method?: "session" | "bearer" | "api_key" | "cron_secret" | "telegram_webhook" | "webhook_secret";
};
// Business-Daten (was wurde getan)