commit
Let proxyBot switch the front with a shared bot token
Co-Authored-By: Sandrone <[email protected]>
diff --git a/src/lib/server/plural/middleware/auth.ts b/src/lib/server/plural/middleware/auth.ts
index ca5311b..b9ed5c2 100644
--- a/src/lib/server/plural/middleware/auth.ts
+++ b/src/lib/server/plural/middleware/auth.ts
@@ -12,7 +12,8 @@
import type { Context, Next } from "hono";
import type { Env } from "../hono";
-import { decodeAccessToken } from "../security";
+import { constantTimeStringEqual, decodeAccessToken } from "../security";
+import { rt } from "../runtime";
import { HttpError } from "../errors";
import { getUserByUsername } from "../services/users";
import { currentSsoSession } from "../services/sso_sessions";
@@ -60,6 +61,19 @@ export async function requireAuth(c: Context<Env>, next: Next): Promise<void> {
await next();
}
+// For routes the bot may call: its token passes, anyone else needs a normal sign-in
+export async function requireAuthOrBot(c: Context<Env>, next: Next): Promise<void> {
+ const botToken = rt().env.BOT_TOKEN;
+ const token = extractBearer(c);
+
+ if (botToken && token && constantTimeStringEqual(token, botToken)) {
+ await next();
+ return;
+ }
+
+ await requireAuth(c, next);
+}
+
/** Attach user if authenticated, otherwise leave undefined. Never rejects. */
export async function optionalAuth(c: Context<Env>, next: Next): Promise<void> {
try {
diff --git a/src/lib/server/plural/routes/fronting.ts b/src/lib/server/plural/routes/fronting.ts
index a3ac5b2..fcfcf61 100644
--- a/src/lib/server/plural/routes/fronting.ts
+++ b/src/lib/server/plural/routes/fronting.ts
@@ -11,7 +11,7 @@ import { FrontersSchema, MessageSchema, SwitchSchema } from "../schemas";
import { deleteSwitch, getFronters, getMembers, getSwitches, setFront } from "../services/system";
import { enrichMembersWithTags } from "../services/tags";
import { enrichMembersWithStatus } from "../services/status";
-import { requireAuth, requireAdmin } from "../middleware/auth";
+import { requireAuth, requireAuthOrBot, requireAdmin } from "../middleware/auth";
import { broadcastFrontingUpdate } from "../ws";
import { HttpError } from "../errors";
import type { JsonObject } from "../types";
@@ -91,9 +91,9 @@ frontingRoutes.openapi(
method: "post",
path: "/switch",
tags: ["Fronting"],
- summary: "Set the front. An empty list means switch-out",
+ summary: "Set the front. An empty list means switch-out. proxyBot's token works here too",
security: signedIn,
- middleware: [requireAuth] as const,
+ middleware: [requireAuthOrBot] as const,
request: jsonBody(z.object({ members: z.array(z.string()).default([]) })),
responses: ok(MessageSchema),
}),
diff --git a/src/lib/server/plural/types.ts b/src/lib/server/plural/types.ts
index c072ffe..c454c8f 100644
--- a/src/lib/server/plural/types.ts
+++ b/src/lib/server/plural/types.ts
@@ -16,6 +16,8 @@ export interface PluralEnv {
JWT_SECRET?: string;
SSO_CLIENT_ID?: string;
SSO_CLIENT_SECRET?: string;
+ // Shared with proxyBot so it can switch without an SSO sign-in
+ BOT_TOKEN?: string;
// Vars
SSO_ISSUER?: string;