Admin tools for Claude: an MCP server, connected with an admin's approval
- services/mcp: the MCP server at /mcp (Streamable HTTP, stateless): the admin's reading tools, the daily tasks and the forum's moderation; every tool calls the site's API with the caller's token, and members' e-mail addresses and names are masked in the answers. - Backend: OAuth 2.1 for MCP clients (registration with Claude's or a local return address only, PKCE, rotating refresh tokens, revocation); the admin guards accept the token for reading and the writes in MCP_WRITES only. - Forum: an admin's token reaches the moderator endpoints only. - Frontend: the approval page (/oauth/engedely), Admin → Claude to revoke a connection, the authorization server's metadata at its standard address. - Deploy: the mcp service in compose, Caddy, CI; the worker removes old codes, tokens and clients. docs/mcp.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
-- Claude (or another MCP client) connected to the admin tools through OAuth
|
||||
-- (docs/mcp.md). An admin approves the connection on the site; the client
|
||||
-- then holds a short access token and a refresh token. Every secret is
|
||||
-- stored as its SHA-256 hash, like the sessions.
|
||||
|
||||
-- The clients that registered themselves (RFC 7591), only with an allowed
|
||||
-- return address (Claude's callback, or a local one for Claude Code).
|
||||
CREATE TABLE oauth_clients (
|
||||
id text PRIMARY KEY,
|
||||
name text NOT NULL CHECK (char_length(name) <= 100),
|
||||
redirect_uris text[] NOT NULL,
|
||||
-- NULL: a public client, proven by PKCE alone.
|
||||
secret_hash bytea,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_used_at timestamptz
|
||||
);
|
||||
|
||||
-- One approved connection: who allowed which client. Revoking it ends all
|
||||
-- of its tokens.
|
||||
CREATE TABLE oauth_grants (
|
||||
id uuid PRIMARY KEY,
|
||||
client_id text NOT NULL REFERENCES oauth_clients (id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
scope text NOT NULL,
|
||||
-- The MCP server the tokens are for (RFC 8707).
|
||||
resource text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_used_at timestamptz,
|
||||
revoked_at timestamptz
|
||||
);
|
||||
CREATE INDEX oauth_grants_user_idx ON oauth_grants (user_id);
|
||||
|
||||
-- The one-time code the approval sends back to the client (10 minutes).
|
||||
CREATE TABLE oauth_codes (
|
||||
code_hash bytea PRIMARY KEY,
|
||||
client_id text NOT NULL REFERENCES oauth_clients (id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
redirect_uri text NOT NULL,
|
||||
code_challenge text NOT NULL,
|
||||
scope text NOT NULL,
|
||||
resource text,
|
||||
expires_at timestamptz NOT NULL,
|
||||
used_at timestamptz,
|
||||
-- The connection made with it: a second use of the code revokes it.
|
||||
grant_id uuid REFERENCES oauth_grants (id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- An access token (an hour) and its refresh token (30 days). A refresh makes
|
||||
-- a new pair; the old refresh token sent again revokes the whole connection.
|
||||
CREATE TABLE oauth_tokens (
|
||||
id uuid PRIMARY KEY,
|
||||
grant_id uuid NOT NULL REFERENCES oauth_grants (id) ON DELETE CASCADE,
|
||||
access_hash bytea NOT NULL UNIQUE,
|
||||
access_expires_at timestamptz NOT NULL,
|
||||
refresh_hash bytea NOT NULL UNIQUE,
|
||||
refresh_expires_at timestamptz NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
refreshed_at timestamptz
|
||||
);
|
||||
CREATE INDEX oauth_tokens_grant_idx ON oauth_tokens (grant_id);
|
||||
CREATE INDEX oauth_tokens_expiry_idx ON oauth_tokens (refresh_expires_at);
|
||||
@@ -141,6 +141,58 @@ pub async fn user_by_session(pool: &PgPool, token: &str) -> Result<Option<(Uuid,
|
||||
Ok(row.map(|r| (r.session_id, r.user)))
|
||||
}
|
||||
|
||||
/// A valid OAuth access token of an MCP client (`oauth_tokens`): the
|
||||
/// connection it belongs to and what it was allowed.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OauthAccess {
|
||||
pub grant_id: Uuid,
|
||||
pub scope: String,
|
||||
pub resource: Option<String>,
|
||||
}
|
||||
|
||||
/// The connection and user of a valid (unexpired, not revoked) OAuth access
|
||||
/// token; notes the connection's last use (at most once a minute).
|
||||
pub async fn user_by_oauth_token(pool: &PgPool, token: &str) -> Result<Option<(OauthAccess, User)>, sqlx::Error> {
|
||||
#[derive(sqlx::FromRow)]
|
||||
struct Row {
|
||||
grant_id: Uuid,
|
||||
scope: String,
|
||||
resource: Option<String>,
|
||||
#[sqlx(flatten)]
|
||||
user: User,
|
||||
}
|
||||
let row: Option<Row> = sqlx::query_as(
|
||||
"SELECT g.id AS grant_id, g.scope, g.resource, u.id, u.email, u.email_verified_at, u.display_name, \
|
||||
u.locale, u.roles, u.created_at, u.last_login_at \
|
||||
FROM oauth_tokens t JOIN oauth_grants g ON g.id = t.grant_id JOIN users u ON u.id = g.user_id \
|
||||
WHERE t.access_hash = $1 AND t.access_expires_at > now() AND g.revoked_at IS NULL",
|
||||
)
|
||||
.bind(token_hash(token))
|
||||
.fetch_optional(pool)
|
||||
.await?;
|
||||
let Some(row) = row else { return Ok(None) };
|
||||
sqlx::query(
|
||||
"UPDATE oauth_grants SET last_used_at = now() \
|
||||
WHERE id = $1 AND (last_used_at IS NULL OR last_used_at < now() - interval '60 seconds')",
|
||||
)
|
||||
.bind(row.grant_id)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(Some((OauthAccess { grant_id: row.grant_id, scope: row.scope, resource: row.resource }, row.user)))
|
||||
}
|
||||
|
||||
/// Revokes every OAuth connection of a user (log out everywhere, a new password).
|
||||
pub async fn revoke_oauth_grants<'e, E>(executor: E, user_id: Uuid) -> Result<(), sqlx::Error>
|
||||
where
|
||||
E: sqlx::PgExecutor<'e>,
|
||||
{
|
||||
sqlx::query("UPDATE oauth_grants SET revoked_at = now() WHERE user_id = $1 AND revoked_at IS NULL")
|
||||
.bind(user_id)
|
||||
.execute(executor)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user