This repository has no description
1import { Client, ok } from '@atcute/client';
2import { mainSchema as getServiceAuthSchema } from '@atcute/atproto/types/server/getServiceAuth';
3import type { Nsid } from '@atcute/lexicons/syntax';
4import type { OAuthUserAgent } from '@atcute/oauth-browser-client';
5
6export const createClient = (agent: OAuthUserAgent): Client => new Client({ handler: agent });
7
8export interface ServiceAuthOptions {
9 // service did for the jwt aud claim
10 aud: string;
11 lxm: string;
12 // clamped to at least 60s, matching appview's service client.
13 expiresInSeconds?: number;
14}
15
16// did:web service id, with ports percent-encoded like serviceauth didweb.
17export const serviceDidForHost = (host: string): string => `did:web:${host.replace(/:/g, '%3A')}`;
18
19// mint a service-auth jwt for knot/spindle xrpc calls.
20export const mintServiceAuth = async (
21 agent: OAuthUserAgent,
22 { aud, lxm, expiresInSeconds = 60 }: ServiceAuthOptions
23): Promise<string> => {
24 const client = createClient(agent);
25 const exp = Math.floor(Date.now() / 1000) + Math.max(expiresInSeconds, 60);
26 const { token } = await ok(
27 client.call(getServiceAuthSchema, {
28 params: { aud, exp, lxm: lxm as Nsid }
29 })
30 );
31 return token;
32};