feat(accounts): the hub as an OpenID Connect provider for T5DE Music (#23)
Second half of docs/music.md's phase 1: the hub as an OAuth 2.1 / OpenID Connect provider, with T5DE Music as its first client. Builds on #22 (merged). **Provider** (`@better-auth/oauth-provider` 1.7.26): issuer `API_URL/api/auth` (prod: `https://hub.t5de.dev/api/auth`), discovery at `/api/auth/.well-known/openid-configuration`, keys from the existing JWT plugin (Ed25519). The JWT plugin's `/token` route is off (nothing used it). Seven tables (migration `0022_oauth_provider`). - **Music** (`t5de-music`) is made at startup from `MUSIC_CLIENT_SECRET` and `MUSIC_REDIRECT_URIS`: trusted (no consent screen), confidential, PKCE required, HTTPS redirect URIs only (plain HTTP on localhost in development). Its secret is stored as a SHA-256 hash the hub supplies (`storeClientSecret`), so seeding and the plugin agree. Dynamic client registration stays off. - **Scopes**: `openid profile email offline_access`, `imvu` (the person's linked IMVU accounts, real IDs and names, in the ID token and userinfo: first-party sites only, as the plan says) and `imvu:link`. - **The hub's API as a resource**: Music asks for `resource=API_URL/api/v1` and gets a JWT access token (`at+jwt`, that audience). The API checks it against its own keys. Routes for sites (`linking`): `GET /api/v1/account/imvu` and the existing link routes (start, check, cancel, unlink), for any account including basic ones. Every other route answers such a token 403 `token_not_allowed`, and a token without `imvu:link` 403 `insufficient_scope`. **Website**: `/signin` opened by authorize ("Sign in with your T5DE account to continue to T5DE Music") sends people back to authorize once signed in, by password or Discord. `t5de_provider=discord` in Music's request starts Discord at once, so "Sign in with Discord" on Music goes straight to Discord. `/consent` for sites added later. `GET /api/v1/oauth/clients/:id` names the site. Second half of docs/music.md's phase 1: the hub as an OAuth 2.1 / OpenID Connect provider, with T5DE Music as its first client. Builds on #22 (merged). **Provider** (`@better-auth/oauth-provider` 1.7.26): issuer `API_URL/api/auth` (prod: `https://hub.t5de.dev/api/auth`), discovery at `/api/auth/.well-known/openid-configuration`, keys from the existing JWT plugin (Ed25519). The JWT plugin's `/token` route is off (nothing used it). Seven tables (migration `0022_oauth_provider`). - **Music** (`t5de-music`) is made at startup from `MUSIC_CLIENT_SECRET` and `MUSIC_REDIRECT_URIS`: trusted (no consent screen), confidential, PKCE required, HTTPS redirect URIs only (plain HTTP on localhost in development). Its secret is stored as a SHA-256 hash the hub supplies (`storeClientSecret`), so seeding and the plugin agree. Dynamic client registration stays off. - **Scopes**: `openid profile email offline_access`, `imvu` (the person's linked IMVU accounts, real IDs and names, in the ID token and userinfo: first-party sites only, as the plan says) and `imvu:link`. - **The hub's API as a resource**: Music asks for `resource=API_URL/api/v1` and gets a JWT access token (`at+jwt`, that audience). The API checks it against its own keys. Routes for sites (`linking`): `GET /api/v1/account/imvu` and the existing link routes (start, check, cancel, unlink), for any account including basic ones. Every other route answers such a token 403 `token_not_allowed`, and a token without `imvu:link` 403 `insufficient_scope`. **Website**: `/signin` opened by authorize ("Sign in with your T5DE account to continue to T5DE Music") sends people back to authorize once signed in, by password or Discord. `t5de_provider=discord` in Music's request starts Discord at once, so "Sign in with Discord" on Music goes straight to Discord. `/consent` for sites added later. `GET /api/v1/oauth/clients/:id` names the site. Tests: `apps/api/test/oidc.test.ts` runs Music's whole flow against the API (discovery, sign-in redirect with the hint, code + PKCE, token, ID token and userinfo claims, the access token on link routes and refused elsewhere, scopes, a basic account linking through Music). Full suite 75/75; `check-types` and `build` pass. Not checked in a browser yet. **Not in this PR** (later phases): merging a basic account into a member (`user.merged`), pairwise subjects and the console for third-party clients, `@imvu/verify` in place of the hub's link code. Deploying: `MUSIC_CLIENT_SECRET` (32+ random characters, shared with Music's stack) and `MUSIC_REDIRECT_URIS=https://music.t5de.dev/api/auth/callback` in the hub stacks (dhkatz/infra), with Music's own stack. Tests: `apps/api/test/oidc.test.ts` runs Music's whole flow against the API (discovery, sign-in redirect with the hint, code + PKCE, token, ID token and userinfo claims, the access token on link routes and refused elsewhere, scopes, a basic account linking through Music). Full suite 75/75; `check-types` and `build` pass. Not checked in a browser yet. **Not in this PR** (later phases): merging a basic account into a member (`user.merged`), pairwise subjects and the console for third-party clients, `@imvu/verify` in place of the hub's link code. Deploying: `MUSIC_CLIENT_SECRET` (32+ random characters, shared with Music's stack) and `MUSIC_REDIRECT_URIS=https://music.t5de.dev/api/auth/callback` in the hub stacks (dhkatz/infra), with Music's own stack. Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #23.
This commit is contained in:
1 parent
f05874f800
commit
df42b7833a
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>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user