Our Personal Data Server from scratch!
tranquil.farm
oauth
atproto
pds
rust
postgresql
objectstorage
fun
1import type {
2 AccessToken,
3 Did,
4 EmailAddress,
5 Handle,
6 ScopeSet,
7} from "./types/branded.ts";
8import type { Session } from "./types/api.ts";
9import type {
10 DelegationAuditEntry,
11 DelegationControlledAccount,
12 DelegationController,
13 DelegationScopePreset,
14 SsoLinkedAccount,
15} from "./types/api.ts";
16import { api, ApiError } from "./api.ts";
17import type { Result } from "./types/result.ts";
18
19export interface AuthenticatedClient {
20 readonly token: AccessToken;
21 readonly session: Session;
22
23 getSsoLinkedAccounts(): Promise<{ accounts: SsoLinkedAccount[] }>;
24
25 listDelegationControllers(): Promise<
26 Result<{ controllers: DelegationController[] }, ApiError>
27 >;
28 listDelegationControlledAccounts(): Promise<
29 Result<{ accounts: DelegationControlledAccount[] }, ApiError>
30 >;
31 getDelegationScopePresets(): Promise<
32 Result<{ presets: DelegationScopePreset[] }, ApiError>
33 >;
34 addDelegationController(
35 controllerDid: Did,
36 grantedScopes: ScopeSet,
37 ): Promise<Result<{ success: boolean }, ApiError>>;
38 removeDelegationController(
39 controllerDid: Did,
40 ): Promise<Result<{ success: boolean }, ApiError>>;
41 createDelegatedAccount(
42 handle: Handle,
43 email?: EmailAddress,
44 controllerScopes?: ScopeSet,
45 ): Promise<Result<{ did: Did; handle: Handle }, ApiError>>;
46 getDelegationAuditLog(
47 limit: number,
48 offset: number,
49 ): Promise<
50 Result<{ entries: DelegationAuditEntry[]; total: number }, ApiError>
51 >;
52
53 exportBlobs(): Promise<Blob>;
54}
55
56export function createAuthenticatedClient(
57 session: Session,
58): AuthenticatedClient {
59 const token = session.accessJwt;
60
61 return {
62 token,
63 session,
64
65 getSsoLinkedAccounts: () => api.getSsoLinkedAccounts(token),
66
67 listDelegationControllers: () => api.listDelegationControllers(token),
68 listDelegationControlledAccounts: () =>
69 api.listDelegationControlledAccounts(token),
70 getDelegationScopePresets: () => api.getDelegationScopePresets(),
71 addDelegationController: (controllerDid, grantedScopes) =>
72 api.addDelegationController(token, controllerDid, grantedScopes),
73 removeDelegationController: (controllerDid) =>
74 api.removeDelegationController(token, controllerDid),
75 createDelegatedAccount: (handle, email, controllerScopes) =>
76 api.createDelegatedAccount(token, handle, email, controllerScopes),
77 getDelegationAuditLog: (limit, offset) =>
78 api.getDelegationAuditLog(token, limit, offset),
79
80 exportBlobs: () => api.exportBlobs(token),
81 };
82}
83
84export { ApiError };