diff --git a/Cargo.lock b/Cargo.lock index 1bd45293..b523e5b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3001,9 +3001,9 @@ dependencies = [ [[package]] name = "oauth-provider" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b0f78829493c60f76b32253acbcbddf67f358262f85e6c042e80cb706e7dbbe" +checksum = "94b5d403ee812668e1b57a9da9c584cf4d61b57e70619b29b1b6b82aa00ba631" dependencies = [ "async-trait", "base64 0.22.1", @@ -3023,9 +3023,9 @@ dependencies = [ [[package]] name = "oauth-provider-axum" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a63dafd8ad1cd3e468152a257a09791d62f2c6d0eed8e73fce2442b5ce49a75" +checksum = "601b1fbf556dc9b408880735b01265c72ab6cb357e01803f9c0450dea8f213ab" dependencies = [ "async-trait", "axum", @@ -3036,9 +3036,9 @@ dependencies = [ [[package]] name = "oauth-provider-laravel" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8b9a71814d8f022adafed725f98a7f766e81ee555137d350b5254a9a68e353" +checksum = "d7be2166aec76c8a904e77752116e929bb39ad2f5b5e2badf7e47a7da4783c5d" dependencies = [ "async-trait", "axum", @@ -3058,9 +3058,9 @@ dependencies = [ [[package]] name = "oauth-provider-mcp" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55162b5882d6f3a986d437e51bcd86d523de01dc7026534b18da5c8121db069e" +checksum = "52af05e7e13b3f20e7c9b2dea41c11c48ad8debe86c86e874497dc6be6854b5e" dependencies = [ "async-trait", "oauth-provider", @@ -4497,7 +4497,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix", "windows-sys 0.61.2", diff --git a/app/Cargo.toml b/app/Cargo.toml index 8afa4694..344cd324 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -25,8 +25,8 @@ laravel-rust = { version = "=0.1.4", features = ["database-postgres", "minijinja axum = "0.8" base64 = "0.22" hex = "0.4" -oauth-provider = "0.1.3" -oauth-provider-laravel = { version = "=0.1.1", features = ["postgres"] } +oauth-provider = "0.1.4" +oauth-provider-laravel = { version = "=0.1.2", features = ["postgres"] } hmac = "0.12" jsonwebtoken = "9" serde = { version = "1", features = ["derive"] } diff --git a/app/src/auth/oauth.rs b/app/src/auth/oauth.rs index 5b224353..68314a73 100644 --- a/app/src/auth/oauth.rs +++ b/app/src/auth/oauth.rs @@ -74,7 +74,9 @@ pub fn granted_scopes(resource: &str, staff: bool) -> Result, &'stat } } -fn is_admin_resource(resource: &str) -> bool { +/// Whether `resource` is an admin MCP endpoint (`.../mcp/admin`). +#[must_use] +pub fn is_admin_resource(resource: &str) -> bool { resource.trim_end_matches('/').ends_with("/mcp/admin") } diff --git a/app/src/console/staff.rs b/app/src/console/staff.rs index bcd43ac7..a1c9e3cb 100644 --- a/app/src/console/staff.rs +++ b/app/src/console/staff.rs @@ -71,36 +71,28 @@ impl UserSetStaff { } } -/// Ends every OAuth connection of someone's that holds the `admin` scope (the admin MCP endpoint): its access and -/// refresh tokens stop working, and a code not yet exchanged can't be. The endpoint also checks `users.staff` on -/// every call; this makes the tokens themselves useless. Their other connections (`mcp`) are left alone. Returns -/// how many connections it ended. +/// Ends every OAuth connection of someone's to the admin MCP endpoint (`/mcp/admin`, the only resource that grants the +/// `admin` scope): its access and refresh tokens stop working, and a code not yet exchanged can't be. The endpoint also +/// checks `users.staff` on every call; this makes the tokens themselves useless. Their other connections (`mcp`) are +/// left alone. Returns how many connections it ended. +/// +/// oauth-provider-laravel does the revoking (`OAuth::disconnect_resource`), so it follows that crate's tables. Every +/// admin resource the OAuth server is configured with is covered, and the one the endpoint checks tokens against. /// /// # Errors /// On a database error. pub async fn revoke_admin_connections(subject: &str) -> laravel::Result { - let now = laravel::now().timestamp().as_microsecond(); - let families: Vec = db::query!( - "SELECT family_id FROM oauth_tokens WHERE subject = {subject} AND ' ' || scope || ' ' LIKE '% admin %' \ - AND revoked_at IS NULL \ - UNION SELECT family_id FROM oauth_codes WHERE subject = {subject} AND ' ' || scope || ' ' LIKE '% admin %' \ - AND used_at IS NULL AND NOT EXISTS (SELECT 1 FROM oauth_families f \ - WHERE f.family_id = oauth_codes.family_id AND f.revoked_at IS NOT NULL)" - ) - .select_as::() - .await?; - for family in &families { - db::query!( - "INSERT INTO oauth_families (family_id, revoked_at) VALUES ({family}, {now}) \ - ON CONFLICT (family_id) DO UPDATE SET revoked_at = COALESCE(oauth_families.revoked_at, excluded.revoked_at)" - ) - .insert() - .await?; - db::query!("UPDATE oauth_tokens SET revoked_at = {now} WHERE family_id = {family} AND revoked_at IS NULL") - .update() - .await?; + let oauth = laravel::container::resolve::().await?; + let mut resources: Vec = + oauth.config().resources.iter().filter(|r| crate::auth::oauth::is_admin_resource(r)).cloned().collect(); + resources.push(crate::mcp::admin::resource()); + resources.sort(); + resources.dedup(); + let mut revoked = 0; + for resource in &resources { + revoked += oauth.disconnect_resource(subject, resource).await?.families; } - Ok(families.len() as u64) + Ok(revoked) } /// `org:add-member --org --user [--role admin|member|read]`: add an existing user to an diff --git a/app/tests/staff_commands.rs b/app/tests/staff_commands.rs index a925f455..0f58bb2f 100644 --- a/app/tests/staff_commands.rs +++ b/app/tests/staff_commands.rs @@ -39,27 +39,35 @@ async fn user_set_staff_turns_staff_on_and_off(app: Application) -> laravel::Res async fn user_set_staff_false_revokes_admin_connections(app: Application) -> laravel::Result<()> { let ada = person("Ada", "ada@example.com").await?; let subject = ada.pid.to_string(); - let row = |family: &str, n: &str, scope: &str| { - let (family, scope, access, refresh) = - (family.to_string(), scope.to_string(), format!("access-{n}"), format!("refresh-{n}")); + // Admin tokens are bound to the admin endpoint; mcp tokens to the MCP endpoint. + let admin_resource = app::mcp::admin::resource(); + let mcp_resource = admin_resource.trim_end_matches("/admin").to_string(); + let row = |family: &str, n: &str, scope: &str, resource: &str| { + let (family, scope, access, refresh, resource) = ( + family.to_string(), + scope.to_string(), + format!("access-{n}"), + format!("refresh-{n}"), + resource.to_string(), + ); let subject = subject.clone(); async move { db::query!( "INSERT INTO oauth_tokens (family_id, client_key, client_id, subject, access_hash, refresh_hash, \ resource, scope, created_at, access_expires_at, refresh_expires_at) \ - VALUES ({family}, 'k', 'c', {subject}, {access}, {refresh}, 'https://mcp.test/mcp', {scope}, 1, \ + VALUES ({family}, 'k', 'c', {subject}, {access}, {refresh}, {resource}, {scope}, 1, \ 9999999999999999, 9999999999999999)" ) .insert() .await } }; - row("fam-admin", "1", "admin").await?; - row("fam-mcp", "2", "mcp").await?; + row("fam-admin", "1", "admin", &admin_resource).await?; + row("fam-mcp", "2", "mcp", &mcp_resource).await?; db::query!( "INSERT INTO oauth_codes (code_hash, family_id, client_key, client_id, subject, redirect_uri, code_challenge, \ resource, scope, created_at, expires_at) \ - VALUES ('code-1', 'fam-code', 'k', 'c', {subject}, 'http://127.0.0.1/cb', 'x', 'https://mcp.test/mcp/admin', \ + VALUES ('code-1', 'fam-code', 'k', 'c', {subject}, 'http://127.0.0.1/cb', 'x', {admin_resource}, \ 'admin', 1, 9999999999999999)" ) .insert()