Graphical PDS migrator for AT Protocol
1import { AtprotoOAuthClient } from "@bigmoves/atproto-oauth-client";
2import { SessionStore, StateStore } from "../storage.ts";
3
4const isDev = Deno.env.get("NODE_ENV") !== "production";
5export const scope = [
6 "atproto",
7 "account:email",
8 "account:status?action=manage",
9 "identity:*",
10 "rpc:*?aud=did:web:api.bsky.app#bsky_appview",
11 "rpc:com.atproto.server.createAccount?aud=*",
12].join(" ");
13const publicUrl = Deno.env.get("PUBLIC_URL");
14const url = publicUrl || `http://127.0.0.1:8000`;
15export const clientId = publicUrl
16 ? `${url}/oauth-client-metadata.json`
17 : `http://localhost?redirect_uri=${
18 encodeURIComponent(`${url}/api/oauth/callback`)
19 }&scope=${encodeURIComponent(scope)}`;
20console.log(`ClientId: ${clientId}`);
21
22/**
23 * Create the OAuth client.
24 * @param db - The Deno KV instance for the database
25 * @returns The OAuth client
26 */
27export const createClient = (db: Deno.Kv) => {
28 if (Deno.env.get("NODE_ENV") == "production" && !Deno.env.get("PUBLIC_URL")) {
29 throw new Error("PUBLIC_URL is not set");
30 }
31
32 return new AtprotoOAuthClient({
33 clientMetadata: {
34 client_name: "Statusphere React App",
35 client_id: clientId,
36 client_uri: url,
37 redirect_uris: [`${url}/api/oauth/callback`],
38 scope: scope,
39 grant_types: ["authorization_code", "refresh_token"],
40 response_types: ["code"],
41 application_type: "web",
42 token_endpoint_auth_method: "none",
43 dpop_bound_access_tokens: true,
44 },
45 stateStore: new StateStore(db),
46 sessionStore: new SessionStore(db),
47 didCache: undefined,
48 allowHttp: isDev,
49 plcDirectoryUrl: Deno.env.get("PLC_URL") ?? "https://plc.directory",
50 });
51};
52
53const kv = await Deno.openKv();
54export const oauthClient = createClient(kv);