Files
t5de-hub/apps/web/lib/oauth.ts
T
dhkatz 9112e3cc30
CI / test (pull_request) Successful in 1m29s
CI / image (apps/api/Dockerfile, t5de-hub-api) (pull_request) Skipped
CI / image (apps/web/Dockerfile, t5de-hub-web) (pull_request) Skipped
Pull request / title (pull_request) Successful in 1s
The hub as an OpenID Connect provider for T5DE Music
2026-11-19 01:51:05 -08:00

26 lines
1.5 KiB
TypeScript

// Signing in for another T5DE site (OpenID Connect, docs/music.md). The hub's authorize endpoint sends people who
// aren't signed in to /signin with the site's request and a signature; once they're signed in, this sends them back
// to the authorize endpoint with the same request, which then returns them to the site.
type Params = Record<string, string | string[] | undefined>;
/** The provider's own parameters on the sign-in page, not part of the site's request. */
const internal = new Set(["sig", "exp", "ba_iat", "ba_param", "next", "error"]);
/** Where to go once signed in, if the sign-in page was opened for a site's request (else undefined). */
export function continueURL(params: Params): string | undefined {
if (typeof params.sig !== "string" || typeof params.client_id !== "string") return undefined;
const q = new URLSearchParams();
for (const [k, v] of Object.entries(params)) {
if (internal.has(k) || v === undefined) continue;
for (const one of Array.isArray(v) ? v : [v]) q.append(k, one);
}
return `/api/auth/oauth2/authorize?${q}`;
}
/** The site's client ID, if the page was opened for one. */
export const requestingClient = (params: Params) => (typeof params.client_id === "string" ? params.client_id : undefined);
/** Whether the site asked to go straight to a sign-in provider (Music's "Sign in with Discord"). */
export const requestedProvider = (params: Params) => (typeof params.t5de_provider === "string" ? params.t5de_provider : undefined);