Skip to main content

OAuth API Reference

OAuth 2.1 authentication endpoints for MCP server authorization.
All OAuth endpoints are internal and used by the Plugged.in application. External API users should use API key authentication instead.

Base URL

Endpoints

Initiate OAuth Flow

Initiates OAuth 2.1 authorization flow with PKCE.
serverUuid
string
required
UUID of the MCP server to authorize
redirectUri
string
default:"/api/oauth/callback"
OAuth callback URI (usually default)
Request Example:
Response:
Status Codes: Security Features:
  • ✅ PKCE code_challenge generated (S256 method)
  • ✅ State parameter bound to user session
  • ✅ HMAC integrity hash for state validation
  • ✅ 5-minute expiration on PKCE state

OAuth Callback

Handles OAuth provider callback with authorization code.
code
string
required
Authorization code from OAuth provider
state
string
required
PKCE state parameter (generated during initiation)
error
string
OAuth error code (if authorization failed)
error_description
string
Human-readable error description
Success Response: Redirects to:
Error Response: Redirects to:
Error Codes: Security Validations:
  1. ✅ PKCE state exists and not expired
  2. ✅ HMAC integrity hash verification
  3. ✅ User ID binding check (prevents code injection)
  4. ✅ One-time use enforcement (state deleted after use)
  5. ✅ Rate limiting (10 requests per 15 minutes per user)
OAuth 2.1 Compliance:
  • ✅ PKCE code_verifier validation
  • ✅ State parameter integrity
  • ✅ Token rotation on refresh
  • ✅ Refresh token reuse detection

Token Refresh (Internal)

This endpoint is internal only and used automatically by the application. Manual calls are not recommended.
Refreshes OAuth access token using stored refresh token.
serverUuid
string
required
UUID of the MCP server with expired token
Request Example:
Response:
Status Codes: Security Features (OAuth 2.1):
  • ✅ Optimistic locking prevents concurrent refresh
  • ✅ Refresh token marked as used before exchange
  • ✅ Automatic token rotation
  • ✅ Reuse detection → immediate revocation
  • ✅ Server ownership validation (multi-level)
Race Condition Prevention: The endpoint uses atomic database operations:

PKCE Cleanup (Internal Cron)

This endpoint requires CRON_SECRET authentication and should only be called by external cron services.
Manually triggers expired PKCE state cleanup.
Authorization
string
required
Bearer token with CRON_SECRET value
Request Example:
Response:
Automatic Cleanup: The application automatically runs cleanup every 15 minutes with a 10-minute grace period:
Grace Period Logic: States are only deleted if they expired more than 10 minutes ago:
This protects OAuth flows that may still be completing even after the 5-minute PKCE expiration.

Metrics Endpoint

Public endpoint exposing Prometheus metrics for monitoring
Returns OAuth metrics in Prometheus format. Response Example:
Available Metrics:
  • oauth_flows_total{provider, status} - Total flows by outcome
  • oauth_flow_duration_seconds{provider, status} - Flow duration histogram
  • oauth_token_refresh_total{status, reason} - Refresh attempts
  • oauth_token_refresh_duration_seconds{status} - Refresh duration
  • oauth_token_revocations_total{reason} - Token revocations
  • oauth_active_tokens - Current active token count
  • oauth_pkce_validations_total{status, reason} - PKCE validations
  • oauth_pkce_states_created_total - States created
  • oauth_pkce_states_cleaned_total{reason} - States cleaned up
  • oauth_active_pkce_states - Current active states
  • oauth_security_events_total{event_type, severity} - Security events
  • oauth_integrity_violations_total{violation_type} - Integrity violations
  • oauth_code_injection_attempts_total - Code injection attempts
  • oauth_discovery_attempts_total{method, status} - Discovery attempts
  • oauth_discovery_duration_seconds{method, status} - Discovery duration
  • oauth_client_registrations_total{status} - Client registrations
  • oauth_client_registration_duration_seconds{status} - Registration duration
Scrape Configuration:

OAuth Flow Diagram


Security Features Summary

PKCE (RFC 7636)

  • S256 challenge method
  • 256-bit code verifier
  • One-time state validation
  • 5-minute expiration

State Integrity

  • HMAC-SHA256 binding
  • User ID validation
  • Server UUID binding
  • Tampering detection

Token Rotation

  • Single-use refresh tokens
  • Automatic rotation
  • Reuse detection
  • Immediate revocation

Ownership Validation

  • Server → Profile → Project → User chain
  • Multi-level verification
  • Prevents token substitution
  • Cross-user attack prevention

Error Handling

Client-Side Error Handling

Server-Side Error Handling

All OAuth endpoints return structured errors:

Rate Limiting

OAuth endpoints are protected by rate limiting: Rate Limit Headers:
Rate Limit Exceeded Response:

Testing OAuth Endpoints

Development Environment

Integration Tests


Best Practices

Security Checklist

  • ✅ Always use HTTPS in production
  • ✅ Never log sensitive tokens or code verifiers
  • ✅ Monitor oauth_code_injection_attempts_total metric
  • ✅ Set up alerts for oauth_token_reuse_detected
  • ✅ Rotate NEXTAUTH_SECRET periodically
  • ✅ Review OAuth security logs weekly
  • ✅ Keep dependencies updated (npm audit)

Performance Tips

  • ✅ Enable HTTP/2 for faster OAuth redirects
  • ✅ Use connection pooling for database queries
  • ✅ Cache OAuth provider metadata (RFC 9728)
  • ✅ Monitor p95 latency for token refresh
  • ✅ Set appropriate PKCE cleanup interval (15 min)

Monitoring Best Practices

  • ✅ Track OAuth flow success rate (SLO: >95%)
  • ✅ Monitor token refresh duration (p95 < 2s)
  • ✅ Alert on any code injection attempts
  • ✅ Review integrity violations daily
  • ✅ Analyze OAuth error distribution weekly

OAuth 2.1 Security

Complete OAuth security implementation

Observability

Monitoring and logging guide

Migration Guide

Upgrade to OAuth observability

Troubleshooting

Common OAuth issues and solutions