## Summary This PR implements critical MCP OAuth2 compliance features for Coder's authorization server, adding PKCE support, resource parameter handling, and OAuth2 server metadata discovery. This brings Coder's OAuth2 implementation significantly closer to production readiness for MCP (Model Context Protocol) integrations. ## What's Added ### OAuth2 Authorization Server Metadata (RFC 8414) - Add `/.well-known/oauth-authorization-server` endpoint for automatic client discovery - Returns standardized metadata including supported grant types, response types, and PKCE methods - Essential for MCP client compatibility and OAuth2 standards compliance ### PKCE Support (RFC 7636) - Implement Proof Key for Code Exchange with S256 challenge method - Add `code_challenge` and `code_challenge_method` parameters to authorization flow - Add `code_verifier` validation in token exchange - Provides enhanced security for public clients (mobile apps, CLIs) ### Resource Parameter Support (RFC 8707) - Add `resource` parameter to authorization and token endpoints - Store resource URI and bind tokens to specific audiences - Critical for MCP's resource-bound token model ### Enhanced OAuth2 Error Handling - Add OAuth2-compliant error responses with proper error codes - Use standard error format: `{"error": "code", "error_description": "details"}` - Improve error consistency across OAuth2 endpoints ### Authorization UI Improvements - Fix authorization flow to use POST-based consent instead of GET redirects - Remove dependency on referer headers for security decisions - Improve CSRF protection with proper state parameter validation ## Why This Matters **For MCP Integration:** MCP requires OAuth2 authorization servers to support PKCE, resource parameters, and metadata discovery. Without these features, MCP clients cannot securely authenticate with Coder. **For Security:** PKCE prevents authorization code interception attacks, especially critical for public clients. Resource binding ensures tokens are only valid for intended services. **For Standards Compliance:** These are widely adopted OAuth2 extensions that improve interoperability with modern OAuth2 clients. ## Database Changes - **Migration 000343:** Adds `code_challenge`, `code_challenge_method`, `resource_uri` to `oauth2_provider_app_codes` - **Migration 000343:** Adds `audience` field to `oauth2_provider_app_tokens` for resource binding - **Audit Updates:** New OAuth2 fields properly tracked in audit system - **Backward Compatibility:** All changes maintain compatibility with existing OAuth2 flows ## Test Coverage - Comprehensive PKCE test suite in `coderd/identityprovider/pkce_test.go` - OAuth2 metadata endpoint tests in `coderd/oauth2_metadata_test.go` - Integration tests covering PKCE + resource parameter combinations - Negative tests for invalid PKCE verifiers and malformed requests ## Testing Instructions ```bash # Run the comprehensive OAuth2 test suite ./scripts/oauth2/test-mcp-oauth2.sh Manual Testing with Interactive Server # Start Coder in development mode ./scripts/develop.sh # In another terminal, set up test app and run interactive flow eval $(./scripts/oauth2/setup-test-app.sh) ./scripts/oauth2/test-manual-flow.sh # Opens browser with OAuth2 flow, handles callback automatically # Clean up when done ./scripts/oauth2/cleanup-test-app.sh Individual Component Testing # Test metadata endpoint curl -s http://localhost:3000/.well-known/oauth-authorization-server | jq . # Test PKCE generation ./scripts/oauth2/generate-pkce.sh # Run specific test suites go test -v ./coderd/identityprovider -run TestVerifyPKCE go test -v ./coderd -run TestOAuth2AuthorizationServerMetadata ``` ### Breaking Changes None. All changes maintain backward compatibility with existing OAuth2 flows. --- Change-Id: Ifbd0d9a543d545f9f56ecaa77ff2238542ff954a Signed-off-by: Thomas Kosiewski <tk@coder.com>
OAuth2 Test Scripts
This directory contains test scripts for the MCP OAuth2 implementation in Coder.
Prerequisites
-
Start Coder in development mode:
./scripts/develop.sh
-
Login to get a session token:
./scripts/coder-dev.sh login
Scripts
test-mcp-oauth2.sh
Complete automated test suite that verifies all OAuth2 functionality:
- Metadata endpoint
- PKCE flow
- Resource parameter support
- Token refresh
- Error handling
Usage:
chmod +x ./scripts/oauth2/test-mcp-oauth2.sh
./scripts/oauth2/test-mcp-oauth2.sh
setup-test-app.sh
Creates a test OAuth2 application and outputs environment variables.
Usage:
eval $(./scripts/oauth2/setup-test-app.sh)
echo "Client ID: $CLIENT_ID"
cleanup-test-app.sh
Deletes a test OAuth2 application.
Usage:
./scripts/oauth2/cleanup-test-app.sh $CLIENT_ID
# Or if CLIENT_ID is set as environment variable:
./scripts/oauth2/cleanup-test-app.sh
generate-pkce.sh
Generates PKCE code verifier and challenge for manual testing.
Usage:
./scripts/oauth2/generate-pkce.sh
test-manual-flow.sh
Launches a local Go web server to test the OAuth2 flow interactively. The server automatically handles the OAuth2 callback and token exchange, providing a user-friendly web interface with results.
Usage:
# First set up an app
eval $(./scripts/oauth2/setup-test-app.sh)
# Then run the test server
./scripts/oauth2/test-manual-flow.sh
Features:
- Starts a local web server on port 9876
- Automatically captures the authorization code
- Performs token exchange without manual intervention
- Displays results in a clean web interface
- Shows example API calls you can make with the token
oauth2-test-server.go
A Go web server that handles OAuth2 callbacks and token exchange. Used internally by test-manual-flow.sh
but can also be run standalone:
export CLIENT_ID="your-client-id"
export CLIENT_SECRET="your-client-secret"
export CODE_VERIFIER="your-code-verifier"
export STATE="your-state"
go run ./scripts/oauth2/oauth2-test-server.go
Example Workflow
-
Run automated tests:
./scripts/oauth2/test-mcp-oauth2.sh
-
Interactive browser testing:
# Create app eval $(./scripts/oauth2/setup-test-app.sh) # Run the test server (opens in browser automatically) ./scripts/oauth2/test-manual-flow.sh # - Opens authorization URL in terminal # - Handles callback automatically # - Shows token exchange results # Clean up when done ./scripts/oauth2/cleanup-test-app.sh
-
Generate PKCE for custom testing:
./scripts/oauth2/generate-pkce.sh # Use the generated values in your own curl commands
Environment Variables
All scripts respect these environment variables:
SESSION_TOKEN
: Coder session token (auto-read from.coderv2/session
)BASE_URL
: Coder server URL (default:http://localhost:3000
)CLIENT_ID
: OAuth2 client IDCLIENT_SECRET
: OAuth2 client secret
OAuth2 Endpoints
- Metadata:
GET /.well-known/oauth-authorization-server
- Authorization:
GET/POST /oauth2/authorize
- Token:
POST /oauth2/tokens
- Apps API:
/api/v2/oauth2-provider/apps