Skip to content
API Reference

OAuth Primitives

Low-level OAuth 2.0 primitives for discovery, token exchange, and JWT operations.

The OAuth package provides pure OAuth 2.0 primitives with no MCP dependency. Use it when you need direct control over OAuth flows or are building outside the MCP framework.

  • Custom OAuth flows outside of MCP
  • Token exchange (RFC 8693) for delegated access
  • Authorization server discovery
  • JWT signing and verification
  • Building your own auth middleware
Terminal window
pip install keycardai-oauth

keycardai.oauth

Export Description
Client / AsyncClient OAuth client for discovery, registration, token exchange, and impersonation
TokenResponse Parsed token endpoint response
AuthorizationServerMetadata Parsed .well-known metadata
pkce.authenticate High-level PKCE login flow with browser launch and loopback callback
pkce.OAuthCallbackServer Local callback receiver for custom PKCE flows
utils.pkce Low-level PKCE code verifier and challenge utilities
GrantType, TokenType OAuth enum constants
BearerAuth, BasicAuth HTTP auth strategies for outbound requests
extract_bearer_token Utility to extract bearer tokens from headers
OAuthError, TokenExchangeError Exception types
from keycardai.oauth import Client
zone_url = "https://<zone-id>.keycard.cloud"
with Client(zone_url) as client:
metadata = client.discover_server_metadata()
print(metadata.issuer)
print(metadata.token_endpoint)
from keycardai.oauth import BasicAuth, Client
with Client(zone_url, auth=BasicAuth("<client-id>", "<client-secret>")) as client:
response = client.exchange_token(
subject_token="<user-access-token>",
resource="https://api.github.com",
)
print(response.access_token)

Mint a token that acts as a specific user who isn’t present. The client authenticates with its own confidential credential and names the user; the SDK builds the substitute-user token and runs the RFC 8693 exchange. See Act on Behalf of Absent Users for the setup and policy requirements.

from keycardai.oauth import Client
from keycardai.oauth.http.auth import BasicAuth
with Client(zone_url, auth=BasicAuth("<client-id>", "<client-secret>")) as client:
response = client.impersonate(
user_identifier="troy.barnes@greendale.edu",
resource="https://api.github.com",
)
print(response.access_token)

Use this flow when a CLI tool, desktop app, or MCP client needs to log a user in through their browser. The authenticate() helper opens the user’s browser at the authorize endpoint, runs a local callback server, and exchanges the authorization code for tokens.

import httpx
from keycardai.oauth.pkce import authenticate
# Hit the protected resource without a token to get the WWW-Authenticate challenge
async with httpx.AsyncClient() as http:
response = await http.get("<resource-url>")
challenge = response.headers["www-authenticate"]
token = await authenticate(
client_id="<client-id>",
resource_url="<resource-url>",
www_authenticate_header=challenge,
scopes=["read:data"],
)
print(token.access_token)