mirror of
https://github.com/Dvorinka/Primora.git
synced 2026-10-28 15:51:23 +00:00
Self-hosted instances are now email/password only — OAuth providers (GitHub/Google/Discord/Microsoft) activate solely when PRIMORA_MANAGED=true, reserved for the future hosted tier. Sign-up lifecycle: the first public sign-up becomes the instance admin; after bootstrap, sign-up closes by default. A better-auth databaseHook gates only /sign-up/email, so admin-created users and OAuth flows bypass it. The login page reads GET /instance/public to hide the sign-up tab, show the bootstrap notice, and render only the providers the instance offers. Instance settings live in core.settings (migration 00010) and resolve in-app > env > built-in default, cached 15s. Platform admins (better-auth user.role) manage sign-up, mail transport, and rate limits via GET/PUT/DELETE /instance/settings or the new Settings → Instance tab. Secret values are AES-256-GCM encrypted at rest with PRIMORA_ENCRYPTION_KEY, never returned by the API, and decrypted by the auth service at send time. Auth service reads the same settings table for the sign-up gate and mail config, so in-app changes take effect without a restart. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
import { serve } from "@hono/node-server";
|
|
import { Hono } from "hono";
|
|
import { logger } from "hono/logger";
|
|
import { requestId } from "hono/request-id";
|
|
import { HTTPException } from "hono/http-exception";
|
|
import { createClient } from "redis";
|
|
|
|
import { auth, promoteAdminEmails, runAuthMigrations } from "./lib/auth.js";
|
|
import { authPool } from "./lib/db.js";
|
|
import { env } from "./lib/env.js";
|
|
|
|
const redisClient = createClient({
|
|
url: env.DRAGONFLY_URL,
|
|
});
|
|
|
|
try {
|
|
await redisClient.connect();
|
|
} catch (error) {
|
|
console.warn(JSON.stringify({ level: "warn", msg: "dragonfly unavailable", error }));
|
|
}
|
|
|
|
await retry("auth_migrations", runAuthMigrations);
|
|
await promoteAdminEmails();
|
|
|
|
const app = new Hono();
|
|
|
|
app.use("*", requestId());
|
|
app.use("*", logger());
|
|
|
|
app.use("/auth/*", async (c, next) => {
|
|
if (!redisClient.isOpen) {
|
|
await next();
|
|
return;
|
|
}
|
|
const identifier = c.req.header("x-forwarded-for")?.split(",")[0]?.trim() || c.req.header("x-real-ip") || "unknown";
|
|
const windowKey = `rate-limit:auth:${identifier}:${new Date().toISOString().slice(0, 16)}`;
|
|
const count = await redisClient.incr(windowKey);
|
|
await redisClient.expire(windowKey, 60);
|
|
if (count > 60) {
|
|
throw new HTTPException(429, { message: "Too many auth requests" });
|
|
}
|
|
await next();
|
|
});
|
|
|
|
app.get("/health", async (c) => {
|
|
let db = "ok";
|
|
let cache = redisClient.isOpen ? "ok" : "disabled";
|
|
try {
|
|
await authPool.query("select 1");
|
|
} catch (error) {
|
|
db = error instanceof Error ? error.message : "error";
|
|
}
|
|
if (redisClient.isOpen) {
|
|
try {
|
|
await redisClient.ping();
|
|
} catch (error) {
|
|
cache = error instanceof Error ? error.message : "error";
|
|
}
|
|
}
|
|
return c.json({
|
|
status: db === "ok" ? "ok" : "degraded",
|
|
checks: { database: db, dragonfly: cache },
|
|
});
|
|
});
|
|
|
|
app.on(["GET", "POST"], "/auth/*", (c) => auth.handler(c.req.raw));
|
|
|
|
serve({
|
|
fetch: app.fetch,
|
|
port: env.AUTH_PORT,
|
|
});
|
|
|
|
async function retry(label: string, fn: () => Promise<void>) {
|
|
let lastError: unknown;
|
|
for (let attempt = 1; attempt <= 20; attempt += 1) {
|
|
try {
|
|
await fn();
|
|
return;
|
|
} catch (error) {
|
|
lastError = error;
|
|
console.warn(JSON.stringify({ level: "warn", msg: `${label}_retry`, attempt, error }));
|
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
}
|
|
}
|
|
throw lastError;
|
|
}
|