A remote user is name@host here, in users.username, in paths (/@alice@music.example) and in mentions, through one handle rule in shared/validation.ts that paths, MENTION_RE and Markdown all build on; Markdown undoes GFM's email autolink of a handle. Their name, email and avatar come from their home at each sign-in and by its signed events (federation_outbox, delivered by the federation job with backoff): a rename at home leaves the old handle an alias here, and a password change or a withdrawn grant ends their sessions here. Their password, email, avatar and name can't change here (assertLocalUser), nor can they sign in elsewhere from here. An admin keeps the trusted and blocked instances on /manage (federation:manage): a trusted instance's users sign in from a button on the login page, a blocked one's can't and are signed out at once, and any other's join with an invite from the register page. The /authorize page asks the user once per instance, and /settings lists the instances their account signs in to, each disconnectable; /login?next= brings a signed-out visitor back to /authorize. Emails stay unique among local accounts only, and migration 0024 adds the tables, makes that index partial and reserves authorize and federation as category slugs. remote/gerbeur-instance.ts is the only way to other instances: https and public addresses only, no redirects, no certificate fallback, unless GERBEUR_FEDERATION_ALLOW_PRIVATE lets two instances on one machine talk. 298 backend tests and 702 frontend tests pass, and deno check, deno fmt, deno lint, tsc, eslint, doc:lint and i18n:compile --strict are clean; checked end to end with two built instances on one machine: sign-in through the other's login and consent pages, consent skipped once granted and asked again after disconnecting, a rename and a password change at home reaching the other, and blocking; screenshots of the login, consent, profile, settings and manage pages at 1280 and 390px. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
120 lines
4.0 KiB
TypeScript
120 lines
4.0 KiB
TypeScript
/**
|
|
* Renders the way in for users of another gerbeur instance, under the login
|
|
* form and, with the invite, under the register form.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { t } from "@lingui/core/macro";
|
|
import { Trans } from "@lingui/react/macro";
|
|
import { FormProvider } from "react-hook-form";
|
|
|
|
import { API_URL } from "../../config/api.ts";
|
|
import type { Wire } from "../../../shared/api.ts";
|
|
import type {
|
|
FederationLoginRequest,
|
|
FederationLoginResponse,
|
|
FederationPeer,
|
|
} from "../../../shared/federation.ts";
|
|
import { dataOr, expectOk } from "../../utils/shared/api.ts";
|
|
import { deserializeFederationPeer } from "../../utils/shared/deserialize.ts";
|
|
import { useApiForm } from "../../hooks/form/useApiForm.ts";
|
|
import { FormError } from "../form/FormError.tsx";
|
|
import { SubmitButton } from "../form/SubmitButton.tsx";
|
|
import { TextField } from "../form/TextField.tsx";
|
|
|
|
/** Passed to {@link FederatedSignIn} by `UserLogin` and `UserRegister`. */
|
|
export interface FederatedSignInProps {
|
|
/**
|
|
* The invite the register page was opened with, which lets a user of any
|
|
* instance join; the login page has none, and offers the trusted instances.
|
|
*/
|
|
inviteToken?: string;
|
|
}
|
|
|
|
interface Values {
|
|
instance: string;
|
|
}
|
|
|
|
/**
|
|
* Renders a button per trusted instance (`GET /api/federation/peers`) and a
|
|
* field for any other's address. Either starts the sign-in
|
|
* (`POST /api/federation/logins`) and leaves for that instance's `/authorize`
|
|
* page, which sends the user back to `/federation/callback`.
|
|
*/
|
|
export function FederatedSignIn(
|
|
{ inviteToken }: FederatedSignInProps,
|
|
): React.JSX.Element {
|
|
const [peers, setPeers] = useState<FederationPeer[]>([]);
|
|
const form = useApiForm<Values>({ defaultValues: { instance: "" } });
|
|
|
|
useEffect(() => {
|
|
if (inviteToken) return;
|
|
const controller = new AbortController();
|
|
fetch(`${API_URL}/api/federation/peers`, { signal: controller.signal })
|
|
.then((res) => dataOr<Wire<FederationPeer>[]>(res, []))
|
|
.then((list) => setPeers(list.map(deserializeFederationPeer)))
|
|
.catch(() => {});
|
|
return () => controller.abort();
|
|
}, [inviteToken]);
|
|
|
|
const onSubmit = form.submit(async ({ instance }) => {
|
|
const { authorizeUrl } = await expectOk<FederationLoginResponse>(
|
|
await fetch(`${API_URL}/api/federation/logins`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(
|
|
{
|
|
instance: instance.trim(),
|
|
inviteToken,
|
|
} satisfies FederationLoginRequest,
|
|
),
|
|
}),
|
|
);
|
|
globalThis.location.assign(authorizeUrl);
|
|
});
|
|
|
|
return (
|
|
<div className="auth-federated">
|
|
<p className="auth-federated-title">
|
|
{inviteToken
|
|
? <Trans>Or join with your account on another gerbeur</Trans>
|
|
: <Trans>Or log in with your account on another gerbeur</Trans>}
|
|
</p>
|
|
<FormProvider {...form}>
|
|
{peers.map((peer) => (
|
|
<button
|
|
key={peer.issuer}
|
|
type="button"
|
|
className="btn-secondary auth-federated-peer"
|
|
disabled={form.formState.isSubmitting}
|
|
onClick={() => {
|
|
form.setValue("instance", peer.issuer);
|
|
onSubmit();
|
|
}}
|
|
>
|
|
<Trans>Log in with {peer.name ?? peer.host}</Trans>
|
|
<span className="auth-federated-host">{peer.host}</span>
|
|
</button>
|
|
))}
|
|
<form onSubmit={onSubmit} className="form">
|
|
<FormError title={t`Couldn't reach that instance`} />
|
|
<TextField<Values>
|
|
name="instance"
|
|
placeholder={t`Its address, like music.example`}
|
|
autoComplete="url"
|
|
rules={{ required: true }}
|
|
/>
|
|
<SubmitButton
|
|
className="btn-secondary"
|
|
pendingLabel={<Trans>Connecting…</Trans>}
|
|
>
|
|
<Trans>Continue</Trans>
|
|
</SubmitButton>
|
|
</form>
|
|
</FormProvider>
|
|
</div>
|
|
);
|
|
}
|