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