The hub as an OpenID Connect provider for T5DE Music
This commit is contained in:
1 parent
f05874f800
commit
9112e3cc30
20 files changed
+6531
-29
No files matched your search
@@ -0,0 +1,45 @@
|
||||
"use client";
|
||||
// Allow or deny a site's request. The provider's client plugin sends the page's signed request (oauth_query) along,
|
||||
// and answers with where to go next: back to the site, with a code or with access_denied.
|
||||
import { oauthProviderClient } from "@better-auth/oauth-provider/client";
|
||||
import { createAuthClient } from "better-auth/react";
|
||||
import { useState } from "react";
|
||||
import { ErrorText } from "@/components/forms";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
const consentClient = createAuthClient({
|
||||
baseURL: typeof window === "undefined" ? undefined : window.location.origin,
|
||||
plugins: [oauthProviderClient()],
|
||||
});
|
||||
|
||||
export function ConsentForm() {
|
||||
const [pending, setPending] = useState<"allow" | "deny" | null>(null);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
async function answer(accept: boolean) {
|
||||
setPending(accept ? "allow" : "deny");
|
||||
setError("");
|
||||
const { data, error } = await consentClient.oauth2.consent({ accept });
|
||||
const url = data && typeof data === "object" && "url" in data ? (data as { url?: string }).url : undefined;
|
||||
if (error || !url) {
|
||||
setError(error?.message ?? "That didn't work. Start again from the site.");
|
||||
setPending(null);
|
||||
return;
|
||||
}
|
||||
window.location.assign(url);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<div className="flex gap-2">
|
||||
<Button type="button" disabled={pending !== null} onClick={() => void answer(true)}>
|
||||
{pending === "allow" ? "Allowing…" : "Allow"}
|
||||
</Button>
|
||||
<Button type="button" variant="outline" disabled={pending !== null} onClick={() => void answer(false)}>
|
||||
{pending === "deny" ? "Denying…" : "Deny"}
|
||||
</Button>
|
||||
</div>
|
||||
{error && <ErrorText>{error}</ErrorText>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Another site asks to sign the person in with their T5DE account. T5DE's own sites (Music) are trusted and never get
|
||||
// here; this is for sites added later (docs/music.md, The OIDC provider).
|
||||
import type { Metadata } from "next";
|
||||
import { redirect } from "next/navigation";
|
||||
import { NarrowCard } from "@/components/narrow-card";
|
||||
import { api, getMe } from "@/lib/api";
|
||||
import { ConsentForm } from "./consent-form";
|
||||
|
||||
export const metadata: Metadata = { title: "Allow Access" };
|
||||
|
||||
/** What each scope lets a site see or do, in words. */
|
||||
const scopeText: Record<string, string> = {
|
||||
openid: "Know it's you",
|
||||
profile: "Your name and username",
|
||||
email: "Your email address",
|
||||
offline_access: "Stay signed in when you're away",
|
||||
imvu: "Your linked IMVU accounts",
|
||||
"imvu:link": "Link and unlink IMVU accounts for you",
|
||||
};
|
||||
|
||||
export default async function Consent({ searchParams }: { searchParams: Promise<Record<string, string | string[] | undefined>> }) {
|
||||
const params = await searchParams;
|
||||
const me = await getMe();
|
||||
if (!me) redirect("/signin");
|
||||
const clientId = typeof params.client_id === "string" ? params.client_id : "";
|
||||
const site = clientId ? (await api.oauth.clients({ clientId }).get()).data : undefined;
|
||||
const name = site && "client" in site ? site.client.name : undefined;
|
||||
if (!name) return <NarrowCard title="Allow Access" description="This request isn't valid any more. Start again from the site." />;
|
||||
const scopes = (typeof params.scope === "string" ? params.scope : "").split(" ").filter((s) => s in scopeText);
|
||||
return (
|
||||
<NarrowCard title="Allow Access" description={`${name} wants to use your T5DE account, ${me.username}.`}>
|
||||
<div className="space-y-4">
|
||||
<ul className="list-disc space-y-1 pl-5 text-sm">
|
||||
{scopes.map((s) => (
|
||||
<li key={s}>{scopeText[s]}</li>
|
||||
))}
|
||||
</ul>
|
||||
<ConsentForm />
|
||||
</div>
|
||||
</NarrowCard>
|
||||
);
|
||||
}
|
||||
@@ -6,28 +6,35 @@ import { NarrowCard } from "@/components/narrow-card";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import { api, getMe, unwrap } from "@/lib/api";
|
||||
import { discordError } from "@/lib/discord";
|
||||
import { continueURL, requestedProvider, requestingClient } from "@/lib/oauth";
|
||||
import { safeNext } from "@/lib/paths";
|
||||
import { SignInForm } from "./signin-form";
|
||||
|
||||
export const metadata: Metadata = { title: "Sign in" };
|
||||
|
||||
export default async function SignIn({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ next?: string | string[]; error?: string | string[] }>;
|
||||
}) {
|
||||
export default async function SignIn({ searchParams }: { searchParams: Promise<Record<string, string | string[] | undefined>> }) {
|
||||
const params = await searchParams;
|
||||
const next = safeNext(params.next);
|
||||
// Opened for another T5DE site (Music): back to it once signed in.
|
||||
const forSite = continueURL(params);
|
||||
const next = forSite ?? safeNext(params.next);
|
||||
if (await getMe()) redirect(next);
|
||||
const { discord } = unwrap(await api.hub.get());
|
||||
const clientId = requestingClient(params);
|
||||
const site = forSite && clientId ? (await api.oauth.clients({ clientId }).get()).data : undefined;
|
||||
const siteName = site && "client" in site ? site.client.name : undefined;
|
||||
const error = discordError(params.error);
|
||||
// The site asked for Discord (its "Sign in with Discord" button): go straight there, unless that just failed.
|
||||
const autoDiscord = discord && forSite !== undefined && requestedProvider(params) === "discord" && !error;
|
||||
return (
|
||||
<NarrowCard title="Sign in" description="to your T5DE account.">
|
||||
<NarrowCard
|
||||
title="Sign in"
|
||||
description={siteName ? `with your T5DE account to continue to ${siteName}.` : "to your T5DE account."}
|
||||
>
|
||||
<div className="space-y-6">
|
||||
{error && <ErrorText>{error}</ErrorText>}
|
||||
{discord && (
|
||||
<>
|
||||
<DiscordButton mode="sign-in" next={next} />
|
||||
<DiscordButton mode="sign-in" next={next} auto={autoDiscord} />
|
||||
<div className="flex items-center gap-3 text-xs text-muted-foreground">
|
||||
<Separator className="flex-1" />
|
||||
or with your username
|
||||
|
||||
@@ -18,6 +18,8 @@ export function SignInForm({ next }: { next: string }) {
|
||||
const [step, setStep] = useState<"password" | "code" | "backup">("password");
|
||||
|
||||
const done = () => {
|
||||
// Back to the hub's authorize endpoint (signing in for another T5DE site): a full page load, it's the API's.
|
||||
if (next.startsWith("/api/")) return window.location.assign(next);
|
||||
router.push(next);
|
||||
router.refresh();
|
||||
};
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
"use client";
|
||||
// Discord sign-in (a basic account the first time, docs/music.md) and, from the account page, linking Discord to a
|
||||
// member's account. Discord sends people back to the API, which sends them on to these addresses on this site.
|
||||
import { useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { ErrorText } from "@/components/forms";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
|
||||
export function DiscordButton({ mode, next = "/" }: { mode: "sign-in" | "link"; next?: string }) {
|
||||
/** auto: start at once (a T5DE site asked for Discord sign-in). */
|
||||
export function DiscordButton({ mode, next = "/", auto = false }: { mode: "sign-in" | "link"; next?: string; auto?: boolean }) {
|
||||
const [pending, setPending] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
@@ -29,6 +30,14 @@ export function DiscordButton({ mode, next = "/" }: { mode: "sign-in" | "link";
|
||||
}
|
||||
}
|
||||
|
||||
const started = useRef(false);
|
||||
useEffect(() => {
|
||||
if (!auto || started.current) return;
|
||||
started.current = true;
|
||||
void go();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [auto]);
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<Button type="button" variant={mode === "link" ? "outline" : "default"} className={mode === "link" ? undefined : "w-full"} disabled={pending} onClick={() => void go()}>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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);
|
||||
@@ -10,6 +10,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@base-ui/react": "^1.9.0",
|
||||
"@better-auth/oauth-provider": "1.7.26",
|
||||
"@elysiajs/eden": "1.4.9",
|
||||
"@t5de/api": "workspace:*",
|
||||
"better-auth": "1.7.26",
|
||||
|
||||
Reference in new issue
Block a user