/* MCP OAuth sign-in for Streamable HTTP servers (mcp_oauth.c): RFC 9728 Protected Resource * Metadata discovery, RFC 8414 / OIDC Authorization Server discovery, RFC 7591 Dynamic Client * Registration, S256 PKCE loopback/paste authorization, RFC 8707 resource binding, RFC 9207 iss * verification, step-up scope union, and cross-process locked token refresh. All tokens, client * secrets and client state are sealed in picu_mcp_oauth under the credential store's key. */ #ifndef PIC_MCP_OAUTH_H #define PIC_MCP_OAUTH_H #include #include "mcp_wire.h" #include "oauth.h" /* Non-secret per-server OAuth configuration (stored in picu_mcp_server.oauth_json or read from a * project file / --mcp-config). clientSecret is never stored here. */ typedef struct { char client_id[256]; char callback_url[512]; int callback_port; /* 0: unset (ephemeral unless callback_url carries a port) */ char scope[512]; char client_name[128]; char auth_server_metadata_url[1024]; int has_client_secret; /* non-secret indicator that a sealed clientSecret was saved */ } pic_mcp_oauth_cfg; /* Parsed WWW-Authenticate challenge (Bearer or DPoP). */ typedef struct { int is_oauth; /* 1 when a Bearer or DPoP challenge was present */ char resource_metadata[1024]; char scope[512]; char error[128]; char error_description[256]; } pic_mcp_oauth_challenge; /* Auth status of an HTTP MCP server for get_mcp_servers and /mcp (no passphrase prompt). */ typedef enum { PIC_MCP_AUTH_NONE, PIC_MCP_AUTH_SIGNED_IN, PIC_MCP_AUTH_NEEDS_AUTH } pic_mcp_auth_status; /* Context held for the lifetime of a pic_mcp_target's HTTP connection. */ typedef struct { char name[128]; char url[1024]; pic_mcp_oauth_cfg cfg; char *client_secret_override; /* optional runtime secret from project/--mcp-config (wiped on free) */ int quiet; /* 1: never prompt for credential store passphrase */ } pic_mcp_oauth_ctx; /* ---- pure helpers (unit- and capacity-tested) ---------------------------------------------- */ /* Builds the canonical storage key "normalized_name|normalized_url" into OUT (CAP bytes): * lowercased name with '-' mapped to '_', lowercased scheme/host, default port stripped, fragment * dropped, trailing slash normalized. Returns bytes written (>= 0), or -1 on bad input or when * the result does not fit in CAP bytes (including NUL). */ int pic_mcp_oauth_server_key(const char *name, const char *url, char *out, size_t cap); /* Parses a WWW-Authenticate header (Bearer or DPoP; quoted with escapes and unquoted params). */ void pic_mcp_oauth_parse_www_auth(const char *header, pic_mcp_oauth_challenge *out); /* Enforces https:// for remote endpoints and allows http:// only for loopback (127.0.0.1, * localhost, [::1]). Rejects non-http(s) schemes, userinfo and control characters. 0, or -1 with err. */ int pic_mcp_oauth_url_allowed(const char *url, char *err, size_t errcap); /* Validates a loopback callback URL (http:// to 127.0.0.1, localhost or [::1]). 0, or -1 with err. */ int pic_mcp_oauth_callback_allowed(const char *url, char *err, size_t errcap); /* Validates RFC 8707 protected resource URI against the MCP server URL (same origin and path prefix * match). 0, or -1 with err. */ int pic_mcp_oauth_resource_matches(const char *server_url, const char *resource_url, char *err, size_t errcap); /* Validates RFC 9207 authorization response iss parameter against expected metadata issuer when iss * is present or the server advertised authorization_response_iss_parameter_supported. 0, or -1 with err. */ int pic_mcp_oauth_check_iss(const char *got_iss, const char *expected_issuer, int iss_supported, char *err, size_t errcap); /* Builds the deduplicated space-separated union of CONFIGURED, GRANTED and CHALLENGED scopes in * order into OUT (CAP bytes). Returns bytes written (>= 0), or -1 if CAP is too small. */ int pic_mcp_oauth_step_up_scope(const char *configured, const char *granted, const char *challenged, char *out, size_t cap); /* Parses non-secret OAuth config JSON into CFG (any NULL/empty JSON zeroes CFG and returns 0). * Validates callback_port, callback_url and auth_server_metadata_url. 0, or -1 with err. */ int pic_mcp_oauth_cfg_parse(const char *oauth_json, pic_mcp_oauth_cfg *cfg, char *err, size_t errcap); /* Serializes non-secret fields of CFG to a JSON object string (malloc'd), or NULL if CFG has no * fields set. Never includes any secret. */ char *pic_mcp_oauth_cfg_json(const pic_mcp_oauth_cfg *cfg); /* ---- storage, login, logout and transport hook --------------------------------------------- */ /* Seals CLIENT_SECRET as configuredClientSecret in picu_mcp_oauth for NAME/URL (unlocking or * preparing the credential store first), preserving any existing state in the row. 0, or -1 with err. */ int pic_mcp_oauth_save_secret(const char *server_name, const char *server_url, const char *client_secret, char *err, size_t errcap); /* Removes the picu_mcp_oauth row for NAME/URL with secure_delete. 1 removed, 0 none stored, -1 error. */ int pic_mcp_oauth_logout(const char *server_name, const char *server_url, char *err, size_t errcap); /* Reports the server's auth state without prompting for a passphrase. */ pic_mcp_auth_status pic_mcp_oauth_status(const char *server_name, const char *server_url, const char *oauth_json, const char *last_error, int has_explicit_auth); /* Runs the full interactive OAuth sign-in flow for an HTTP MCP server and seals the resulting * tokens, client info and discovery state in picu_mcp_oauth. 0, or -1 with err. */ int pic_mcp_oauth_login(const char *server_name, const char *server_url, const pic_mcp_oauth_cfg *cfg, const char *client_secret_override, const pic_oauth_io *io, int timeout_seconds, char *err, size_t errcap); /* Populates CTX and OUT for pic_mcp_target.auth (bearer attachment + proactive/reactive refresh; * never launches a browser). Call pic_mcp_oauth_ctx_free(CTX) when done. */ void pic_mcp_oauth_attach(pic_mcp_oauth_ctx *ctx, const char *server_name, const char *server_url, const pic_mcp_oauth_cfg *cfg, const char *client_secret_override, int quiet, pic_mcp_auth *out); void pic_mcp_oauth_ctx_free(pic_mcp_oauth_ctx *ctx); #endif