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