Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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