g1t/packages/contracts/src/identity.ts

719 lines29,496 bytesCodeBlame
1import type { AccessClient, BasePermission, RepoGrant } from "./access";
2import type { Acting, CreateRunCredentialInput, RunBinding } from "./audit";
3import type { RepoPath } from "./repos";
4import type { Result } from "./result";
5
6export type User = {
7 id: string;
8 username: string;
9 /**
10 * `workspace` when a workspace is acting through one of its own access
11 * tokens: `id` is then the workspace's and `username` its slug. `system`
12 * is g1t itself doing platform work, such as a security update
13 * (`username` `g1t`). Absent means `user`.
14 */
15 kind?: "user" | "workspace" | "agent" | "system";
16 /**
17 * Whether the account's email address is confirmed. Only set on users
18 * resolved from credentials; unverified accounts cannot change anything.
19 */
20 verified?: boolean;
21 /**
22 * The workspaces this user belongs to. Set on users resolved from
23 * credentials, so any service can authorize from it.
24 */
25 workspaces?: Membership[];
26 /**
27 * The person's uploaded avatar: the SHA-256 of its bytes, served at
28 * `/avatars/<avatar>`. Only set on the signed-in person; absent means
29 * the generated letter avatar.
30 */
31 avatar?: string;
32 /**
33 * Set on an agent resolved from its token: who it acts for ("g1t
34 * on behalf of syntaqx"), with which credential, and what it may do.
35 */
36 acting?: Acting;
37 /**
38 * The repositories this user has a role on directly, whether or not they
39 * belong to its workspace. Set with `workspaces`; see `access.ts`.
40 */
41 grants?: RepoGrant[];
42 /**
43 * Set on a user resolved from an access token: its scopes (null for full
44 * access) and the workspaces or repositories it reaches. See scopes.ts.
45 */
46 token?: {
47 token_id: string;
48 scopes?: string[] | null;
49 legacy?: boolean;
50 };
51};
52
53/** What a member may do: an owner also manages the workspace's members. */
54export type Role = "owner" | "member";
55
56export type Membership = {
57 /** The workspace's name in URLs: `g1t.sh/<slug>`. */
58 slug: string;
59 role: Role;
60 /** Its display name. Set on users resolved from credentials. */
61 name?: string;
62 /** Its uploaded icon, as `Workspace.avatar`. */
63 avatar?: string;
64 /** The workspace's base permission: what members get on every repository. Absent means `write`. */
65 base_permission?: BasePermission;
66};
67
68/** How long an old workspace slug redirects, and stays reserved for it, after a rename. */
69export const SLUG_HOLD_DAYS = 90;
70
71/** How long a workspace must wait between renames. */
72export const RENAME_COOLDOWN_HOURS = 24;
73
74/** The largest avatar that can be uploaded, in bytes. */
75export const MAX_AVATAR_BYTES = 1024 * 1024;
76
77/**
78 * Where a workspace keeps its repositories' git data: anywhere g1t stores
79 * it (the default), or in the EU only. It applies to repositories made
80 * after it is set.
81 */
82export type DataResidency = "anywhere" | "eu";
83
84/**
85 * A workspace: the owner of repositories, and the first segment of their
86 * URLs. A person's own space and a team's are the same thing.
87 */
88export type Workspace = {
89 id: string;
90 slug: string;
91 name: string;
92 /** One line saying what the workspace is for. */
93 description: string | null;
94 /** RFC 3339. */
95 createdAt: string;
96 memberCount: number;
97 /**
98 * The workspace's uploaded icon: the SHA-256 of its bytes, served at
99 * `/avatars/<avatar>`. Null means the generated letter avatar.
100 */
101 avatar: string | null;
102 /** What every member gets on each repository; owners have Admin. */
103 basePermission?: BasePermission;
104};
105
106export type Member = {
107 username: string;
108 role: Role;
109 /** Their display name, when they set one. */
110 name?: string | null;
111 /** Their uploaded avatar's hash, served at `/avatars/<avatar>`; null for the generated letter avatar. */
112 avatar?: string | null;
113};
114
115/** An owner of a workspace, as staff see them. */
116export type AdminOwner = { username: string; email: string | null };
117
118/** A workspace as staff see it. Mirrors `AdminWorkspace` in `crates/contracts/src/identity.rs`. */
119export type AdminWorkspace = {
120 slug: string;
121 name: string;
122 /** RFC 3339. */
123 createdAt: string;
124 owners: AdminOwner[];
125 memberCount: number;
126};
127
128/** A member of a workspace, as staff see them. */
129export type AdminMember = { username: string; email: string | null; role: Role; /** RFC 3339. */ joined: string };
130
131export type AdminWorkspaceDetail = {
132 slug: string;
133 name: string;
134 description: string | null;
135 /** RFC 3339. */
136 createdAt: string;
137 /** Owners first, then by username. */
138 members: AdminMember[];
139 /** It can never be deleted, by anyone (identity's `PROTECTED_WORKSPACES`). */
140 protected: boolean;
141};
142
143/** The most workspaces one `workspaces` call returns. */
144export const ADMIN_WORKSPACES_LIMIT = 500;
145
146/**
147 * Whether anyone may make an account, or only someone with an invite.
148 * Identity's `REGISTRATION_MODE`; unset means `invite`.
149 */
150export type RegistrationMode = "invite" | "open";
151
152/** How many invites a person may have out at once, unless identity's `INVITES_PER_USER` says otherwise. */
153export const INVITES_PER_USER = 5;
154/** How long an invite works, unless identity's `INVITE_TTL_DAYS` says otherwise. */
155export const INVITE_TTL_DAYS = 30;
156
157/** Only a pending invite can be used or revoked. Revoked and expired ones never used give the invite back. */
158export type InviteStatus = "pending" | "redeemed" | "expired" | "revoked";
159
160/** One invite. Mirrors `Invite` in `crates/contracts/src/identity.rs`. */
161export type Invite = {
162 id: string;
163 /** `g1t-k7m2-…`: returned when it is made, and to its maker while pending. */
164 code: string | null;
165 /** The code's first group, such as `g1t-k7m2`. */
166 hint: string;
167 /** Only this address can use it. */
168 email: string | null;
169 /** `account` makes an account; `workspace` joins an existing one to `workspace`. */
170 kind: "account" | "workspace";
171 /** The workspace using it joins. */
172 workspace: string | null;
173 status: InviteStatus;
174 /** Whose allowance it used. */
175 chargedTo: "user" | "workspace" | "none";
176 /** Its maker's username; null when g1t staff made it. */
177 invitedBy: string | null;
178 /** The account that used it. */
179 redeemedBy: string | null;
180 /** RFC 3339. */
181 createdAt: string;
182 /** RFC 3339. */
183 expiresAt: string;
184 redeemedAt: string | null;
185 revokedAt: string | null;
186 /** The staff member who minted it; only in staff views. */
187 staff?: string | null;
188};
189
190/** How many invites someone may have out. `limit` and `remaining` are null for no limit. */
191export type Allowance = { limit: number | null; used: number; remaining: number | null };
192
193export type InvitesOverview = {
194 mode: RegistrationMode;
195 allowance: Allowance;
196 /** Workspaces the person owns that were granted invites to share. */
197 workspaces: { slug: string; allowance: Allowance }[];
198 invites: Invite[];
199};
200
201/** What a valid code is for, before it is used. */
202export type InvitePreview = {
203 kind: "account" | "workspace";
204 /** Pending, unless `anyStatus` asked about a code that is spent. */
205 status: InviteStatus;
206 /** Null when g1t staff sent it. */
207 invitedBy: { username: string; name: string | null; avatar: string | null } | null;
208 workspace: ProfileWorkspace | null;
209 /** The repository it accepts an invitation to, such as `{ name: "flagon-io/g1t", role: "write" }`. */
210 repository: { name: string; role: string } | null;
211 /** Partly hidden, such as `a•••@example.com`. */
212 email: string | null;
213 /** The bound address in full, while the invite is pending: it fills in and locks the sign-up form. */
214 address: string | null;
215 /** Whether the bound address has a g1t account already: sign in to accept. */
216 hasAccount: boolean;
217 /** With a viewer: whether it is theirs (for one of their confirmed addresses, or used by them). */
218 forViewer: boolean | null;
219 expiresAt: string;
220};
221
222export type WaitlistStatus = "waiting" | "invited" | "dismissed";
223
224export type WaitlistEntry = {
225 id: string;
226 email: string;
227 about: string | null;
228 status: WaitlistStatus;
229 inviteId: string | null;
230 decidedBy: string | null;
231 decidedAt: string | null;
232 /** What staff wrote when approving; it went in the invite email. */
233 note: string | null;
234 /** The account made with the invite, once it was used. */
235 joinedAs: string | null;
236 /** When they first asked. */
237 createdAt: string;
238 /** When they last asked. */
239 updatedAt: string;
240};
241
242export type InviteGrant = { amount: number; note: string | null; grantedBy: string; createdAt: string };
243export type InviteTreeNode = { username: string; joinedAt: string; invited: InviteTreeNode[] };
244
245/** Where a person came from and whom they brought. For a workspace, `username` is its slug. */
246export type InviteTree = {
247 username: string;
248 /** Who invited them, then who invited that person, and so on. */
249 invitedBy: string[];
250 /** The staff member who minted their invite, when staff did. */
251 staff: string | null;
252 allowance: Allowance;
253 grants: InviteGrant[];
254 invites: Invite[];
255 /** Whom they invited, three levels down. */
256 invited: InviteTreeNode[];
257};
258
259/** The most rows one staff listing of invites or the waitlist returns. */
260export const ADMIN_INVITES_LIMIT = 500;
261
262/**
263 * Staff-only identity, for sudo.g1t.sh. It takes no viewer and checks no
264 * membership: only sudo calls it, over its service binding, once Cloudflare
265 * Access and its staff list have let someone in. Never call it on behalf of
266 * a customer.
267 */
268export interface IdentityAdminApi {
269 /** Every workspace, newest first, at most 500; `query` matches slug, name, or an owner's username or email. */
270 workspaces(query?: string): Promise<AdminWorkspace[]>;
271 /** One workspace with all its members, or null. */
272 workspace(slug: string): Promise<AdminWorkspaceDetail | null>;
273
274 /** The waitlist, newest first; `query` matches the address or what they said. */
275 waitlist(query?: string | null, status?: WaitlistStatus | null): Promise<WaitlistEntry[]>;
276 /** How many requests are waiting, for the navigation's badge. */
277 waitlistPending(): Promise<number>;
278 /**
279 * Approving mints an invite bound to the address and emails it, with
280 * `note` (up to 500 characters) if given; dismissing only marks it.
281 */
282 decideWaitlist(id: string, approve: boolean, staff: string, note?: string | null): Promise<Result<WaitlistEntry>>;
283 /** Invites, newest first; `query` is a code's start, or part of an email, inviter or redeemer. */
284 invites(query?: string | null): Promise<Invite[]>;
285 revokeInvite(id: string, staff: string): Promise<Result<Invite>>;
286 /** An invite that uses nobody's allowance, optionally bound to (and emailed to) `email`. */
287 mintInvite(email: string | null, staff: string): Promise<Result<Invite>>;
288 /** More invites (or fewer, with a negative amount) for a person or a workspace. */
289 grantInvites(
290 target: "user" | "workspace",
291 name: string,
292 amount: number,
293 note: string,
294 staff: string,
295 ): Promise<Result<Allowance>>;
296 /** Where a person came from and whom they brought, or null. */
297 inviteTree(username: string): Promise<InviteTree | null>;
298 /** A workspace's granted invites and the invites made for it, or null. */
299 workspaceInvites(slug: string): Promise<InviteTree | null>;
300
301 /** Workspaces owners deleted that are not purged yet, newest first. */
302 deletedWorkspaces(): Promise<DeletedWorkspace[]>;
303 /**
304 * Brings a deleted workspace back, with its members, tokens and what went
305 * with it, while it is still restorable. Publishes `workspace.restored`.
306 */
307 restoreWorkspace(workspaceId: string, staff: string): Promise<Result<boolean>>;
308 /**
309 * Purges a deleted workspace now rather than at `purgeAfter`. `confirm` is
310 * its slug, typed out. Refused for a protected workspace. Publishes
311 * `workspace.deleted`.
312 */
313 purgeWorkspace(workspaceId: string, staff: string, confirm: string): Promise<Result<boolean>>;
314}
315
316/** Who is asking. Every read and write in every service takes one. */
317export type Viewer = User | null;
318
319export type SshKey = {
320 id: string;
321 title: string;
322 fingerprint: string;
323 /** RFC 3339. */
324 createdAt: string;
325};
326
327export type AccessToken = {
328 id: string;
329 name: string;
330 /** RFC 3339. */
331 createdAt: string;
332 /** RFC 3339, to within a few minutes. Null until it is first used. */
333 lastUsedAt: string | null;
334 /**
335 * For a workspace's token, the username of the member who made it. Null
336 * once that account is gone, and on personal tokens.
337 */
338 createdBy: string | null;
339 /** Its scopes, as `resource:level`. Null: full access. */
340 scopes: string[] | null;
341 /** Made before tokens had scopes: full access until someone narrows it. */
342 legacy: boolean;
343 /** RFC 3339. Null: it does not expire. */
344 expiresAt: string | null;
345};
346
347/** What a new or changed token may do. */
348export type TokenGrant = {
349 /** Null: full access. */
350 scopes: string[] | null;
351};
352
353export type DeviceStart = {
354 /** Secret held by the tool and exchanged for a token once approved. */
355 deviceCode: string;
356 /** Short code shown to the person, e.g. `WDJB-MJHT`. */
357 userCode: string;
358 /** Seconds until both codes stop working. */
359 expiresIn: number;
360 /** Seconds the tool should wait between polls. */
361 interval: number;
362};
363
364export type DeviceRequest = { userCode: string; clientName: string };
365
366export type DeviceClaim =
367 | { status: "pending" | "denied" | "expired" }
368 | { status: "approved"; token: string; user: User };
369
370/** What the site passes on once a person has approved an application. */
371export type OAuthApproval = {
372 clientId: string;
373 /** Shown wherever the application's access is listed. */
374 clientName: string;
375 redirectUri: string;
376 /** PKCE challenge, method S256. */
377 codeChallenge: string;
378 /** What the person granted. Null: full access. */
379 scopes: string[] | null;
380};
381
382export type OAuthTokens = {
383 accessToken: string;
384 /** Works once; using it returns the next one. */
385 refreshToken: string;
386 /** Seconds until the access token stops working. */
387 expiresIn: number;
388 /** The scopes granted, space-separated, or `*` for full access. */
389 scope?: string | null;
390};
391
392/** An application a person has signed in to. */
393export type OAuthGrant = {
394 id: string;
395 clientName: string;
396 /** RFC 3339. */
397 createdAt: string;
398 /** RFC 3339. */
399 lastUsedAt: string;
400 /** What the person granted. Null: full access. */
401 scopes: string[] | null;
402 /** Signed in before applications had scopes: full access until narrowed. */
403 legacy: boolean;
404};
405
406/** Accounts, credentials and sessions. */
407/**
408 * What deleting a workspace takes with it, and what stands in the way:
409 * nothing does while `billing` is null and it is not `protected`.
410 */
411export type WorkspaceDeletion = {
412 /** Its live repositories, deleted with it. */
413 repositories: number;
414 /** Its projects, hidden with it. */
415 projects: number;
416 members: number;
417 /** Why billing cannot close it yet, in words for its owner. */
418 billing: string | null;
419 /** It can never be deleted, by anyone. */
420 protected: boolean;
421};
422
423/** How long a deleted workspace is kept, for g1t's staff to restore, before it is purged. */
424export const WORKSPACE_RESTORE_DAYS = 30;
425
426/**
427 * Workspaces nobody can delete, whatever identity's `PROTECTED_WORKSPACES`
428 * says: Flagon's, which runs g1t. Services that act on `workspace.deleting`
429 * check it too, so one published for it by mistake changes nothing.
430 */
431export const ALWAYS_PROTECTED_WORKSPACES: readonly string[] = ["flagon-io"];
432
433/** Whether `slug` is one of `ALWAYS_PROTECTED_WORKSPACES`, in any case. */
434export function isProtectedWorkspace(slug: string): boolean {
435 return ALWAYS_PROTECTED_WORKSPACES.includes(slug.trim().toLowerCase());
436}
437
438/** A workspace an owner deleted, kept until `purgeAfter` for staff to restore. */
439export type DeletedWorkspace = {
440 workspaceId: string;
441 slug: string;
442 name: string;
443 /** RFC 3339. */
444 deletedAt: string;
445 /** The username of the owner who deleted it. */
446 deletedBy: string;
447 /** RFC 3339: when it is purged unless restored first. */
448 purgeAfter: string;
449 /** What went with it, counted when it was deleted. */
450 went: WorkspaceDeletion;
451 /** Whether staff can still restore it. */
452 restorable: boolean;
453};
454
455export interface IdentityApi extends AccessClient {
456 /**
457 * Creates an account and signs it in. While registration is invite-only,
458 * `inviteCode` must be an unused, unexpired invite (and, when it names an
459 * email, that address); it is ignored while registration is open.
460 */
461 register(
462 username: string,
463 email: string,
464 password: string,
465 inviteCode?: string | null,
466 /** Who is asking, such as the visitor's IP address, for rate limits. */
467 client?: string | null,
468 ): Promise<Result<{ user: User; sessionToken: string }>>;
469 /** Verifies a username and password for website sign-in. */
470 /**
471 * Verifies a username, or any confirmed address of the account, and its
472 * password. Wrong passwords are counted against the account and `client`
473 * (the visitor's IP address); past a limit nothing is checked for a while.
474 */
475 signIn(username: string, password: string, client?: string | null): Promise<Result<{ user: User; sessionToken: string }>>;
476 signOut(sessionToken: string): Promise<void>;
477
478 /** Sends the confirmation email again. */
479 resendVerification(user: User): Promise<Result<boolean>>;
480 /** Confirms the address the emailed token was sent to. */
481 verifyEmail(token: string): Promise<Result<User>>;
482 /** Emails a reset link if the address has an account. Always resolves. */
483 /**
484 * Any confirmed address of an account works; the link goes to it, and the
485 * primary and backup are told. A few an hour per address and per `client`.
486 */
487 requestPasswordReset(email: string, client?: string | null): Promise<boolean>;
488 /** Sets a new password from an emailed token and ends every session. */
489 resetPassword(token: string, password: string): Promise<Result<User>>;
490
491 /**
492 * Device sign-in (RFC 8628). A tool starts a request, a person approves
493 * its short code in a browser, and the tool claims an access token.
494 */
495 deviceStart(clientName: string): Promise<DeviceStart>;
496 /** What a user code is asking for, or null if it is not valid. */
497 deviceLookup(userCode: string): Promise<DeviceRequest | null>;
498 deviceResolve(userCode: string, user: User, approve: boolean): Promise<Result<boolean>>;
499 deviceClaim(deviceCode: string): Promise<DeviceClaim>;
500
501 /**
502 * OAuth 2.1 for applications that sign a person in through the browser.
503 * The caller has checked the client and its redirect address; this
504 * returns the one-time code the application exchanges for tokens.
505 */
506 oauthAuthorize(user: User, approval: OAuthApproval): Promise<{ code: string }>;
507 /** Redeems a code. It works once, for that client, with the PKCE verifier. */
508 oauthExchange(code: string, codeVerifier: string, clientId: string, redirectUri: string): Promise<Result<OAuthTokens>>;
509 /** Trades a refresh token for new tokens; the old ones stop working. */
510 oauthRefresh(refreshToken: string, clientId: string): Promise<Result<OAuthTokens>>;
511 /** Applications the user has signed in to, most recently used first. */
512 listOAuthGrants(user: User): Promise<OAuthGrant[]>;
513 /** Signs an application out. */
514 revokeOAuthGrant(user: User, id: string): Promise<void>;
515 /** Changes what an application may do, at once and when it refreshes. */
516 updateOAuthGrant(user: User, id: string, grant: TokenGrant): Promise<Result<OAuthGrant>>;
517
518 createWorkspace(user: User, slug: string, name: string): Promise<Result<Workspace>>;
519 /** Public details of a workspace, or null. */
520 getWorkspace(slug: string): Promise<Workspace | null>;
521 /** Where a workspace keeps its repositories' git data; null when there is no such workspace. */
522 workspaceResidency(slug: string): Promise<DataResidency | null>;
523 /**
524 * Owners only. Applies to repositories made from then on. Offer `eu`
525 * only when the repos service's `storageOptions()` says it is available.
526 */
527 setWorkspaceResidency(actor: User, slug: string, residency: DataResidency): Promise<Result<DataResidency>>;
528 /** Members only. */
529 listMembers(slug: string, viewer: Viewer): Promise<Result<Member[]>>;
530 /** Owners only. */
531 addMember(actor: User, slug: string, username: string): Promise<Result<boolean>>;
532 /** Owners only. */
533 removeMember(actor: User, slug: string, username: string): Promise<Result<boolean>>;
534 /** Owners only. An empty name falls back to the slug. */
535 updateWorkspace(actor: User, slug: string, details: { name: string; description: string }): Promise<Result<Workspace>>;
536 /**
537 * Owners only. Changes the slug, the first segment of the workspace's
538 * URLs; the display name is untouched. The old slug redirects to the new
539 * one, and stays reserved for this workspace, for `SLUG_HOLD_DAYS`.
540 * Publishes `workspace.renamed`.
541 */
542 renameWorkspace(actor: User, slug: string, newSlug: string): Promise<Result<Workspace>>;
543 /** Whether `renameWorkspace` would be allowed, changing nothing. */
544 checkWorkspaceRename(actor: User, slug: string, newSlug: string): Promise<Result<boolean>>;
545 /**
546 * The workspace's current slug when `slug` is one it was renamed from
547 * within `SLUG_HOLD_DAYS`; null otherwise, including for a slug in use.
548 */
549 resolveSlug(slug: string): Promise<string | null>;
550 /**
551 * Owners only, a person only. `confirm` is the slug, typed out. Refused
552 * for a protected workspace, and while billing cannot settle it. Its
553 * repositories, projects and apps go with it; it is kept for
554 * `WORKSPACE_RESTORE_DAYS`, when g1t's staff can restore it, then purged.
555 * Its slug is never given to anyone else; the person whose username it is
556 * may make it again once it is purged. Publishes `workspace.deleting`.
557 */
558 deleteWorkspace(actor: User, slug: string, confirm: string): Promise<Result<boolean>>;
559 /** What `deleteWorkspace` would take with it, and what stands in its way, changing nothing. */
560 checkWorkspaceDeletion(actor: User, slug: string): Promise<Result<WorkspaceDeletion>>;
561 /**
562 * Owners only. `image` is the file in base64: PNG, JPEG, WebP or GIF, at
563 * most `MAX_AVATAR_BYTES`, checked by its bytes. Null removes the icon.
564 */
565 setWorkspaceAvatar(actor: User, slug: string, image: string | null): Promise<Result<Workspace>>;
566 /** A person's own avatar, as `setWorkspaceAvatar`: the new one, or null. */
567 setUserAvatar(user: User, image: string | null): Promise<Result<string | null>>;
568
569 /**
570 * A workspace's own access tokens. They belong to the workspace, act as
571 * it, and keep working when the member who made one leaves. Members only.
572 */
573 listWorkspaceTokens(slug: string, viewer: Viewer): Promise<Result<AccessToken[]>>;
574 /** Owners only. The plaintext token is returned once and never stored. */
575 createWorkspaceToken(
576 actor: User,
577 slug: string,
578 name: string,
579 grant?: TokenGrant & { ttlSeconds?: number },
580 ): Promise<Result<{ token: string; info: AccessToken }>>;
581 /** Owners only. */
582 removeWorkspaceToken(actor: User, slug: string, id: string): Promise<Result<boolean>>;
583
584 userForSession(sessionToken: string): Promise<Viewer>;
585
586 /** Whether registration is invite-only. */
587 registration(): Promise<RegistrationMode>;
588 /** A person's invites and what they have left. */
589 listInvites(user: User): Promise<InvitesOverview>;
590 /**
591 * A person makes an invite, optionally for one address (emailed to it),
592 * using one of theirs or, with `workspace`, one the workspace was granted.
593 * People only: never an agent or a workspace's token.
594 */
595 createInvite(user: User, options?: { email?: string | null; workspace?: string | null }): Promise<Result<Invite>>;
596 /** Its maker, or an owner of its workspace, revokes a pending invite; the invite comes back. */
597 revokeInvite(user: User, id: string): Promise<Result<Invite>>;
598 /**
599 * What a code is for. Unknown, used, revoked and expired codes all get the
600 * same answer, unless `anyStatus`: then a real code that is spent is
601 * described, with its `status`. `viewer` sets `forViewer`.
602 */
603 checkInvite(
604 code: string,
605 client?: string | null,
606 options?: { viewer?: User | null; anyStatus?: boolean },
607 ): Promise<Result<InvitePreview>>;
608 /**
609 * A signed-in person uses a workspace invite sent to their address, or one
610 * sent with a repository invitation; returns the workspace's slug, or
611 * `workspace/repo`.
612 */
613 acceptInvite(user: User, code: string): Promise<Result<string>>;
614 /** Owners only. Invites an address into a workspace, always with an invite bound to it. */
615 inviteMember(actor: User, slug: string, email: string): Promise<Result<Invite>>;
616 /** Owners only: the workspace's invites, newest first. */
617 workspaceInvites(slug: string, viewer: Viewer): Promise<Result<Invite[]>>;
618 /** Owners only. */
619 revokeWorkspaceInvite(actor: User, slug: string, id: string): Promise<Result<Invite>>;
620 /** Someone without an invite asks for one. Always the same answer for a valid address. */
621 requestAccess(email: string, about: string, client?: string | null): Promise<Result<boolean>>;
622
623 /** Verifies git credentials: the account password or an access token. */
624 userForGitCredentials(username: string, secret: string): Promise<Viewer>;
625 /** Resolves a `g1t_…` access token, as sent to the API and MCP server. */
626 userForAccessToken(token: string): Promise<Viewer>;
627 userForSshKey(fingerprint: string): Promise<Viewer>;
628 userByUsername(username: string): Promise<Viewer>;
629 /** The names behind account and workspace ids; unknown ids are left out. */
630 usernames(ids: string[]): Promise<Record<string, string>>;
631
632 /** A person's public profile, or null if there is no such account. Never an email address. */
633 profile(username: string): Promise<Profile | null>;
634 /** A person changes their own profile. Every field is replaced; an empty one is cleared. */
635 updateProfile(actor: User, fields: ProfileFields): Promise<Result<Profile>>;
636 /**
637 * The workspaces a profile shows `viewer`: those the viewer belongs to
638 * as well, and those of `publicIn` (where the person made a public
639 * project) that the person really belongs to. Nothing else.
640 */
641 profileWorkspaces(username: string, viewer: Viewer, publicIn: string[]): Promise<ProfileWorkspace[]>;
642
643 listSshKeys(user: User): Promise<SshKey[]>;
644 /** Takes one line in OpenSSH public key format. */
645 addSshKey(user: User, title: string, publicKey: string): Promise<Result<SshKey>>;
646 removeSshKey(user: User, id: string): Promise<void>;
647
648 listAccessTokens(user: User): Promise<AccessToken[]>;
649 /**
650 * The plaintext token is returned once and never stored. With
651 * `ttlSeconds` the token expires and is left out of token lists; that
652 * form is used for hosted agents. A token made for a workspace acting
653 * through a token of its own belongs to that workspace too.
654 */
655 createAccessToken(
656 user: User,
657 name: string,
658 ttlSeconds?: number,
659 grant?: TokenGrant & { listed?: boolean },
660 ): Promise<{ token: string; info: AccessToken }>;
661 /** Changes what one of a person's tokens may do; the token is unchanged. */
662 updateAccessToken(user: User, id: string, grant: TokenGrant): Promise<Result<AccessToken>>;
663 /**
664 * A token for a g1t agent working for `onBehalfOf`: it acts as
665 * `g1t`, in `scope.repo` only, and only for `scope.operations`.
666 */
667 createAgentToken(
668 onBehalfOf: User,
669 scope: AgentScope,
670 ttlSeconds: number,
671 ): Promise<{ token: string; info: AccessToken }>;
672 removeAccessToken(user: User, id: string): Promise<void>;
673 /**
674 * A token for one sandbox run: it acts as the agent on behalf of
675 * `onBehalfOf`, can do only what the run's kind needs in `repo`, and
676 * expires after `ttlSeconds`. See `audit.ts`.
677 */
678 createRunCredential(input: CreateRunCredentialInput): Promise<{ token: string; info: AccessToken }>;
679 /** Ties tokens, by the SHA-256 of their text in hex, to the agent run their sandbox recorded. */
680 bindRunCredentials(tokenHashes: string[], runId: string): Promise<boolean>;
681 /** Ends a sandbox's run credentials, by hash or by run. Never touches another token. */
682 revokeRunCredentials(target: { tokenHashes?: string[]; runId?: string | null }): Promise<boolean>;
683}
684
685
686/** What an agent's token may do: these operations, in this repository. */
687export type AgentScope = { repo: RepoPath; operations: string[]; run?: RunBinding };
688
689/** The most characters each profile field takes. Mirrors `crates/contracts/src/identity.rs`. */
690export const PROFILE_LIMITS = { name: 80, bio: 160, location: 80, website: 200, pronouns: 40 } as const;
691
692/** What anyone may see about a person, at `g1t.sh/u/<username>`. */
693export type Profile = {
694 username: string;
695 /** The name they go by, if they gave one. */
696 name: string | null;
697 bio: string | null;
698 location: string | null;
699 /** Always an `https://` address. */
700 website: string | null;
701 pronouns: string | null;
702 /** The uploaded avatar's hash, served at `/avatars/<avatar>`. */
703 avatar: string | null;
704 /** When the account was made. RFC 3339. */
705 createdAt: string;
706};
707
708/** What a person may change on their profile. Empty clears a field. */
709export type ProfileFields = {
710 name: string;
711 bio: string;
712 location: string;
713 /** `https://…`; a bare `example.com` is taken as `https://example.com`. */
714 website: string;
715 pronouns: string;
716};
717
718/** A workspace on a person's profile. */
719export type ProfileWorkspace = { slug: string; name: string; avatar: string | null };