g1t/crates/contracts/src/identity.rs
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| API and MCP server, Rust identity service, registration, site redesign | 1 | //! The identity service: accounts, credentials and sessions. |
| 2 | //! | |
| 3 | //! Each `*Args` struct is the argument of the method of the same name, | |
| 4 | //! served at `POST /rpc/<method>`. | |
| 5 | ||
| 6 | use serde::{Deserialize, Serialize}; | |
| 7 | ||
| 8 | use crate::User; | |
| 9 | ||
| 10 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 11 | #[serde(rename_all = "camelCase")] | |
| 12 | pub struct SshKey { | |
| 13 | pub id: String, | |
| 14 | pub title: String, | |
| 15 | pub fingerprint: String, | |
| RFC 3339 timestamps in identity and repos | 16 | /// RFC 3339. |
| 17 | pub created_at: String, | |
| API and MCP server, Rust identity service, registration, site redesign | 18 | } |
| 19 | ||
| 20 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 21 | #[serde(rename_all = "camelCase")] | |
| 22 | pub struct AccessToken { | |
| 23 | pub id: String, | |
| 24 | pub name: String, | |
| RFC 3339 timestamps in identity and repos | 25 | /// RFC 3339. |
| 26 | pub created_at: String, | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 27 | /// RFC 3339, to within a few minutes. Null until it is first used. |
| 28 | pub last_used_at: Option<String>, | |
| 29 | /// For a workspace's token, the username of the member who made it. | |
| 30 | /// Null once that account is gone, and on personal tokens. | |
| 31 | pub created_by: Option<String>, | |
| API and MCP server, Rust identity service, registration, site redesign | 32 | } |
| 33 | ||
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 34 | /// `sign_in`: verifies a username, or any confirmed email address of the |
| 35 | /// account, and its password, for website sign-in. Wrong passwords are | |
| 36 | /// counted against the account and `client`, and past a limit nothing is | |
| 37 | /// checked for a while (see identity's `throttle.rs`). | |
| API and MCP server, Rust identity service, registration, site redesign | 38 | /// Returns `Outcome<SignedIn>`. |
| 39 | #[derive(Debug, Serialize, Deserialize)] | |
| 40 | pub struct SignInArgs { | |
| 41 | pub username: String, | |
| 42 | pub password: String, | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 43 | /// Who is asking, such as the visitor's IP address, for rate limits. |
| 44 | #[serde(default)] | |
| 45 | pub client: Option<String>, | |
| API and MCP server, Rust identity service, registration, site redesign | 46 | } |
| 47 | ||
| 48 | #[derive(Debug, Serialize, Deserialize)] | |
| 49 | #[serde(rename_all = "camelCase")] | |
| 50 | pub struct SignedIn { | |
| 51 | pub user: User, | |
| 52 | pub session_token: String, | |
| 53 | } | |
| 54 | ||
| 55 | /// `sign_out` and `user_for_session`. | |
| 56 | #[derive(Debug, Serialize, Deserialize)] | |
| 57 | #[serde(rename_all = "camelCase")] | |
| 58 | pub struct SessionArgs { | |
| 59 | pub session_token: String, | |
| 60 | } | |
| 61 | ||
| 62 | /// `user_for_git_credentials`: the account password or an access token. | |
| 63 | #[derive(Debug, Serialize, Deserialize)] | |
| 64 | pub struct GitCredentialsArgs { | |
| 65 | pub username: String, | |
| 66 | pub secret: String, | |
| 67 | } | |
| 68 | ||
| 69 | /// `user_for_access_token`. | |
| 70 | #[derive(Debug, Serialize, Deserialize)] | |
| 71 | pub struct TokenArgs { | |
| 72 | pub token: String, | |
| 73 | } | |
| 74 | ||
| 75 | /// `user_for_ssh_key`. | |
| 76 | #[derive(Debug, Serialize, Deserialize)] | |
| 77 | pub struct FingerprintArgs { | |
| 78 | pub fingerprint: String, | |
| 79 | } | |
| 80 | ||
| 81 | /// `user_by_username`. | |
| 82 | #[derive(Debug, Serialize, Deserialize)] | |
| 83 | pub struct UsernameArgs { | |
| 84 | pub username: String, | |
| 85 | } | |
| 86 | ||
| What happened across an outcome, as a feed beside its graph | 87 | /// `usernames`: the names behind account and workspace ids, as events and |
| 88 | /// other records store them. Returns a map from id to name; ids it does | |
| 89 | /// not know are left out. | |
| 90 | #[derive(Debug, Serialize, Deserialize)] | |
| 91 | pub struct UsernamesArgs { | |
| 92 | pub ids: Vec<String>, | |
| 93 | } | |
| 94 | ||
| API and MCP server, Rust identity service, registration, site redesign | 95 | /// `list_ssh_keys` and `list_access_tokens`. |
| 96 | #[derive(Debug, Serialize, Deserialize)] | |
| 97 | pub struct UserArgs { | |
| 98 | pub user: User, | |
| 99 | } | |
| 100 | ||
| 101 | /// `add_ssh_key`: `public_key` is one line in OpenSSH format. | |
| 102 | /// Returns `Outcome<SshKey>`. | |
| 103 | #[derive(Debug, Serialize, Deserialize)] | |
| 104 | #[serde(rename_all = "camelCase")] | |
| 105 | pub struct AddSshKeyArgs { | |
| 106 | pub user: User, | |
| 107 | pub title: String, | |
| 108 | pub public_key: String, | |
| 109 | } | |
| 110 | ||
| 111 | /// `remove_ssh_key` and `remove_access_token`. | |
| 112 | #[derive(Debug, Serialize, Deserialize)] | |
| 113 | pub struct RemoveArgs { | |
| 114 | pub user: User, | |
| 115 | pub id: String, | |
| 116 | } | |
| 117 | ||
| Agents as a team: lifecycle, merge queue, billing and a new shell | 118 | /// `create_access_token`: a token that acts as `user`. For a workspace |
| 119 | /// acting through a token of its own, the new token belongs to that | |
| 120 | /// workspace too. | |
| API and MCP server, Rust identity service, registration, site redesign | 121 | #[derive(Debug, Serialize, Deserialize)] |
| Account dropdown, llms.txt onboarding, hosted agent runner (not yet deployed) | 122 | #[serde(rename_all = "camelCase")] |
| API and MCP server, Rust identity service, registration, site redesign | 123 | pub struct CreateAccessTokenArgs { |
| 124 | pub user: User, | |
| 125 | pub name: String, | |
| Account dropdown, llms.txt onboarding, hosted agent runner (not yet deployed) | 126 | /// When set, the token stops working after this many seconds and is |
| 127 | /// left out of the user's token list. Used for hosted attempts. | |
| 128 | #[serde(default)] | |
| 129 | pub ttl_seconds: Option<u64>, | |
| API and MCP server, Rust identity service, registration, site redesign | 130 | } |
| 131 | ||
| 132 | /// The plaintext token is returned once and never stored. | |
| 133 | #[derive(Debug, Serialize, Deserialize)] | |
| 134 | pub struct CreatedAccessToken { | |
| 135 | pub token: String, | |
| 136 | pub info: AccessToken, | |
| 137 | } | |
| 138 | ||
| 139 | /// `register`: creates an account and signs it in. | |
| 140 | /// Returns `Outcome<SignedIn>`. | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 141 | /// |
| 142 | /// While registration is invite-only (`REGISTRATION_MODE=invite`), every | |
| 143 | /// new account needs `invite_code`: an unused, unexpired invite, and, when | |
| 144 | /// the invite names an email, that address. See [`CreateInviteArgs`]. | |
| API and MCP server, Rust identity service, registration, site redesign | 145 | #[derive(Debug, Serialize, Deserialize)] |
| 146 | pub struct RegisterArgs { | |
| 147 | pub username: String, | |
| 148 | pub email: String, | |
| 149 | pub password: String, | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 150 | /// An invite code such as `g1t-k7m2-q9xd-4hpw-…`. Ignored while |
| 151 | /// registration is open. | |
| 152 | #[serde(default)] | |
| 153 | pub invite_code: Option<String>, | |
| 154 | /// Who is asking, such as the visitor's IP address, for rate limits. | |
| 155 | #[serde(default)] | |
| 156 | pub client: Option<String>, | |
| API and MCP server, Rust identity service, registration, site redesign | 157 | } |
| Email verification, password reset, and Git for AI scale positioning | 158 | |
| 159 | /// `verify_email`: the token from the emailed link. Returns `Outcome<User>`. | |
| 160 | #[derive(Debug, Serialize, Deserialize)] | |
| 161 | pub struct EmailTokenArgs { | |
| 162 | pub token: String, | |
| 163 | } | |
| 164 | ||
| 165 | /// `request_password_reset`. Always succeeds, so it cannot be used to find | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 166 | /// out which addresses have accounts. Any confirmed address of an account |
| 167 | /// works: the link goes to the address given, and the primary (and the | |
| 168 | /// backup) are told a reset was asked for. A few requests an hour per | |
| 169 | /// address and per `client`; past that, nothing is sent. | |
| Email verification, password reset, and Git for AI scale positioning | 170 | #[derive(Debug, Serialize, Deserialize)] |
| 171 | pub struct EmailArgs { | |
| 172 | pub email: String, | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 173 | /// Who is asking, such as the visitor's IP address, for rate limits. |
| 174 | #[serde(default)] | |
| 175 | pub client: Option<String>, | |
| Email verification, password reset, and Git for AI scale positioning | 176 | } |
| 177 | ||
| 178 | /// `reset_password`: sets a new password and ends every session. | |
| 179 | /// Returns `Outcome<User>`. | |
| 180 | #[derive(Debug, Serialize, Deserialize)] | |
| 181 | pub struct ResetPasswordArgs { | |
| 182 | pub token: String, | |
| 183 | pub password: String, | |
| 184 | } | |
| Device sign-in replaces registering and minting tokens over the API | 185 | |
| 186 | /// `device_start`: begins a device sign-in. Returns `DeviceStart`. | |
| 187 | #[derive(Debug, Serialize, Deserialize)] | |
| 188 | #[serde(rename_all = "camelCase")] | |
| 189 | pub struct DeviceStartArgs { | |
| 190 | /// What is asking, shown to the person approving, e.g. "Claude Code". | |
| 191 | pub client_name: String, | |
| 192 | } | |
| 193 | ||
| 194 | #[derive(Debug, Serialize, Deserialize)] | |
| 195 | #[serde(rename_all = "camelCase")] | |
| 196 | pub struct DeviceStart { | |
| 197 | /// Secret held by the tool and exchanged for a token once approved. | |
| 198 | pub device_code: String, | |
| 199 | /// Short code shown to the person, e.g. `WDJB-MJHT`. | |
| 200 | pub user_code: String, | |
| 201 | /// Seconds until both codes stop working. | |
| 202 | pub expires_in: u32, | |
| 203 | /// Seconds the tool should wait between polls. | |
| 204 | pub interval: u32, | |
| 205 | } | |
| 206 | ||
| 207 | /// `device_lookup`: what a user code is asking for, or null if it is not | |
| 208 | /// valid. Returns `Option<DeviceRequest>`. | |
| 209 | #[derive(Debug, Serialize, Deserialize)] | |
| 210 | #[serde(rename_all = "camelCase")] | |
| 211 | pub struct DeviceLookupArgs { | |
| 212 | pub user_code: String, | |
| 213 | } | |
| 214 | ||
| 215 | #[derive(Debug, Serialize, Deserialize)] | |
| 216 | #[serde(rename_all = "camelCase")] | |
| 217 | pub struct DeviceRequest { | |
| 218 | pub user_code: String, | |
| 219 | pub client_name: String, | |
| 220 | } | |
| 221 | ||
| 222 | /// `device_resolve`: the signed-in person approves or denies a request. | |
| 223 | /// Returns `Outcome<bool>`. | |
| 224 | #[derive(Debug, Serialize, Deserialize)] | |
| 225 | #[serde(rename_all = "camelCase")] | |
| 226 | pub struct DeviceResolveArgs { | |
| 227 | pub user_code: String, | |
| 228 | pub user: User, | |
| 229 | pub approve: bool, | |
| 230 | } | |
| 231 | ||
| 232 | /// `device_claim`: the tool asks whether its request was approved. | |
| 233 | #[derive(Debug, Serialize, Deserialize)] | |
| 234 | #[serde(rename_all = "camelCase")] | |
| 235 | pub struct DeviceClaimArgs { | |
| 236 | pub device_code: String, | |
| 237 | } | |
| 238 | ||
| 239 | /// The answer to a `device_claim`. | |
| 240 | #[derive(Debug, Serialize, Deserialize)] | |
| 241 | #[serde(tag = "status", rename_all = "snake_case")] | |
| 242 | pub enum DeviceClaim { | |
| 243 | /// Nobody has approved or denied it yet; ask again after the interval. | |
| 244 | Pending, | |
| 245 | Denied, | |
| 246 | /// The code was never issued, has expired, or was already used. | |
| 247 | Expired, | |
| 248 | /// The access token, returned once. | |
| 249 | Approved { | |
| 250 | token: String, | |
| 251 | user: User, | |
| 252 | }, | |
| 253 | } | |
| Workspaces own repositories | 254 | |
| 255 | /// A workspace: the owner of repositories, and the first segment of their | |
| 256 | /// URLs. A person's own space and a team's are the same thing. | |
| 257 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 258 | #[serde(rename_all = "camelCase")] | |
| 259 | pub struct Workspace { | |
| 260 | pub id: String, | |
| 261 | pub slug: String, | |
| 262 | pub name: String, | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 263 | /// One line saying what the workspace is for. |
| 264 | pub description: Option<String>, | |
| Workspaces own repositories | 265 | /// RFC 3339. |
| 266 | pub created_at: String, | |
| 267 | pub member_count: u32, | |
| Workspace names and icons, and a component kit for every control | 268 | /// The workspace's uploaded icon: the SHA-256 of its bytes, served at |
| 269 | /// `/avatars/<avatar>`. Null means the generated letter avatar. | |
| 270 | #[serde(default)] | |
| 271 | pub avatar: Option<String>, | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 272 | /// What every member gets on each of its repositories; owners have |
| 273 | /// Admin. See [`crate::access`]. | |
| 274 | #[serde(default)] | |
| 275 | pub base_permission: crate::access::BasePermission, | |
| Workspaces own repositories | 276 | } |
| 277 | ||
| 278 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 279 | pub struct Member { | |
| 280 | pub username: String, | |
| 281 | pub role: crate::Role, | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 282 | /// Their display name, when they set one. |
| 283 | #[serde(default)] | |
| 284 | pub name: Option<String>, | |
| 285 | /// Their uploaded avatar: the SHA-256 of its bytes, served at | |
| 286 | /// `/avatars/<avatar>`. None means the generated letter avatar. | |
| 287 | #[serde(default)] | |
| 288 | pub avatar: Option<String>, | |
| Workspaces own repositories | 289 | } |
| 290 | ||
| 291 | /// `create_workspace`. Returns `Outcome<Workspace>`. | |
| 292 | #[derive(Debug, Serialize, Deserialize)] | |
| 293 | pub struct CreateWorkspaceArgs { | |
| 294 | pub user: User, | |
| 295 | pub slug: String, | |
| 296 | #[serde(default)] | |
| 297 | pub name: String, | |
| 298 | } | |
| 299 | ||
| 300 | /// `get_workspace`: public details, or null. Returns `Option<Workspace>`. | |
| 301 | #[derive(Debug, Serialize, Deserialize)] | |
| 302 | pub struct SlugArgs { | |
| 303 | pub slug: String, | |
| 304 | } | |
| 305 | ||
| 306 | /// `list_members`: members only. Returns `Outcome<Vec<Member>>`. | |
| 307 | #[derive(Debug, Serialize, Deserialize)] | |
| 308 | pub struct ListMembersArgs { | |
| 309 | pub slug: String, | |
| 310 | pub viewer: crate::Viewer, | |
| 311 | } | |
| 312 | ||
| 313 | /// `add_member` and `remove_member`: owners only. | |
| 314 | /// Each returns `Outcome<bool>`. | |
| 315 | #[derive(Debug, Serialize, Deserialize)] | |
| 316 | pub struct MemberArgs { | |
| 317 | pub actor: User, | |
| 318 | pub slug: String, | |
| 319 | pub username: String, | |
| 320 | } | |
| OAuth 2.1 sign-in for MCP clients and other applications | 321 | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 322 | /// `update_workspace`: owners only. An empty name falls back to the slug; |
| 323 | /// an empty description clears it. Returns `Outcome<Workspace>`. | |
| 324 | #[derive(Debug, Serialize, Deserialize)] | |
| 325 | pub struct UpdateWorkspaceArgs { | |
| 326 | pub actor: User, | |
| 327 | pub slug: String, | |
| 328 | pub name: String, | |
| 329 | pub description: String, | |
| 330 | } | |
| 331 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 332 | /// `rename_workspace`: owners only. Changes the workspace's slug, the first |
| 333 | /// segment of its URLs, to `new_slug`; the display name is untouched. The | |
| 334 | /// old slug redirects to the new one, and is held for this workspace, for | |
| 335 | /// [`SLUG_HOLD_DAYS`]. Publishes `workspace.renamed`. Returns | |
| 336 | /// `Outcome<Workspace>`. | |
| 337 | /// | |
| 338 | /// `check_workspace_rename` takes the same arguments and answers whether | |
| 339 | /// the rename would be allowed, changing nothing. Returns `Outcome<bool>`. | |
| 340 | #[derive(Debug, Serialize, Deserialize)] | |
| 341 | #[serde(rename_all = "camelCase")] | |
| 342 | pub struct RenameWorkspaceArgs { | |
| 343 | pub actor: User, | |
| 344 | pub slug: String, | |
| 345 | pub new_slug: String, | |
| 346 | } | |
| 347 | ||
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 348 | /// `delete_workspace`: owners only, and only a person. `confirm` must be |
| 349 | /// the workspace's slug, typed out. Refused while the workspace still | |
| 350 | /// holds repositories or projects, or while billing cannot settle it | |
| 351 | /// (`close_workspace`). Removes its memberships, its access tokens and its | |
| 352 | /// old-slug redirects; billing's ledger and the audit log keep its | |
| 353 | /// history. The slug is never given to another workspace; the person | |
| 354 | /// whose username it is may make a workspace of that name again. | |
| 355 | /// Publishes `workspace.deleted`. Returns `Outcome<bool>`. | |
| 356 | /// | |
| 357 | /// `check_workspace_deletion` takes the same arguments (with `confirm` | |
| 358 | /// ignored) and says what stands in the way, changing nothing. Returns | |
| 359 | /// `Outcome<WorkspaceDeletion>`. | |
| 360 | #[derive(Debug, Serialize, Deserialize)] | |
| 361 | pub struct DeleteWorkspaceArgs { | |
| 362 | pub actor: User, | |
| 363 | pub slug: String, | |
| 364 | #[serde(default)] | |
| 365 | pub confirm: String, | |
| 366 | /// Where the request came in, for the audit log; g1t.sh when absent. | |
| 367 | #[serde(default)] | |
| 368 | pub surface: Option<crate::audit::Surface>, | |
| 369 | } | |
| 370 | ||
| 371 | /// What stands between a workspace and its deletion. Nothing does when | |
| 372 | /// both counts are zero and `billing` is null. | |
| 373 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 374 | pub struct WorkspaceDeletion { | |
| 375 | pub repositories: u32, | |
| 376 | pub projects: u32, | |
| 377 | /// Why billing cannot close the workspace yet, in words for its owner. | |
| 378 | pub billing: Option<String>, | |
| 379 | } | |
| 380 | ||
| 381 | impl WorkspaceDeletion { | |
| 382 | pub fn blocked(&self) -> bool { | |
| 383 | self.repositories > 0 || self.projects > 0 || self.billing.is_some() | |
| 384 | } | |
| 385 | ||
| 386 | /// Why the workspace cannot be deleted yet, as one sentence, or `None`. | |
| 387 | pub fn reason(&self, slug: &str) -> Option<String> { | |
| 388 | let plural = |n: u32, one: &str, many: &str| { | |
| 389 | format!("{n} {}", if n == 1 { one } else { many }) | |
| 390 | }; | |
| 391 | let mut held = Vec::new(); | |
| 392 | if self.repositories > 0 { | |
| 393 | held.push(plural(self.repositories, "repository", "repositories")); | |
| 394 | } | |
| 395 | if self.projects > 0 { | |
| 396 | held.push(plural(self.projects, "project", "projects")); | |
| 397 | } | |
| 398 | if !held.is_empty() { | |
| 399 | return Some(format!( | |
| 400 | "{slug} still holds {}. Transfer them to another workspace first.", | |
| 401 | held.join(" and ") | |
| 402 | )); | |
| 403 | } | |
| 404 | self.billing.clone() | |
| 405 | } | |
| 406 | } | |
| 407 | ||
| 408 | /// `transfer_repo_scopes`: a repository moved from `from` to `to`; the | |
| 409 | /// tokens of agents at work on it are kept pointing at it. For repos' | |
| 410 | /// `transfer`. Returns `bool`. | |
| 411 | #[derive(Debug, Serialize, Deserialize)] | |
| 412 | pub struct TransferRepoScopesArgs { | |
| 413 | pub from: crate::repos::RepoPath, | |
| 414 | pub to: crate::repos::RepoPath, | |
| 415 | } | |
| 416 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 417 | /// How long a workspace's old slug keeps redirecting to it, and stays |
| 418 | /// reserved for it, after a rename. | |
| 419 | pub const SLUG_HOLD_DAYS: u64 = 90; | |
| 420 | ||
| 421 | /// How long a workspace must wait between renames. | |
| 422 | pub const RENAME_COOLDOWN_HOURS: u64 = 24; | |
| 423 | ||
| 424 | // `resolve_slug` takes `SlugArgs` and returns `Option<String>`: the | |
| 425 | // workspace's current slug when `slug` is one it was renamed from within | |
| 426 | // the last `SLUG_HOLD_DAYS`, and null otherwise (including for a slug that | |
| 427 | // is in use). | |
| 428 | ||
| Workspace names and icons, and a component kit for every control | 429 | /// `set_workspace_avatar`: owners only. `image` is the file's bytes in |
| 430 | /// base64: PNG, JPEG, WebP or GIF, at most `MAX_AVATAR_BYTES`. Null removes | |
| 431 | /// the icon. Returns `Outcome<Workspace>`. | |
| 432 | #[derive(Debug, Serialize, Deserialize)] | |
| 433 | pub struct SetWorkspaceAvatarArgs { | |
| 434 | pub actor: User, | |
| 435 | pub slug: String, | |
| 436 | pub image: Option<String>, | |
| 437 | } | |
| 438 | ||
| 439 | /// `set_user_avatar`: a person's own avatar, as `SetWorkspaceAvatarArgs`. | |
| 440 | /// Returns `Outcome<Option<String>>`: the new avatar, or null once removed. | |
| 441 | #[derive(Debug, Serialize, Deserialize)] | |
| 442 | pub struct SetUserAvatarArgs { | |
| 443 | pub user: User, | |
| 444 | pub image: Option<String>, | |
| 445 | } | |
| 446 | ||
| 447 | /// The largest avatar that can be uploaded, in bytes. | |
| 448 | pub const MAX_AVATAR_BYTES: usize = 1024 * 1024; | |
| 449 | ||
| Agents as a team: lifecycle, merge queue, billing and a new shell | 450 | /// `list_workspace_tokens`: members only. Returns |
| 451 | /// `Outcome<Vec<AccessToken>>`. | |
| 452 | #[derive(Debug, Serialize, Deserialize)] | |
| 453 | pub struct WorkspaceTokensArgs { | |
| 454 | pub slug: String, | |
| 455 | pub viewer: crate::Viewer, | |
| 456 | } | |
| 457 | ||
| 458 | /// `create_workspace_token`: owners only. The token belongs to the | |
| 459 | /// workspace, acts as it, and keeps working when the member who made it | |
| 460 | /// leaves. Returns `Outcome<CreatedAccessToken>`. | |
| 461 | #[derive(Debug, Serialize, Deserialize)] | |
| 462 | pub struct CreateWorkspaceTokenArgs { | |
| 463 | pub actor: User, | |
| 464 | pub slug: String, | |
| 465 | pub name: String, | |
| 466 | } | |
| 467 | ||
| 468 | /// `remove_workspace_token`: owners only. Returns `Outcome<bool>`. | |
| 469 | #[derive(Debug, Serialize, Deserialize)] | |
| 470 | pub struct RemoveWorkspaceTokenArgs { | |
| 471 | pub actor: User, | |
| 472 | pub slug: String, | |
| 473 | pub id: String, | |
| 474 | } | |
| 475 | ||
| OAuth 2.1 sign-in for MCP clients and other applications | 476 | /// `oauth_authorize`: the signed-in person approved an application. The |
| 477 | /// caller has checked the client and that it may be redirected to | |
| 478 | /// `redirect_uri`. Returns `OAuthCode`. | |
| 479 | #[derive(Debug, Serialize, Deserialize)] | |
| 480 | #[serde(rename_all = "camelCase")] | |
| 481 | pub struct OAuthAuthorizeArgs { | |
| 482 | pub user: User, | |
| 483 | pub client_id: String, | |
| 484 | /// Shown wherever the application's access is listed. | |
| 485 | pub client_name: String, | |
| 486 | pub redirect_uri: String, | |
| 487 | /// PKCE challenge, method S256. | |
| 488 | pub code_challenge: String, | |
| 489 | } | |
| 490 | ||
| 491 | #[derive(Debug, Serialize, Deserialize)] | |
| 492 | pub struct OAuthCode { | |
| 493 | pub code: String, | |
| 494 | } | |
| 495 | ||
| 496 | /// `oauth_exchange`: redeems an authorization code. | |
| 497 | /// Returns `Outcome<OAuthTokens>`. | |
| 498 | #[derive(Debug, Serialize, Deserialize)] | |
| 499 | #[serde(rename_all = "camelCase")] | |
| 500 | pub struct OAuthExchangeArgs { | |
| 501 | pub code: String, | |
| 502 | pub code_verifier: String, | |
| 503 | pub client_id: String, | |
| 504 | pub redirect_uri: String, | |
| 505 | } | |
| 506 | ||
| 507 | /// `oauth_refresh`: trades a refresh token for new tokens. | |
| 508 | /// Returns `Outcome<OAuthTokens>`. | |
| 509 | #[derive(Debug, Serialize, Deserialize)] | |
| 510 | #[serde(rename_all = "camelCase")] | |
| 511 | pub struct OAuthRefreshArgs { | |
| 512 | pub refresh_token: String, | |
| 513 | pub client_id: String, | |
| 514 | } | |
| 515 | ||
| 516 | #[derive(Debug, Serialize, Deserialize)] | |
| 517 | #[serde(rename_all = "camelCase")] | |
| 518 | pub struct OAuthTokens { | |
| 519 | pub access_token: String, | |
| 520 | /// Works once; using it returns the next one. | |
| 521 | pub refresh_token: String, | |
| 522 | /// Seconds until the access token stops working. | |
| 523 | pub expires_in: u64, | |
| 524 | } | |
| 525 | ||
| 526 | /// An application a person has signed in to. Listed by `list_oauth_grants` | |
| 527 | /// and ended by `revoke_oauth_grant`. | |
| 528 | #[derive(Debug, Serialize, Deserialize)] | |
| 529 | #[serde(rename_all = "camelCase")] | |
| 530 | pub struct OAuthGrant { | |
| 531 | pub id: String, | |
| 532 | pub client_name: String, | |
| 533 | /// RFC 3339. | |
| 534 | pub created_at: String, | |
| 535 | /// RFC 3339. | |
| 536 | pub last_used_at: String, | |
| 537 | } | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 538 | |
| 539 | ||
| 540 | /// What an agent's token may do: these operations, in this repository. | |
| Agents get guardrails, run credentials, an audit log, a context hub, repository instructions and mentions; security upkeep; snake_case API | 541 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 542 | pub struct AgentScope { |
| 543 | pub repo: crate::repos::RepoPath, | |
| 544 | /// API and MCP operation names, such as `create_issue`. | |
| 545 | pub operations: Vec<String>, | |
| Agents get guardrails, run credentials, an audit log, a context hub, repository instructions and mentions; security upkeep; snake_case API | 546 | /// Set on a run credential: the run it belongs to, and what it may do |
| 547 | /// with git. See [`crate::credentials`]. | |
| 548 | #[serde(default, skip_serializing_if = "Option::is_none")] | |
| 549 | pub run: Option<crate::credentials::RunBinding>, | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 550 | } |
| 551 | ||
| 552 | /// `create_agent_token`: a token for a g1t agent working on someone's | |
| 553 | /// behalf. It acts as `g1t-agent`, a member of the repository's workspace, | |
| 554 | /// and only for the operations in `scope`. Returns `CreatedAccessToken`. | |
| 555 | #[derive(Debug, Serialize, Deserialize)] | |
| 556 | #[serde(rename_all = "camelCase")] | |
| 557 | pub struct CreateAgentTokenArgs { | |
| 558 | /// The person the agent works for; the token is recorded as theirs. | |
| 559 | pub on_behalf_of: User, | |
| 560 | pub scope: AgentScope, | |
| 561 | pub ttl_seconds: u64, | |
| 562 | } | |
| 563 | ||
| 564 | // `agent_scope` takes `TokenArgs` and returns `Option<AgentScope>`: what an | |
| 565 | // agent's token may do, or null for any other token. | |
| 566 | ||
| 567 | /// The id and name g1t's agents act under. | |
| 568 | pub const AGENT_ID: &str = "usr_g1t_agent"; | |
| 569 | pub const AGENT_NAME: &str = "g1t-agent"; | |
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 570 | |
| 571 | // --- Staff --------------------------------------------------------------- | |
| 572 | // | |
| 573 | // Staff-only methods, for sudo.g1t.sh. They take no viewer and check no | |
| 574 | // membership: only sudo calls them, over its service binding, after it has | |
| 575 | // verified a Cloudflare Access sign-in and its staff list. Nothing a | |
| 576 | // customer can reach should ever forward to them. | |
| 577 | ||
| 578 | /// `notify_owners`: emails a short notice, with one link, to each owner of | |
| 579 | /// a workspace with a confirmed address. Called by other services (billing | |
| 580 | /// warns owners near their usage limit), never on a person's behalf. | |
| 581 | /// Returns how many were sent. | |
| 582 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 583 | pub struct NotifyOwnersArgs { | |
| 584 | pub workspace: String, | |
| 585 | pub subject: String, | |
| 586 | /// One or two sentences: what happened and what it means. | |
| 587 | pub intro: String, | |
| 588 | /// The button's words, such as `Open billing`. | |
| 589 | pub action: String, | |
| 590 | /// Where the button goes; must be on g1t.sh. | |
| 591 | pub link: String, | |
| 592 | /// Small print: why they got it. | |
| 593 | pub footer: String, | |
| 594 | } | |
| 595 | ||
| 596 | /// `admin_workspaces`: every workspace, newest first, at most | |
| 597 | /// [`ADMIN_WORKSPACES_LIMIT`], optionally only those whose slug, name or | |
| 598 | /// an owner's username or email contains `query`. Returns | |
| 599 | /// `Vec<AdminWorkspace>`. Staff only. | |
| 600 | #[derive(Debug, Default, Serialize, Deserialize)] | |
| 601 | pub struct AdminWorkspacesArgs { | |
| 602 | #[serde(default)] | |
| 603 | pub query: Option<String>, | |
| 604 | } | |
| 605 | ||
| 606 | /// The most workspaces one `admin_workspaces` call returns. | |
| 607 | pub const ADMIN_WORKSPACES_LIMIT: usize = 500; | |
| 608 | ||
| 609 | /// An owner of a workspace, as staff see them. | |
| 610 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| 611 | pub struct AdminOwner { | |
| 612 | pub username: String, | |
| 613 | pub email: Option<String>, | |
| 614 | } | |
| 615 | ||
| 616 | /// A workspace as staff see it: who owns it and how many belong to it. | |
| 617 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| 618 | #[serde(rename_all = "camelCase")] | |
| 619 | pub struct AdminWorkspace { | |
| 620 | pub slug: String, | |
| 621 | pub name: String, | |
| 622 | /// RFC 3339. | |
| 623 | pub created_at: String, | |
| 624 | pub owners: Vec<AdminOwner>, | |
| 625 | pub member_count: u32, | |
| 626 | } | |
| 627 | ||
| 628 | /// `admin_workspace`: one workspace with every member, or null. Takes | |
| 629 | /// `SlugArgs`; returns `Option<AdminWorkspaceDetail>`. Staff only. | |
| 630 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 631 | #[serde(rename_all = "camelCase")] | |
| 632 | pub struct AdminWorkspaceDetail { | |
| 633 | pub slug: String, | |
| 634 | pub name: String, | |
| 635 | pub description: Option<String>, | |
| 636 | /// RFC 3339. | |
| 637 | pub created_at: String, | |
| 638 | /// Owners first, then by username. | |
| 639 | pub members: Vec<AdminMember>, | |
| 640 | } | |
| 641 | ||
| 642 | /// A member of a workspace, as staff see them. | |
| 643 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 644 | pub struct AdminMember { | |
| 645 | pub username: String, | |
| 646 | pub email: Option<String>, | |
| 647 | pub role: crate::Role, | |
| 648 | /// When they joined the workspace. RFC 3339. | |
| 649 | pub joined: String, | |
| 650 | } | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 651 | |
| 652 | // --- Profiles ------------------------------------------------------------ | |
| 653 | // | |
| 654 | // A person's public page at `g1t.sh/u/<username>`. Everything in a | |
| 655 | // `Profile` is shown to anyone, signed in or not; an email address never is. | |
| 656 | ||
| 657 | /// The most characters each profile field takes. | |
| 658 | pub const MAX_PROFILE_NAME: usize = 80; | |
| 659 | pub const MAX_PROFILE_BIO: usize = 160; | |
| 660 | pub const MAX_PROFILE_LOCATION: usize = 80; | |
| 661 | pub const MAX_PROFILE_WEBSITE: usize = 200; | |
| 662 | pub const MAX_PROFILE_PRONOUNS: usize = 40; | |
| 663 | ||
| 664 | /// What anyone may see about a person. | |
| 665 | #[derive(Clone, Debug, Default, Serialize, Deserialize)] | |
| 666 | #[serde(rename_all = "camelCase")] | |
| 667 | pub struct Profile { | |
| 668 | pub username: String, | |
| 669 | /// The name they go by, if they gave one. | |
| 670 | pub name: Option<String>, | |
| 671 | /// One or two lines about them, at most [`MAX_PROFILE_BIO`] characters. | |
| 672 | pub bio: Option<String>, | |
| 673 | pub location: Option<String>, | |
| 674 | /// An `https://` address. | |
| 675 | pub website: Option<String>, | |
| 676 | pub pronouns: Option<String>, | |
| 677 | /// The uploaded avatar's hash, served at `/avatars/<avatar>`. | |
| 678 | pub avatar: Option<String>, | |
| 679 | /// When the account was made. RFC 3339. | |
| 680 | pub created_at: String, | |
| 681 | } | |
| 682 | ||
| 683 | // `profile` takes `UsernameArgs` and returns `Option<Profile>`: null for | |
| 684 | // an account that does not exist. | |
| 685 | ||
| 686 | /// `update_profile`: a person changes their own profile. Every field is | |
| 687 | /// replaced; an empty one is cleared. Returns `Outcome<Profile>`. | |
| 688 | #[derive(Debug, Default, Serialize, Deserialize)] | |
| 689 | #[serde(rename_all = "camelCase")] | |
| 690 | pub struct UpdateProfileArgs { | |
| 691 | pub actor: User, | |
| 692 | #[serde(default)] | |
| 693 | pub name: String, | |
| 694 | #[serde(default)] | |
| 695 | pub bio: String, | |
| 696 | #[serde(default)] | |
| 697 | pub location: String, | |
| 698 | #[serde(default)] | |
| 699 | pub website: String, | |
| 700 | #[serde(default)] | |
| 701 | pub pronouns: String, | |
| 702 | } | |
| 703 | ||
| 704 | /// `profile_workspaces`: the workspaces shown on a person's profile, as | |
| 705 | /// `viewer` may see them. A membership is shown only when it is no secret | |
| 706 | /// from the viewer: a workspace the viewer belongs to as well, or one of | |
| 707 | /// `public`, the workspaces the caller found the person has made a public | |
| 708 | /// project in (whose page shows that already). Returns | |
| 709 | /// `Vec<ProfileWorkspace>`; empty for an account that does not exist. | |
| 710 | #[derive(Debug, Serialize, Deserialize)] | |
| 711 | pub struct ProfileWorkspacesArgs { | |
| 712 | pub username: String, | |
| 713 | pub viewer: crate::Viewer, | |
| 714 | #[serde(default)] | |
| 715 | pub public: Vec<String>, | |
| 716 | } | |
| 717 | ||
| 718 | /// A workspace on a person's profile. | |
| 719 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 720 | pub struct ProfileWorkspace { | |
| 721 | pub slug: String, | |
| 722 | pub name: String, | |
| 723 | pub avatar: Option<String>, | |
| 724 | } | |
| Search across all of g1t, Explore, and a command palette | 725 | |
| 726 | /// `directory`: every account or every workspace, as their public pages | |
| 727 | /// show them, a page at a time in name order. For services that index | |
| 728 | /// them, such as search; nothing private is in it. Returns | |
| 729 | /// `DirectoryPage`. | |
| 730 | #[derive(Debug, Default, Serialize, Deserialize)] | |
| 731 | pub struct DirectoryArgs { | |
| 732 | /// `user` or `workspace`. | |
| 733 | pub kind: String, | |
| 734 | /// Names after this one. | |
| 735 | #[serde(default)] | |
| 736 | pub after: Option<String>, | |
| 737 | pub limit: u32, | |
| 738 | } | |
| 739 | ||
| 740 | /// One account or workspace in the directory. | |
| 741 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 742 | #[serde(rename_all = "camelCase")] | |
| 743 | pub struct DirectoryEntry { | |
| 744 | /// The account's or workspace's id. | |
| 745 | pub id: String, | |
| 746 | /// A username or a workspace's slug. | |
| 747 | pub slug: String, | |
| 748 | /// A person's display name or a workspace's name. | |
| 749 | pub name: Option<String>, | |
| 750 | /// A person's bio or a workspace's description. | |
| 751 | pub bio: Option<String>, | |
| 752 | pub avatar: Option<String>, | |
| 753 | /// RFC 3339. | |
| 754 | pub created_at: String, | |
| 755 | } | |
| 756 | ||
| 757 | #[derive(Clone, Debug, Default, Serialize, Deserialize)] | |
| 758 | pub struct DirectoryPage { | |
| 759 | pub entries: Vec<DirectoryEntry>, | |
| 760 | /// Where the next page starts; null on the last. | |
| 761 | pub next: Option<String>, | |
| 762 | } | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 763 | |
| 764 | // --- Invites --------------------------------------------------------------- | |
| 765 | // | |
| 766 | // While registration is invite-only, every new account (with a password or | |
| 767 | // through GitHub) needs an invite code. Each person may have | |
| 768 | // `INVITES_PER_USER` invites out at a time; staff grant more to a person or | |
| 769 | // to a workspace, whose owners share them. Inviting an email with no | |
| 770 | // account into a workspace makes an invite bound to that address, which | |
| 771 | // registers and joins in one step. See services/identity/src/invites.rs. | |
| 772 | ||
| 773 | /// Whether anyone may make an account, or only someone with an invite. Set | |
| 774 | /// by identity's `REGISTRATION_MODE` var; anything but `open`, including | |
| 775 | /// leaving it unset, is `invite`, so a missing setting never opens sign-up. | |
| 776 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 777 | #[serde(rename_all = "snake_case")] | |
| 778 | pub enum RegistrationMode { | |
| 779 | #[default] | |
| 780 | Invite, | |
| 781 | Open, | |
| 782 | } | |
| 783 | ||
| 784 | impl RegistrationMode { | |
| 785 | pub fn parse(text: Option<&str>) -> RegistrationMode { | |
| 786 | match text.map(|text| text.trim().to_ascii_lowercase()).as_deref() { | |
| 787 | Some("open") => RegistrationMode::Open, | |
| 788 | _ => RegistrationMode::Invite, | |
| 789 | } | |
| 790 | } | |
| 791 | } | |
| 792 | ||
| 793 | /// How many invites a person may have out at once, unless identity's | |
| 794 | /// `INVITES_PER_USER` var says otherwise. | |
| 795 | pub const INVITES_PER_USER: u32 = 5; | |
| 796 | ||
| 797 | /// How long an invite works, unless identity's `INVITE_TTL_DAYS` var says | |
| 798 | /// otherwise. | |
| 799 | pub const INVITE_TTL_DAYS: u64 = 30; | |
| 800 | ||
| 801 | /// Where an invite stands. Only a pending invite can be used or revoked. | |
| 802 | /// An expired or revoked invite that was never used gives its inviter the | |
| 803 | /// invite back. | |
| 804 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 805 | #[serde(rename_all = "snake_case")] | |
| 806 | pub enum InviteStatus { | |
| 807 | Pending, | |
| 808 | Redeemed, | |
| 809 | Expired, | |
| 810 | Revoked, | |
| 811 | } | |
| 812 | ||
| 813 | /// What using an invite does. | |
| 814 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 815 | #[serde(rename_all = "snake_case")] | |
| 816 | pub enum InviteKind { | |
| 817 | /// Makes a new account, and joins `workspace` when one is set. | |
| 818 | Account, | |
| 819 | /// An existing account joins `workspace`. Never makes an account. | |
| 820 | Workspace, | |
| 821 | } | |
| 822 | ||
| 823 | /// Whose allowance an invite uses. | |
| 824 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 825 | #[serde(rename_all = "snake_case")] | |
| 826 | pub enum InviteCharge { | |
| 827 | /// Its inviter's own. | |
| 828 | User, | |
| 829 | /// The workspace's, granted by staff and shared by its owners. | |
| 830 | Workspace, | |
| 831 | /// Nobody's: staff minted it, or it invites an existing account. | |
| 832 | None, | |
| 833 | } | |
| 834 | ||
| 835 | /// One invite, as the person who made it sees it. | |
| 836 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| 837 | #[serde(rename_all = "camelCase")] | |
| 838 | pub struct Invite { | |
| 839 | pub id: String, | |
| 840 | /// The code, such as `g1t-k7m2-q9xd-…`: returned once when the invite | |
| 841 | /// is made, and afterwards to whoever made it while it is pending. | |
| 842 | /// Null otherwise. | |
| 843 | pub code: Option<String>, | |
| 844 | /// The code's first group, such as `g1t-k7m2`, to recognise it by. | |
| 845 | pub hint: String, | |
| 846 | /// Only an account with this address can use it. Null: anyone with | |
| 847 | /// the code. | |
| 848 | pub email: Option<String>, | |
| 849 | pub kind: InviteKind, | |
| 850 | /// The workspace it joins, by slug. | |
| 851 | pub workspace: Option<String>, | |
| 852 | pub status: InviteStatus, | |
| 853 | pub charged_to: InviteCharge, | |
| 854 | /// Who made it, by username. Null when g1t staff did. | |
| 855 | pub invited_by: Option<String>, | |
| 856 | /// The account that used it, by username. | |
| 857 | pub redeemed_by: Option<String>, | |
| 858 | /// RFC 3339. | |
| 859 | pub created_at: String, | |
| 860 | /// RFC 3339. | |
| 861 | pub expires_at: String, | |
| 862 | /// RFC 3339. | |
| 863 | pub redeemed_at: Option<String>, | |
| 864 | /// RFC 3339. | |
| 865 | pub revoked_at: Option<String>, | |
| 866 | /// The staff member who minted it. Only in staff views. | |
| 867 | #[serde(default, skip_serializing_if = "Option::is_none")] | |
| 868 | pub staff: Option<String>, | |
| 869 | } | |
| 870 | ||
| 871 | /// How many invites someone may have out, and how many they have. | |
| 872 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 873 | pub struct Allowance { | |
| 874 | /// Null: no limit. | |
| 875 | pub limit: Option<u32>, | |
| 876 | /// Pending and used invites; revoked and expired ones are not counted. | |
| 877 | pub used: u32, | |
| 878 | /// Null: no limit. | |
| 879 | pub remaining: Option<u32>, | |
| 880 | } | |
| 881 | ||
| 882 | impl Allowance { | |
| 883 | pub fn new(limit: Option<u32>, used: u32) -> Allowance { | |
| 884 | Allowance { | |
| 885 | limit, | |
| 886 | used, | |
| 887 | remaining: limit.map(|limit| limit.saturating_sub(used)), | |
| 888 | } | |
| 889 | } | |
| 890 | ||
| 891 | pub fn exhausted(&self) -> bool { | |
| 892 | self.remaining == Some(0) | |
| 893 | } | |
| 894 | } | |
| 895 | ||
| 896 | /// A workspace's shared invites, for one of its owners. | |
| 897 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 898 | pub struct WorkspaceAllowance { | |
| 899 | pub slug: String, | |
| 900 | pub allowance: Allowance, | |
| 901 | } | |
| 902 | ||
| 903 | /// `list_invites` (takes `UserArgs`): a person's invites, newest first, | |
| 904 | /// and what they have left. Returns `InvitesOverview`. | |
| 905 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| 906 | pub struct InvitesOverview { | |
| 907 | pub mode: RegistrationMode, | |
| 908 | pub allowance: Allowance, | |
| 909 | /// Workspaces the person owns that staff granted invites to. | |
| 910 | pub workspaces: Vec<WorkspaceAllowance>, | |
| 911 | pub invites: Vec<Invite>, | |
| 912 | } | |
| 913 | ||
| 914 | /// `create_invite`: a person makes an invite, optionally for one email | |
| 915 | /// address. People only; never an agent or a workspace's token, and not | |
| 916 | /// before their email is confirmed. Uses one of the person's invites, or, | |
| 917 | /// with `workspace`, one of the invites staff granted that workspace (its | |
| 918 | /// owners only). Emails the address when one is given. Returns | |
| 919 | /// `Outcome<Invite>`, with the code. | |
| 920 | /// | |
| 921 | /// `revoke_invite` (takes `RemoveArgs`): its maker revokes a pending | |
| 922 | /// invite; a workspace's owners may revoke one made for the workspace. | |
| 923 | /// The invite comes back to whoever it was charged to. Returns | |
| 924 | /// `Outcome<Invite>`. | |
| 925 | #[derive(Debug, Serialize, Deserialize)] | |
| 926 | pub struct CreateInviteArgs { | |
| 927 | pub user: User, | |
| 928 | #[serde(default)] | |
| 929 | pub email: Option<String>, | |
| 930 | /// Use this workspace's granted invites, by slug. | |
| 931 | #[serde(default)] | |
| 932 | pub workspace: Option<String>, | |
| 933 | /// Where the request came in, for the audit log; g1t.sh when absent. | |
| 934 | #[serde(default)] | |
| 935 | pub surface: Option<crate::audit::Surface>, | |
| 936 | } | |
| 937 | ||
| 938 | /// `check_invite`: what an invite code is for, before using it. Returns | |
| 939 | /// `Outcome<InvitePreview>`; a code that is unknown, used, revoked or | |
| 940 | /// expired gets the same answer, so codes cannot be probed. | |
| 941 | #[derive(Debug, Serialize, Deserialize)] | |
| 942 | pub struct InviteCodeArgs { | |
| 943 | pub code: String, | |
| 944 | /// Who is asking, such as the visitor's IP address, for rate limits. | |
| 945 | #[serde(default)] | |
| 946 | pub client: Option<String>, | |
| 947 | } | |
| 948 | ||
| 949 | /// Someone shown on an invite. | |
| 950 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| 951 | pub struct InviteFrom { | |
| 952 | pub username: String, | |
| 953 | pub name: Option<String>, | |
| 954 | pub avatar: Option<String>, | |
| 955 | } | |
| 956 | ||
| 957 | /// What a valid invite code is for. | |
| 958 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
| 959 | #[serde(rename_all = "camelCase")] | |
| 960 | pub struct InvitePreview { | |
| 961 | pub kind: InviteKind, | |
| 962 | /// Null when g1t staff sent it. | |
| 963 | pub invited_by: Option<InviteFrom>, | |
| 964 | pub workspace: Option<ProfileWorkspace>, | |
| 965 | /// The address it is for, partly hidden, such as `a•••@example.com`. | |
| 966 | pub email: Option<String>, | |
| 967 | /// RFC 3339. | |
| 968 | pub expires_at: String, | |
| 969 | } | |
| 970 | ||
| 971 | /// `accept_invite`: a signed-in person uses a workspace invite made for | |
| 972 | /// their confirmed address, and joins the workspace. Returns | |
| 973 | /// `Outcome<String>`: the workspace's slug. | |
| 974 | #[derive(Debug, Serialize, Deserialize)] | |
| 975 | pub struct AcceptInviteArgs { | |
| 976 | pub user: User, | |
| 977 | pub code: String, | |
| 978 | } | |
| 979 | ||
| 980 | /// `invite_member`: an owner invites an email address into a workspace. | |
| 981 | /// It always makes an invite bound to that address and emails it, so the | |
| 982 | /// answer never says whether the address has an account. Without one, the | |
| 983 | /// invite registers and joins in one step, and uses one of the workspace's | |
| 984 | /// granted invites or else one of the owner's own. With one, it costs | |
| 985 | /// nothing. Returns `Outcome<Invite>`, with the code. | |
| 986 | #[derive(Debug, Serialize, Deserialize)] | |
| 987 | pub struct InviteMemberArgs { | |
| 988 | pub actor: User, | |
| 989 | pub slug: String, | |
| 990 | pub email: String, | |
| 991 | /// Where the request came in, for the audit log; g1t.sh when absent. | |
| 992 | #[serde(default)] | |
| 993 | pub surface: Option<crate::audit::Surface>, | |
| 994 | } | |
| 995 | ||
| 996 | /// `workspace_invites` (takes `ListMembersArgs`): a workspace's invites, | |
| 997 | /// newest first. Owners only. Returns `Outcome<Vec<Invite>>`. | |
| 998 | /// | |
| 999 | /// `revoke_workspace_invite`: owners only. Returns `Outcome<Invite>`. | |
| 1000 | #[derive(Debug, Serialize, Deserialize)] | |
| 1001 | pub struct WorkspaceInviteArgs { | |
| 1002 | pub actor: User, | |
| 1003 | pub slug: String, | |
| 1004 | pub id: String, | |
| 1005 | } | |
| 1006 | ||
| 1007 | /// `request_access`: someone without an invite asks for one. Kept on the | |
| 1008 | /// waitlist, one entry per address. Answers the same way whether or not | |
| 1009 | /// the address is already on it. Returns `Outcome<bool>`. | |
| 1010 | #[derive(Debug, Default, Serialize, Deserialize)] | |
| 1011 | pub struct RequestAccessArgs { | |
| 1012 | pub email: String, | |
| 1013 | /// What they will build, if they said. | |
| 1014 | #[serde(default)] | |
| 1015 | pub about: String, | |
| 1016 | /// Who is asking, such as the visitor's IP address, for rate limits. | |
| 1017 | #[serde(default)] | |
| 1018 | pub client: Option<String>, | |
| 1019 | } | |
| 1020 | ||
| 1021 | /// The most characters `RequestAccessArgs::about` keeps. | |
| 1022 | pub const MAX_WAITLIST_ABOUT: usize = 1000; | |
| 1023 | ||
| 1024 | // `registration` takes `{}` and returns `RegistrationMode`. | |
| 1025 | ||
| 1026 | // --- Invites, staff only --- | |
| 1027 | ||
| 1028 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 1029 | #[serde(rename_all = "snake_case")] | |
| 1030 | pub enum WaitlistStatus { | |
| 1031 | Waiting, | |
| 1032 | Invited, | |
| 1033 | Dismissed, | |
| 1034 | } | |
| 1035 | ||
| 1036 | impl WaitlistStatus { | |
| 1037 | pub fn as_str(self) -> &'static str { | |
| 1038 | match self { | |
| 1039 | WaitlistStatus::Waiting => "waiting", | |
| 1040 | WaitlistStatus::Invited => "invited", | |
| 1041 | WaitlistStatus::Dismissed => "dismissed", | |
| 1042 | } | |
| 1043 | } | |
| 1044 | } | |
| 1045 | ||
| 1046 | /// Someone who asked for access. | |
| 1047 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| 1048 | #[serde(rename_all = "camelCase")] | |
| 1049 | pub struct WaitlistEntry { | |
| 1050 | pub id: String, | |
| 1051 | pub email: String, | |
| 1052 | pub about: Option<String>, | |
| 1053 | pub status: WaitlistStatus, | |
| 1054 | pub invite_id: Option<String>, | |
| 1055 | pub decided_by: Option<String>, | |
| 1056 | /// RFC 3339. | |
| 1057 | pub decided_at: Option<String>, | |
| 1058 | /// When they first asked. RFC 3339. | |
| 1059 | pub created_at: String, | |
| 1060 | /// When they last asked. RFC 3339. | |
| 1061 | pub updated_at: String, | |
| 1062 | } | |
| 1063 | ||
| 1064 | /// `admin_waitlist`: the waitlist, oldest first, at most | |
| 1065 | /// [`ADMIN_INVITES_LIMIT`]. Returns `Vec<WaitlistEntry>`. | |
| 1066 | #[derive(Debug, Default, Serialize, Deserialize)] | |
| 1067 | pub struct AdminWaitlistArgs { | |
| 1068 | /// Part of an email address or of what they said. | |
| 1069 | #[serde(default)] | |
| 1070 | pub query: Option<String>, | |
| 1071 | /// Null: every status. | |
| 1072 | #[serde(default)] | |
| 1073 | pub status: Option<WaitlistStatus>, | |
| 1074 | } | |
| 1075 | ||
| 1076 | /// The most rows one staff listing of invites or the waitlist returns. | |
| 1077 | pub const ADMIN_INVITES_LIMIT: usize = 500; | |
| 1078 | ||
| 1079 | /// `admin_decide_waitlist`: approving mints an invite bound to the | |
| 1080 | /// address, charged to nobody, and emails it; dismissing only marks the | |
| 1081 | /// entry. Returns `Outcome<WaitlistEntry>`. | |
| 1082 | #[derive(Debug, Serialize, Deserialize)] | |
| 1083 | pub struct AdminDecideWaitlistArgs { | |
| 1084 | pub id: String, | |
| 1085 | pub approve: bool, | |
| 1086 | /// The staff member, by email. | |
| 1087 | pub staff: String, | |
| 1088 | } | |
| 1089 | ||
| 1090 | /// `admin_invites`: every invite, newest first, at most | |
| 1091 | /// [`ADMIN_INVITES_LIMIT`], optionally only those whose code starts with | |
| 1092 | /// `query`, or whose email, inviter or redeemer contains it. Returns | |
| 1093 | /// `Vec<Invite>`. | |
| 1094 | #[derive(Debug, Default, Serialize, Deserialize)] | |
| 1095 | pub struct AdminInvitesArgs { | |
| 1096 | #[serde(default)] | |
| 1097 | pub query: Option<String>, | |
| 1098 | } | |
| 1099 | ||
| 1100 | /// `admin_revoke_invite`: revokes any pending invite. Returns | |
| 1101 | /// `Outcome<Invite>`. | |
| 1102 | #[derive(Debug, Serialize, Deserialize)] | |
| 1103 | pub struct AdminRevokeInviteArgs { | |
| 1104 | pub id: String, | |
| 1105 | pub staff: String, | |
| 1106 | } | |
| 1107 | ||
| 1108 | /// `admin_mint_invite`: staff make an invite that uses nobody's | |
| 1109 | /// allowance, optionally bound to (and emailed to) an address. Returns | |
| 1110 | /// `Outcome<Invite>`, with the code. | |
| 1111 | #[derive(Debug, Serialize, Deserialize)] | |
| 1112 | pub struct AdminMintInviteArgs { | |
| 1113 | #[serde(default)] | |
| 1114 | pub email: Option<String>, | |
| 1115 | pub staff: String, | |
| 1116 | } | |
| 1117 | ||
| 1118 | /// Who staff grant invites to. | |
| 1119 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 1120 | #[serde(rename_all = "snake_case")] | |
| 1121 | pub enum GrantTarget { | |
| 1122 | User, | |
| 1123 | Workspace, | |
| 1124 | } | |
| 1125 | ||
| 1126 | impl GrantTarget { | |
| 1127 | pub fn as_str(self) -> &'static str { | |
| 1128 | match self { | |
| 1129 | GrantTarget::User => "user", | |
| 1130 | GrantTarget::Workspace => "workspace", | |
| 1131 | } | |
| 1132 | } | |
| 1133 | } | |
| 1134 | ||
| 1135 | /// `admin_grant_invites`: gives a person (by username) or a workspace (by | |
| 1136 | /// slug) `amount` more invites; a negative amount takes some back. Returns | |
| 1137 | /// `Outcome<Allowance>`: theirs afterwards. | |
| 1138 | #[derive(Debug, Serialize, Deserialize)] | |
| 1139 | pub struct AdminGrantInvitesArgs { | |
| 1140 | pub target: GrantTarget, | |
| 1141 | pub name: String, | |
| 1142 | pub amount: i32, | |
| 1143 | #[serde(default)] | |
| 1144 | pub note: String, | |
| 1145 | pub staff: String, | |
| 1146 | } | |
| 1147 | ||
| 1148 | /// The most invites one grant gives or takes back. | |
| 1149 | pub const MAX_INVITE_GRANT: i32 = 1000; | |
| 1150 | ||
| 1151 | /// Invites staff granted. | |
| 1152 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| 1153 | #[serde(rename_all = "camelCase")] | |
| 1154 | pub struct InviteGrant { | |
| 1155 | pub amount: i32, | |
| 1156 | pub note: Option<String>, | |
| 1157 | pub granted_by: String, | |
| 1158 | /// RFC 3339. | |
| 1159 | pub created_at: String, | |
| 1160 | } | |
| 1161 | ||
| 1162 | /// Someone a person invited, and whom they invited in turn. | |
| 1163 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| 1164 | #[serde(rename_all = "camelCase")] | |
| 1165 | pub struct InviteTreeNode { | |
| 1166 | pub username: String, | |
| 1167 | /// When they used the invite. RFC 3339. | |
| 1168 | pub joined_at: String, | |
| 1169 | pub invited: Vec<InviteTreeNode>, | |
| 1170 | } | |
| 1171 | ||
| 1172 | /// `admin_invite_tree` (takes `UsernameArgs`): where a person came from | |
| 1173 | /// and whom they brought, for tracing abuse. Returns `Option<InviteTree>`. | |
| 1174 | /// | |
| 1175 | /// `admin_workspace_invites` (takes `SlugArgs`): a workspace's granted | |
| 1176 | /// invites, grants and invites. Returns `Option<InviteTree>` with | |
| 1177 | /// `username` the slug and no `invited_by`. | |
| 1178 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| 1179 | #[serde(rename_all = "camelCase")] | |
| 1180 | pub struct InviteTree { | |
| 1181 | pub username: String, | |
| 1182 | /// Who invited them, then who invited that person, and so on. Empty | |
| 1183 | /// for an account made without an invite. | |
| 1184 | pub invited_by: Vec<String>, | |
| 1185 | /// The staff member who minted their invite, when staff did. | |
| 1186 | pub staff: Option<String>, | |
| 1187 | pub allowance: Allowance, | |
| 1188 | pub grants: Vec<InviteGrant>, | |
| 1189 | /// Their invites, newest first. | |
| 1190 | pub invites: Vec<Invite>, | |
| 1191 | /// Whom they invited, three levels down. | |
| 1192 | pub invited: Vec<InviteTreeNode>, | |
| 1193 | } | |
| 1194 | ||
| 1195 | #[cfg(test)] | |
| 1196 | mod deletion_tests { | |
| 1197 | use super::WorkspaceDeletion; | |
| 1198 | ||
| 1199 | #[test] | |
| 1200 | fn says_what_is_left_to_move() { | |
| 1201 | let clear = WorkspaceDeletion::default(); | |
| 1202 | assert!(!clear.blocked()); | |
| 1203 | assert_eq!(clear.reason("acme"), None); | |
| 1204 | let held = WorkspaceDeletion { | |
| 1205 | repositories: 2, | |
| 1206 | projects: 1, | |
| 1207 | billing: Some("Pay first.".into()), | |
| 1208 | }; | |
| 1209 | assert!(held.blocked()); | |
| 1210 | assert_eq!( | |
| 1211 | held.reason("acme").as_deref(), | |
| 1212 | Some("acme still holds 2 repositories and 1 project. Transfer them to another workspace first.") | |
| 1213 | ); | |
| 1214 | let owing = WorkspaceDeletion { | |
| 1215 | billing: Some("Pay first.".into()), | |
| 1216 | ..WorkspaceDeletion::default() | |
| 1217 | }; | |
| 1218 | assert_eq!(owing.reason("acme").as_deref(), Some("Pay first.")); | |
| 1219 | } | |
| 1220 | } |