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
UUID of the MCP server to authorize
OAuth callback URI (usually default)
| Code | Description |
|---|---|
| 200 | Authorization URL generated successfully |
| 400 | Invalid server UUID or missing parameters |
| 401 | User not authenticated |
| 404 | Server not found or no OAuth configuration |
| 500 | OAuth discovery or configuration error |
- ✅ 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
Authorization code from OAuth provider
PKCE state parameter (generated during initiation)
OAuth error code (if authorization failed)
Human-readable error description
| Code | Description | User Action |
|---|---|---|
state_expired | PKCE state expired (>5 minutes) | Retry OAuth flow |
state_invalid | Invalid or missing state parameter | Retry OAuth flow |
integrity_violation | State tampering detected | Contact support (security issue) |
code_exchange_failed | Token exchange with provider failed | Check OAuth server status |
user_mismatch | Code injection attempt detected | Immediate security alert |
access_denied | User denied authorization | Normal - user cancelled |
- ✅ PKCE state exists and not expired
- ✅ HMAC integrity hash verification
- ✅ User ID binding check (prevents code injection)
- ✅ One-time use enforcement (state deleted after use)
- ✅ Rate limiting (10 requests per 15 minutes per user)
- ✅ 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.
UUID of the MCP server with expired token
| Code | Description |
|---|---|
| 200 | Token refreshed successfully |
| 400 | Invalid server UUID or no refresh token available |
| 401 | User not authenticated or not server owner |
| 403 | Server ownership violation (security) |
| 409 | Token reuse detected - all tokens revoked (security) |
| 500 | Token exchange with OAuth provider failed |
- ✅ Optimistic locking prevents concurrent refresh
- ✅ Refresh token marked as used before exchange
- ✅ Automatic token rotation
- ✅ Reuse detection → immediate revocation
- ✅ Server ownership validation (multi-level)
PKCE Cleanup (Internal Cron)
This endpoint requires
CRON_SECRET authentication and should only be called by external cron services.Bearer token with
CRON_SECRET valueMetrics Endpoint
Public endpoint exposing Prometheus metrics for monitoring
Flow Metrics
Flow Metrics
oauth_flows_total{provider, status}- Total flows by outcomeoauth_flow_duration_seconds{provider, status}- Flow duration histogram
Token Metrics
Token Metrics
oauth_token_refresh_total{status, reason}- Refresh attemptsoauth_token_refresh_duration_seconds{status}- Refresh durationoauth_token_revocations_total{reason}- Token revocationsoauth_active_tokens- Current active token count
PKCE Metrics
PKCE Metrics
oauth_pkce_validations_total{status, reason}- PKCE validationsoauth_pkce_states_created_total- States createdoauth_pkce_states_cleaned_total{reason}- States cleaned upoauth_active_pkce_states- Current active states
Security Metrics
Security Metrics
oauth_security_events_total{event_type, severity}- Security eventsoauth_integrity_violations_total{violation_type}- Integrity violationsoauth_code_injection_attempts_total- Code injection attempts
Discovery Metrics
Discovery Metrics
oauth_discovery_attempts_total{method, status}- Discovery attemptsoauth_discovery_duration_seconds{method, status}- Discovery duration
Registration Metrics
Registration Metrics
oauth_client_registrations_total{status}- Client registrationsoauth_client_registration_duration_seconds{status}- Registration duration
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:| Endpoint | Limit | Window |
|---|---|---|
/api/oauth/initiate | 10 requests | 15 minutes per user |
/api/oauth/callback | 10 requests | 15 minutes per user |
/api/oauth/refresh | 60 requests | 1 minute per user |
/api/oauth/cleanup-pkce | 1 request | 1 minute (cron only) |
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_totalmetric - ✅ Set up alerts for
oauth_token_reuse_detected - ✅ Rotate
NEXTAUTH_SECRETperiodically - ✅ 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

