atprotoClient

tcmts.atproto.atprotoClient · #zgrkgrzm · function · runs anywhere

An app's OAuth client document, to serve at client (a route of the app's http namespace: export const client = atprotoClient({ … }) in <app>.oauth, served with tcm start --http <app>.oauth=/oauth): what an AT Protocol server reads to know the app that's asking. A public client with DPoP, coming back to the app's home page. On localhost no document is needed: the browser signs in as AT Protocol's loopback client.

import { askedScope } from "#codebase/tcmts/atproto";
import type { SignInOptions } from "#codebase/tcmts/atproto";

export function atprotoClient(
  o: SignInOptions & { name: string; logo?: string },
): (request: Request) => Record<string, unknown> {
  const path = o.client ?? "/oauth/client";
  return (req) => {
    const url = new URL(req.url);
    const home = `${url.origin}${url.pathname.endsWith(path) ? url.pathname.slice(0, -path.length) : ""}/`;
    return {
      client_id: `${url.origin}${url.pathname}`,
      client_name: o.name,
      client_uri: home,
      ...(o.logo ? { logo_uri: o.logo } : {}),
      redirect_uris: [home],
      scope: askedScope(o),
      grant_types: ["authorization_code", "refresh_token"],
      response_types: ["code"],
      token_endpoint_auth_method: "none",
      application_type: "web",
      dpop_bound_access_tokens: true,
    };
  };
}

uses

used by