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