Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
4 changes: 3 additions & 1 deletion app/src/auth/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ pub fn granted_scopes(resource: &str, staff: bool) -> Result<Vec<String>, &'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")
}

Expand Down
42 changes: 17 additions & 25 deletions app/src/console/staff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
let now = laravel::now().timestamp().as_microsecond();
let families: Vec<String> = 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::<String>()
.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::<oauth_provider_laravel::OAuth>().await?;
let mut resources: Vec<String> =
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 <slug> --user <email or forge login> [--role admin|member|read]`: add an existing user to an
Expand Down
22 changes: 15 additions & 7 deletions app/tests/staff_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading