~james/core

core/web/src/lib/api/tranquil.ts -rw-r--r-- 6.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import * as v from "@atcute/lexicons/validations";
import type { Did, Handle } from "@atcute/lexicons/syntax";
import type { OAuthUserAgent } from "@atcute/oauth-browser-client";
import { buildUrl, toResponseError } from "#lib/api/_request.ts";
import { nsidOf, proxyPathOf, type XrpcPath } from "#lib/api/syntax.ts";
import type { QueryValue, XrpcRequestInit } from "#lib/api/client.ts";

export const CREATE_DELEGATE_NSID = nsidOf("farm.tranquil.delegation.createAccount");
export const LIST_DELEGATES_NSID = nsidOf("farm.tranquil.delegation.listControlledAccounts");

export const LIST_DELEGATES_ENDPOINT = proxyPathOf("_delegation.listControlledAccounts");
export const CREATE_DELEGATE_ENDPOINT = proxyPathOf("_delegation.createDelegatedAccount");

const delegatedAccountSchema = v.object({
	did: v.didString(),
	grantedAt: v.datetimeString(),
	grantedScopes: v.string(),
	handle: v.optional(v.handleString())
});

export const listDelegatesSchema = v.object({
	get accounts() {
		return v.array(delegatedAccountSchema);
	}
});

export type DelegatedAccountInfo = v.InferInput<typeof delegatedAccountSchema>;

export interface CreateDelegatedAccountInput {
	/** Full handle or local handle prefix. */
	handle: string;
	/** Scopes granted to the initial controller. */
	controllerScopes: string;
	email?: string;
}

export interface CreateDelegatedAccountOutput {
	did: Did;
	handle: Handle;
}

// agent.handle resolves a relative path against the session's PDS, so only
// the path and query of the built URL are kept; the origin is a placeholder.
const pathFor = (nsid: XrpcPath, params?: Record<string, QueryValue>): string => {
	const url = buildUrl("http://pds.invalid", nsid, params);
	return `${url.pathname}${url.search}`;
};

const tranquilGet = async <T>(
	agent: OAuthUserAgent,
	nsid: XrpcPath,
	params?: Record<string, QueryValue>,
	init?: XrpcRequestInit
): Promise<T> => {
	const headers = new Headers(init?.headers);
	if (!headers.has("accept")) headers.set("accept", "application/json");

	const response = await agent.handle(pathFor(nsid, params), {
		headers,
		signal: init?.signal
	});
	if (!response.ok) throw await toResponseError(response);
	return (await response.json()) as T;
};

const tranquilPost = async <T>(
	agent: OAuthUserAgent,
	nsid: XrpcPath,
	body?: unknown,
	init?: XrpcRequestInit
): Promise<T> => {
	const headers = new Headers(init?.headers);
	if (!headers.has("content-type")) headers.set("content-type", "application/json");
	if (!headers.has("accept")) headers.set("accept", "application/json");

	const response = await agent.handle(pathFor(nsid), {
		method: "POST",
		headers,
		body: body === undefined ? undefined : JSON.stringify(body),
		signal: init?.signal
	});
	if (!response.ok) throw await toResponseError(response);
	return (await response.json()) as T;
};

// ---- shared types, mirrored from tranquil-pds's delegation api ----

export type DelegationScope = string;

export interface ScopePreset {
	name: string;
	label: string;
	description: string;
	scopes: DelegationScope;
}

export interface ControllerInfo {
	did: Did;
	handle?: Handle;
	grantedScopes: DelegationScope;
	grantedAt: string;
	isActive: boolean;
	isLocal: boolean;
}

export type DelegationActionType =
	| "GrantCreated"
	| "GrantRevoked"
	| "ScopesModified"
	| "TokenIssued"
	| "RepoWrite"
	| "BlobUpload"
	| "AccountAction";

export interface AuditLogEntry {
	id: string;
	delegatedDid: Did;
	actorDid: Did;
	controllerDid?: Did;
	actionType: DelegationActionType;
	actionDetails?: unknown;
	createdAt: string;
}

export interface ResolvedIdentity {
	did: Did;
	handle?: Handle;
	pdsUrl?: string;
	isLocal: boolean;
}

// ---- controllers on *this* account ----

export const listControllers = (
	agent: OAuthUserAgent,
	init?: XrpcRequestInit
): Promise<ControllerInfo[]> =>
	tranquilGet<{ controllers: ControllerInfo[] }>(
		agent,
		proxyPathOf("_delegation.listControllers"),
		undefined,
		init
	).then((r) => r.controllers);

export const addController = (
	agent: OAuthUserAgent,
	input: { controllerDid: Did; grantedScopes: DelegationScope },
	init?: XrpcRequestInit
): Promise<{ success: boolean }> =>
	tranquilPost(
		agent,
		proxyPathOf("_delegation.addController"),
		{ controller_did: input.controllerDid, granted_scopes: input.grantedScopes },
		init
	);

export const removeController = (
	agent: OAuthUserAgent,
	controllerDid: Did,
	init?: XrpcRequestInit
): Promise<{ success: boolean }> =>
	tranquilPost(
		agent,
		proxyPathOf("_delegation.removeController"),
		{ controller_did: controllerDid },
		init
	);

export const updateControllerScopes = (
	agent: OAuthUserAgent,
	input: { controllerDid: Did; grantedScopes: DelegationScope },
	init?: XrpcRequestInit
): Promise<{ success: boolean }> =>
	tranquilPost(
		agent,
		proxyPathOf("_delegation.updateControllerScopes"),
		{ controller_did: input.controllerDid, granted_scopes: input.grantedScopes },
		init
	);

// ---- accounts this account controls ----

export const listControlledAccounts = (
	agent: OAuthUserAgent,
	init?: XrpcRequestInit
): Promise<DelegatedAccountInfo[]> =>
	tranquilGet<{ accounts: DelegatedAccountInfo[] }>(
		agent,
		LIST_DELEGATES_ENDPOINT,
		undefined,
		init
	).then((r) => r.accounts);

export const createDelegatedAccount = (
	agent: OAuthUserAgent,
	input: CreateDelegatedAccountInput,
	init?: XrpcRequestInit
): Promise<CreateDelegatedAccountOutput> =>
	tranquilPost(agent, CREATE_DELEGATE_ENDPOINT, input, init);

// ---- audit log, scope presets, identity resolution ----

export const getAuditLog = (
	agent: OAuthUserAgent,
	params?: { limit?: number; offset?: number },
	init?: XrpcRequestInit
): Promise<{ entries: AuditLogEntry[]; total: number }> =>
	tranquilGet(
		agent,
		proxyPathOf("_delegation.getAuditLog"),
		params as Record<string, QueryValue>,
		init
	);

export const getScopePresets = (
	agent: OAuthUserAgent,
	init?: XrpcRequestInit
): Promise<ScopePreset[]> =>
	tranquilGet<{ presets: ScopePreset[] }>(
		agent,
		proxyPathOf("_delegation.getScopePresets"),
		undefined,
		init
	).then((r) => r.presets);

export const resolveController = (
	agent: OAuthUserAgent,
	identifier: string,
	init?: XrpcRequestInit
): Promise<ResolvedIdentity> =>
	tranquilGet(agent, proxyPathOf("_delegation.resolveController"), { identifier }, init);