Skip to content
539 linesCodeBlameRaw
1//! Access tokens: the `g1t_…` secrets used by git, the API and the MCP
2//! server.
3//!
4//! There is one kind of token and two kinds of owner. A personal token
5//! acts as the person who made it. A workspace's token belongs to the
6//! workspace and acts as it, so automation needs no account of its own and
7//! keeps working when the member who set it up leaves. Either is made and
8//! changed with permissions and a reach (token_reach.rs); this file mints,
9//! resolves, lists and deletes them.
10
11use g1t_contracts::identity::*;
12use g1t_contracts::scopes::{FULL_ACCESS, JobToken, Scope, TokenAccess, everything, parse_scopes, permissions_of, scopes_text};
13use g1t_contracts::time::{SQL_NOW, rfc3339};
14use g1t_contracts::{FailureCode, Membership, Outcome, PrincipalKind, Role, User, Viewer, new_id};
15use g1t_kit::now_ms;
16use serde::Deserialize;
17use worker::Result;
18use worker::wasm_bindgen::JsValue;
19
20use crate::{Identity, crypto};
21
22pub const TOKEN_PREFIX: &str = "g1t_";
23/// How stale a token's last-used time may get before it is written again.
24const LAST_USED_RESOLUTION_MS: u64 = 5 * 60 * 1000;
25pub(crate) const MAX_TOKENS_PER_WORKSPACE: usize = 50;
26const WORKSPACE_ID_PREFIX: &str = "wsp_";
27
28pub(crate) const TOKEN_COLUMNS: &str = "access_tokens.id, access_tokens.name, access_tokens.created_at,
29 access_tokens.last_used_at, users.username AS created_by, access_tokens.scopes,
30 access_tokens.expires_at, access_tokens.description, access_tokens.admin,
31 access_tokens.workspace_id, access_tokens.repository_selection,
32 access_tokens.status, access_tokens.review_reason, access_tokens.website,
33 (SELECT slug FROM workspaces WHERE workspaces.id = access_tokens.owner_workspace_id) AS owner_workspace";
34
35/// Who a new token belongs to.
36pub(crate) enum Owner<'a> {
37 User(&'a str),
38 Workspace {
39 id: &'a str,
40 created_by: Option<&'a str>,
41 },
42}
43
44#[derive(Deserialize)]
45pub(crate) struct TokenRow {
46 id: String,
47 name: String,
48 created_at: String,
49 last_used_at: Option<String>,
50 created_by: Option<String>,
51 scopes: Option<String>,
52 expires_at: Option<String>,
53 /// Its reach, status, description and a workspace token's Admin
54 /// (migration 0034; see token_reach.rs).
55 #[serde(flatten)]
56 pub(crate) more: crate::token_reach::TokenRowMore,
57}
58
59/// What a token or grant may do, as it is to be stored. A token reaches
60/// whatever its owner can; only its scopes narrow that.
61///
62/// The `resources` columns (migration 0022) held a limit to some
63/// workspaces or repositories. That limit was retired (migration 0023);
64/// the columns stay, since D1 cannot drop them in place, and nothing
65/// reads or writes them.
66#[derive(Clone, Debug, Default)]
67pub(crate) struct Grant {
68 /// Null: full access.
69 pub scopes: Option<Vec<Scope>>,
70}
71
72impl Grant {
73 /// From what a caller asked for: unknown scopes are left out.
74 pub(crate) fn asked(scopes: &Option<Vec<String>>) -> Self {
75 Grant {
76 scopes: scopes.as_ref().map(|scopes| parse_scopes(&scopes.join(" "))),
77 }
78 }
79
80 /// The `scopes` column: `*` for full access.
81 pub(crate) fn scopes_column(&self) -> String {
82 match &self.scopes {
83 None => FULL_ACCESS.to_owned(),
84 Some(scopes) => scopes_text(scopes),
85 }
86 }
87}
88
89/// A `scopes` column read back: the scopes (null for full access), and
90/// whether it is a legacy row, made before scopes.
91pub(crate) fn stored_scopes(column: Option<&str>) -> (Option<Vec<String>>, bool) {
92 match column {
93 None => (None, true),
94 Some(FULL_ACCESS) => (None, false),
95 Some(text) => (
96 Some(parse_scopes(text).iter().map(|scope| scope.as_str().to_owned()).collect()),
97 false,
98 ),
99 }
100}
101
102/// The owner of a token being used.
103#[derive(Deserialize)]
104struct Presented {
105 id: String,
106 user_id: Option<String>,
107 workspace_id: Option<String>,
108 last_used_at: Option<String>,
109 /// Set on a sandbox run's credential: what it may do, as JSON `AgentScope`.
110 agent_scope: Option<String>,
111 scopes: Option<String>,
112 #[serde(default)]
113 name: Option<String>,
114 /// Set on a workflow job's token (job_tokens.rs): its repository, job
115 /// and run.
116 #[serde(default)]
117 repo: Option<String>,
118 #[serde(default)]
119 job_id: Option<String>,
120 #[serde(default)]
121 job_run_id: Option<String>,
122 #[serde(default)]
123 job_pulls: Option<u32>,
124 /// 1 when its owner let it use the website (migration 0043).
125 #[serde(default)]
126 website: Option<f64>,
127 /// The workspace it is made for, its repositories and status, a
128 /// workspace token's Admin, and when it was made and expires, for the
129 /// rules of the workspaces it reaches (token_reach.rs).
130 #[serde(flatten)]
131 facts: crate::token_reach::Facts,
132}
133
134#[derive(Deserialize)]
135struct WorkspaceRef {
136 id: String,
137 slug: String,
138}
139
140fn text(value: Option<&str>) -> JsValue {
141 value.map_or(JsValue::NULL, JsValue::from)
142}
143
144/// Whether a token being used may be used on the website as its owner: one
145/// a person made and turned that on for, never a workspace's, a job's or an
146/// agent's (apps/web, app/lib/website-token.ts).
147fn website_allowed(presented: &Presented) -> bool {
148 presented.website.is_some_and(|on| on >= 1.0)
149 && presented.user_id.is_some()
150 && presented.workspace_id.is_none()
151 && presented.job_id.is_none()
152 && presented.agent_scope.is_none()
153}
154
155impl Identity {
156 pub async fn user_for_access_token(&self, token: &str) -> Result<Viewer> {
157 if !token.starts_with(TOKEN_PREFIX) {
158 return Ok(None);
159 }
160 let Some(presented) = self
161 .db
162 .prepare(format!(
163 "SELECT id, user_id, workspace_id, last_used_at, agent_scope, scopes, name,
164 repo, job_id, job_run_id, job_pulls, created_at, expires_at,
165 owner_workspace_id, repository_selection, status, admin, website
166 FROM access_tokens
167 WHERE token_hash = ? AND (expires_at IS NULL OR expires_at > {SQL_NOW})"
168 ))
169 .bind(&[crypto::sha256_hex(token).into()])?
170 .first::<Presented>(None)
171 .await?
172 else {
173 return Ok(None);
174 };
175 // A sandbox run's credential (or an older agent token): g1t on
176 // behalf of the person it was made for, in its repository's
177 // workspace while they belong to it, and only for what its scope
178 // lists. See run_credentials.rs.
179 if let Some(scope) = presented
180 .agent_scope
181 .as_deref()
182 .and_then(|scope| serde_json::from_str::<AgentScope>(scope).ok())
183 {
184 return self
185 .agent_principal(
186 &presented.id,
187 presented.user_id.as_deref(),
188 presented.workspace_id.as_deref(),
189 scope,
190 )
191 .await;
192 }
193 let mut viewer = match (&presented.user_id, &presented.workspace_id) {
194 (Some(user_id), _) => {
195 self.find_user(
196 "SELECT id, username, display_username, email_verified_at IS NOT NULL AS verified, avatar
197 FROM users WHERE id = ? AND deleted_at IS NULL",
198 user_id,
199 )
200 .await?
201 }
202 (None, Some(workspace_id)) => self.workspace_principal(workspace_id).await?,
203 (None, None) => None,
204 };
205 if let Some(user) = viewer.as_mut() {
206 self.note_use(&presented).await?;
207 let (scopes, legacy) = stored_scopes(presented.scopes.as_deref());
208 user.token = Some(Box::new(TokenAccess {
209 token_id: presented.id.clone(),
210 scopes,
211 legacy,
212 name: presented.name.clone(),
213 repo: presented.repo.clone(),
214 job: match (&presented.job_id, &presented.job_run_id) {
215 (Some(job_id), Some(run_id)) => Some(JobToken {
216 run_id: run_id.clone(),
217 job_id: job_id.clone(),
218 pull_requests: presented.job_pulls == Some(1),
219 }),
220 _ => None,
221 },
222 website: website_allowed(&presented),
223 ..TokenAccess::default()
224 }));
225 // What it reaches: the workspace it is made for and its
226 // repositories, the workspaces whose rules let it in, a
227 // workspace token's role.
228 self.apply_reach(user, &presented.id, presented.user_id.is_some(), &presented.facts).await?;
229 }
230 Ok(viewer)
231 }
232
233 pub(crate) fn info(row: TokenRow) -> AccessToken {
234 let (scopes, legacy) = stored_scopes(row.scopes.as_deref());
235 let held: Vec<Scope> = scopes.as_ref().map_or_else(everything, |scopes| scopes.iter().filter_map(|scope| Scope::parse(scope)).collect());
236 let mut info = AccessToken {
237 id: row.id,
238 name: row.name,
239 created_at: row.created_at,
240 last_used_at: row.last_used_at,
241 created_by: row.created_by,
242 scopes,
243 legacy,
244 expires_at: row.expires_at,
245 permissions: permissions_of(&held),
246 ..AccessToken::default()
247 };
248 row.more.describe(&mut info);
249 info
250 }
251
252 /// A workspace as the actor behind one of its own tokens. It can do
253 /// what a member can, in that workspace only.
254 pub(crate) async fn workspace_principal(&self, workspace_id: &str) -> Result<Viewer> {
255 let workspace = self
256 .db
257 // A deleted workspace's tokens are refused until it is restored.
258 .prepare("SELECT id, slug FROM workspaces WHERE id = ? AND deleted_at IS NULL")
259 .bind(&[workspace_id.into()])?
260 .first::<WorkspaceRef>(None)
261 .await?;
262 Ok(workspace.map(|workspace| User {
263 id: workspace.id,
264 username: workspace.slug.clone(),
265 kind: PrincipalKind::Workspace,
266 verified: true,
267 workspaces: vec![Membership::member(workspace.slug)],
268 ..User::default()
269 }))
270 }
271
272 async fn note_use(&self, presented: &Presented) -> Result<()> {
273 let now = now_ms();
274 let stale = rfc3339(now.saturating_sub(LAST_USED_RESOLUTION_MS));
275 if presented
276 .last_used_at
277 .as_deref()
278 .is_some_and(|at| at >= stale.as_str())
279 {
280 return Ok(());
281 }
282 self.db
283 .prepare("UPDATE access_tokens SET last_used_at = ? WHERE id = ?")
284 .bind(&[rfc3339(now).into(), presented.id.as_str().into()])?
285 .run()
286 .await?;
287 Ok(())
288 }
289
290 pub(crate) async fn mint(
291 &self,
292 owner: Owner<'_>,
293 name: &str,
294 ttl_seconds: Option<u64>,
295 grant: &Grant,
296 listed: bool,
297 ) -> Result<CreatedAccessToken> {
298 let token = format!("{TOKEN_PREFIX}{}", crypto::random_hex(20));
299 let now = now_ms();
300 let name: String = match name.trim() {
301 "" => "Access token".to_owned(),
302 name => name.chars().take(100).collect(),
303 };
304 let (user_id, workspace_id, created_by) = match owner {
305 Owner::User(id) => (Some(id), None, Some(id)),
306 Owner::Workspace { id, created_by } => (None, Some(id), created_by),
307 };
308 let expires_at = ttl_seconds.map(|ttl| rfc3339(now + ttl * 1000));
309 let info = AccessToken {
310 id: new_id("tok", now),
311 name,
312 created_at: rfc3339(now),
313 last_used_at: None,
314 created_by: None,
315 scopes: grant
316 .scopes
317 .as_ref()
318 .map(|scopes| scopes.iter().map(|scope| scope.as_str().to_owned()).collect()),
319 legacy: false,
320 expires_at: expires_at.clone(),
321 permissions: permissions_of(&grant.scopes.clone().unwrap_or_else(everything)),
322 workspace_owned: workspace_id.is_some(),
323 ..AccessToken::default()
324 };
325 self.db
326 .prepare(
327 "INSERT INTO access_tokens
328 (id, user_id, workspace_id, created_by, name, token_hash, created_at, expires_at,
329 scopes, listed)
330 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
331 )
332 .bind(&[
333 info.id.as_str().into(),
334 text(user_id),
335 text(workspace_id),
336 text(created_by),
337 info.name.as_str().into(),
338 crypto::sha256_hex(&token).into(),
339 info.created_at.as_str().into(),
340 text(expires_at.as_deref()),
341 grant.scopes_column().into(),
342 JsValue::from(u8::from(listed)),
343 ])?
344 .run()
345 .await?;
346 Ok(CreatedAccessToken { token, info })
347 }
348
349 /// A workspace's own token that expires and is never listed: what a
350 /// workflow job's token is minted as (job_tokens.rs).
351 pub(crate) async fn mint_for_workspace(
352 &self,
353 workspace_id: &str,
354 name: &str,
355 ttl_seconds: u64,
356 grant: &Grant,
357 ) -> Result<CreatedAccessToken> {
358 self.mint(
359 Owner::Workspace {
360 id: workspace_id,
361 created_by: None,
362 },
363 name,
364 Some(ttl_seconds),
365 grant,
366 false,
367 )
368 .await
369 }
370
371 pub async fn create_access_token(
372 &self,
373 a: CreateAccessTokenArgs,
374 ) -> Result<CreatedAccessToken> {
375 // Told apart by the id, which says what it names, since callers pass
376 // on actors that other services stored as an id and a name.
377 let owner = if a.user.id.starts_with(WORKSPACE_ID_PREFIX) {
378 Owner::Workspace {
379 id: &a.user.id,
380 created_by: None,
381 }
382 } else {
383 Owner::User(&a.user.id)
384 };
385 let created = self.mint(owner, &a.name, a.ttl_seconds, &Grant::asked(&a.scopes), a.listed).await?;
386 // A person's lasting token: in their security log and their
387 // workspaces' audit logs. Short-lived ones (agents' runs, OAuth
388 // access tokens) are recorded where they are made.
389 if a.ttl_seconds.is_none() && !a.user.id.starts_with(WORKSPACE_ID_PREFIX) && !a.user.username.is_empty() {
390 self.log_security(&a.user.id, "token_created", Some(&created.info.name), None).await;
391 self.audit_account(&a.user, "token.created", &format!("Created access token {}", created.info.name)).await;
392 }
393 Ok(created)
394 }
395
396 /// `remove_access_token`: deletes one of a person's own tokens.
397 pub async fn remove_access_token(&self, a: RemoveArgs) -> Result<()> {
398 let name: Option<String> = self
399 .db
400 .prepare("DELETE FROM access_tokens WHERE id = ? AND user_id = ? RETURNING name")
401 .bind(&[a.id.as_str().into(), a.user.id.as_str().into()])?
402 .first(Some("name"))
403 .await?;
404 if let Some(name) = name {
405 self.log_security(&a.user.id, "token_deleted", Some(&name), None).await;
406 self.audit_account(&a.user, "token.deleted", &format!("Deleted access token {name}")).await;
407 }
408 Ok(())
409 }
410
411 pub async fn list_access_tokens(&self, a: UserArgs) -> Result<Vec<AccessToken>> {
412 let mut tokens = self.tokens_where("access_tokens.user_id = ?", &a.user.id).await?;
413 // Its selected repositories, by name.
414 self.name_repositories(&mut tokens, &Some(a.user)).await?;
415 Ok(tokens)
416 }
417
418 /// The tokens a person or workspace made on purpose: those that do not
419 /// expire, and those made with an expiry from settings, rather than
420 /// those issued to an application or a hosted agent.
421 pub(crate) async fn tokens_where(&self, owner: &str, id: &str) -> Result<Vec<AccessToken>> {
422 let rows = self
423 .db
424 .prepare(format!(
425 "SELECT {TOKEN_COLUMNS} FROM access_tokens
426 LEFT JOIN users ON users.id = access_tokens.created_by
427 WHERE {owner} AND (access_tokens.expires_at IS NULL OR access_tokens.listed = 1)
428 AND access_tokens.agent_scope IS NULL
429 ORDER BY access_tokens.id"
430 ))
431 .bind(&[id.into()])?
432 .all()
433 .await?
434 .results::<TokenRow>()?;
435 Ok(rows.into_iter().map(Self::info).collect())
436 }
437
438 pub async fn list_workspace_tokens(
439 &self,
440 a: WorkspaceTokensArgs,
441 ) -> Result<Outcome<Vec<AccessToken>>> {
442 let slug = a.slug.to_lowercase();
443 if !a.viewer.as_ref().is_some_and(|viewer| viewer.is_member(&slug)) {
444 return Ok(Outcome::fail(
445 FailureCode::Forbidden,
446 "Only members can see a workspace's access tokens.",
447 ));
448 }
449 let Some(workspace) = self.get_workspace(SlugArgs { slug }).await? else {
450 return Ok(Outcome::fail(FailureCode::NotFound, "Workspace not found."));
451 };
452 let mut tokens = self.tokens_where("access_tokens.workspace_id = ?", &workspace.id).await?;
453 self.name_repositories(&mut tokens, &a.viewer).await?;
454 Ok(Outcome::Ok(tokens))
455 }
456
457 /// The workspace's id, if `actor` is a person who owns it.
458 pub(crate) async fn owned_workspace(&self, actor: &User, slug: &str) -> Result<Outcome<String>> {
459 let slug = slug.to_lowercase();
460 if actor.kind != PrincipalKind::User || actor.role_in(&slug) != Some(Role::Owner) {
461 return Ok(Outcome::fail(
462 FailureCode::Forbidden,
463 "Only an owner can manage a workspace's access tokens.",
464 ));
465 }
466 Ok(match self.get_workspace(SlugArgs { slug }).await? {
467 Some(workspace) => Outcome::Ok(workspace.id),
468 None => Outcome::fail(FailureCode::NotFound, "Workspace not found."),
469 })
470 }
471
472 pub async fn remove_workspace_token(
473 &self,
474 a: RemoveWorkspaceTokenArgs,
475 ) -> Result<Outcome<bool>> {
476 let workspace_id = match self.owned_workspace(&a.actor, &a.slug).await? {
477 Outcome::Ok(id) => id,
478 Outcome::Fail(failure) => return Ok(Outcome::Fail(failure)),
479 };
480 let name: Option<String> = self
481 .db
482 .prepare("DELETE FROM access_tokens WHERE id = ? AND workspace_id = ? RETURNING name")
483 .bind(&[a.id.into(), workspace_id.into()])?
484 .first(Some("name"))
485 .await?;
486 if let Some(name) = name {
487 self.audit_workspace(
488 &a.actor,
489 "workspace_token.deleted",
490 &a.slug.to_lowercase(),
491 g1t_contracts::audit::Surface::Web,
492 format!("Deleted workspace access token {name}"),
493 )
494 .await;
495 }
496 Ok(Outcome::Ok(true))
497 }
498}
499
500#[cfg(test)]
501mod tests {
502 use super::*;
503
504 #[test]
505 fn a_row_without_scopes_is_legacy_full_access() {
506 assert_eq!(stored_scopes(None), (None, true));
507 assert_eq!(stored_scopes(Some("*")), (None, false));
508 assert_eq!(
509 stored_scopes(Some("issues:write repo:read nonsense")),
510 (Some(vec!["repo:read".to_owned(), "issues:write".to_owned()]), false)
511 );
512 }
513
514 #[test]
515 fn scopes_are_stored_as_text() {
516 let grant = Grant::asked(&Some(vec!["issues:read".to_owned(), "bogus".to_owned()]));
517 assert_eq!(grant.scopes_column(), "issues:read");
518 assert_eq!(Grant::asked(&None).scopes_column(), "*");
519 assert_eq!(Grant::asked(&Some(vec![])).scopes_column(), "");
520 }
521
522 #[test]
523 fn only_a_persons_own_token_with_it_turned_on_uses_the_website() {
524 let row = |extra: serde_json::Value| {
525 let mut value = serde_json::json!({ "id": "tok_1", "user_id": "usr_1", "workspace_id": null, "last_used_at": null, "agent_scope": null, "scopes": "*", "website": 1.0 });
526 for (key, field) in extra.as_object().unwrap() {
527 value[key] = field.clone();
528 }
529 serde_json::from_value::<Presented>(value).unwrap()
530 };
531 assert!(website_allowed(&row(serde_json::json!({}))));
532 assert!(!website_allowed(&row(serde_json::json!({ "website": 0.0 }))));
533 assert!(!website_allowed(&row(serde_json::json!({ "website": null }))));
534 // A workspace's token, a job's token and an agent's never do.
535 assert!(!website_allowed(&row(serde_json::json!({ "user_id": null, "workspace_id": "wsp_1" }))));
536 assert!(!website_allowed(&row(serde_json::json!({ "job_id": "job_1" }))));
537 assert!(!website_allowed(&row(serde_json::json!({ "agent_scope": "{}" }))));
538 }
539}