Files
short/src/server/auth/oidc.ts
T
traberphandClaude Opus 5.5 b217214512 v2: rewrite on Hono + SQLite with user namespaces
Replaces the Next.js/Prisma/next-auth app with a single-container
Hono server, SQLite via Drizzle, OIDC login against Authentik and a
React/Vite admin UI that talks to a JSON API.

- users get /<username>/<code>, admins (Authentik group) get root
  codes, pinning and full access
- link pages with link and Markdown text blocks, themes, avatars
- editable 302 redirects, random or custom codes
- personal API tokens, CSRF-protected cookie sessions
- privacy-friendly stats: daily-rotating visitor salt, 90-day click
  retention with daily totals kept
- QR codes as PNG/SVG
- tests for permissions, auth, redirects and stats; CI runs them
  before publishing images

See REQUIREMENTS.md for the full requirement list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 13:57:47 +02:00

54 lines
2.0 KiB
TypeScript

import * as client from "openid-client";
import type { Config } from "../config.js";
export interface OidcClaims {
sub: string;
username: string | undefined;
groups: string[];
}
// Thin wrapper so the app can be tested without a real identity provider.
export interface OidcProvider {
authorizationUrl(params: { state: string; codeChallenge: string; redirectUri: string }): Promise<URL>;
exchange(params: { currentUrl: URL; state: string; codeVerifier: string }): Promise<OidcClaims>;
}
export function createOidcProvider(config: Config): OidcProvider {
// Discovery happens lazily so the server can start while Authentik is unavailable.
let discovered: Promise<client.Configuration> | undefined;
const getConfiguration = () => {
discovered ??= client
.discovery(new URL(config.oidc.issuer), config.oidc.clientId, config.oidc.clientSecret)
.catch((err: unknown) => {
discovered = undefined;
throw err;
});
return discovered;
};
return {
async authorizationUrl({ state, codeChallenge, redirectUri }) {
return client.buildAuthorizationUrl(await getConfiguration(), {
redirect_uri: redirectUri,
scope: "openid profile email",
code_challenge: codeChallenge,
code_challenge_method: "S256",
state,
});
},
async exchange({ currentUrl, state, codeVerifier }) {
const tokens = await client.authorizationCodeGrant(await getConfiguration(), currentUrl, {
pkceCodeVerifier: codeVerifier,
expectedState: state,
});
const claims = tokens.claims();
if (!claims) throw new Error("No ID token returned");
const groups = Array.isArray(claims.groups) ? claims.groups.filter((g): g is string => typeof g === "string") : [];
const username = typeof claims.preferred_username === "string" ? claims.preferred_username : undefined;
return { sub: claims.sub, username, groups };
},
};
}
export { calculatePKCECodeChallenge, randomPKCECodeVerifier, randomState } from "openid-client";