mirror of
https://github.com/traberph/short.git
synced 2026-11-22 22:55:12 +00:00
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>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { mkdtempSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { createApp } from "../src/server/app.js";
|
||||
import type { OidcClaims, OidcProvider } from "../src/server/auth/oidc.js";
|
||||
import { createApiToken, createSession, upsertUser } from "../src/server/auth/sessions.js";
|
||||
import type { Config } from "../src/server/config.js";
|
||||
import { openDb } from "../src/server/db/index.js";
|
||||
|
||||
export const BASE_URL = "https://short.test";
|
||||
|
||||
export function setup() {
|
||||
const config: Config = {
|
||||
baseUrl: BASE_URL,
|
||||
oidc: { issuer: "https://auth.test/", clientId: "id", clientSecret: "secret" },
|
||||
adminGroup: "short-admins",
|
||||
sessionSecret: "x".repeat(32),
|
||||
dataDir: mkdtempSync(path.join(tmpdir(), "short-test-")),
|
||||
timezone: "Europe/Berlin",
|
||||
trustProxy: true,
|
||||
logoDevToken: undefined,
|
||||
disableAttribution: false,
|
||||
retentionDays: 90,
|
||||
port: 0,
|
||||
};
|
||||
const db = openDb(":memory:");
|
||||
|
||||
// The fake provider returns whatever claims the test queued for the next login.
|
||||
let nextClaims: OidcClaims = { sub: "sub-x", username: "x", groups: [] };
|
||||
const oidc: OidcProvider = {
|
||||
authorizationUrl: async ({ state }) => new URL(`https://auth.test/authorize?state=${state}`),
|
||||
exchange: async () => nextClaims,
|
||||
};
|
||||
|
||||
const app = createApp({ db, config, oidc });
|
||||
|
||||
const user = (username: string, isAdmin = false) => {
|
||||
const actor = upsertUser(db, { sub: `sub-${username}`, username, groups: isAdmin ? ["short-admins"] : [] }, "short-admins");
|
||||
const token = createApiToken(db, actor.id, "test").token;
|
||||
const session = createSession(db, actor.id);
|
||||
const api = (method: string, url: string, body?: unknown) =>
|
||||
app.request(`/~/api${url}`, {
|
||||
method,
|
||||
headers: { authorization: `Bearer ${token}`, ...(body !== undefined && { "content-type": "application/json" }) },
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
});
|
||||
return { actor, token, session, api };
|
||||
};
|
||||
|
||||
return {
|
||||
app,
|
||||
db,
|
||||
config,
|
||||
user,
|
||||
setNextLogin: (claims: OidcClaims) => {
|
||||
nextClaims = claims;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Lets setImmediate callbacks (click tracking) run.
|
||||
export const flush = () => new Promise((resolve) => setImmediate(resolve));
|
||||
Reference in New Issue
Block a user