|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**oauth-mcp-proxy** is an OAuth 2.1 authentication library for Go MCP servers. It provides server-side OAuth integration with minimal code (3-line integration via `WithOAuth()`), supporting multiple providers (HMAC, Okta, Google, Azure AD). |
| 8 | + |
| 9 | +## Build Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Run tests |
| 13 | +make test |
| 14 | + |
| 15 | +# Run tests with verbose output |
| 16 | +make test-verbose |
| 17 | + |
| 18 | +# Run tests with coverage report (generates coverage.html) |
| 19 | +make test-coverage |
| 20 | + |
| 21 | +# Run linters (same as CI - checks go.mod tidy + golangci-lint) |
| 22 | +make lint |
| 23 | + |
| 24 | +# Format code |
| 25 | +make fmt |
| 26 | + |
| 27 | +# Clean build artifacts and caches |
| 28 | +make clean |
| 29 | + |
| 30 | +# Install/download dependencies |
| 31 | +make install |
| 32 | + |
| 33 | +# Check for security vulnerabilities |
| 34 | +make vuln |
| 35 | +``` |
| 36 | + |
| 37 | +## Architecture |
| 38 | + |
| 39 | +### Core Components |
| 40 | + |
| 41 | +1. **oauth.go** - Main entry point, provides `WithOAuth()` function that creates OAuth server and returns MCP server option |
| 42 | +2. **config.go** - Configuration validation and provider setup |
| 43 | +3. **middleware.go** - Token validation middleware with 5-minute caching |
| 44 | +4. **handlers.go** - OAuth HTTP endpoints (/.well-known/*, /oauth/*) |
| 45 | +5. **provider/provider.go** - Token validators (HMACValidator, OIDCValidator) |
| 46 | + |
| 47 | +### Key Design Patterns |
| 48 | + |
| 49 | +- **Instance-scoped**: Each `Server` instance has its own token cache and validator (no globals) |
| 50 | +- **Provider abstraction**: `TokenValidator` interface supports multiple OAuth providers |
| 51 | +- **Caching strategy**: Tokens cached for 5 minutes using SHA-256 hash as key |
| 52 | +- **Context propagation**: OAuth token extracted from HTTP header → stored in context → validated by middleware → user added to context |
| 53 | + |
| 54 | +### Integration Flow |
| 55 | + |
| 56 | +``` |
| 57 | +1. HTTP request with "Authorization: Bearer <token>" header |
| 58 | +2. CreateHTTPContextFunc() extracts token → adds to context via WithOAuthToken() |
| 59 | +3. OAuth middleware (Server.Middleware()) validates token: |
| 60 | + - Checks cache first (5-minute TTL) |
| 61 | + - If not cached, validates via provider (HMAC or OIDC) |
| 62 | + - Caches result |
| 63 | +4. Adds authenticated User to context via userContextKey |
| 64 | +5. Tool handler accesses user via GetUserFromContext(ctx) |
| 65 | +``` |
| 66 | + |
| 67 | +### Provider System |
| 68 | + |
| 69 | +- **HMAC**: Validates JWT tokens with shared secret (testing/dev) |
| 70 | +- **OIDC**: Validates tokens via JWKS/OIDC discovery (Okta/Google/Azure) |
| 71 | +- All validation happens in `provider/provider.go` |
| 72 | +- Validators implement `TokenValidator` interface |
| 73 | + |
| 74 | +## Testing |
| 75 | + |
| 76 | +The codebase has extensive test coverage across multiple scenarios: |
| 77 | + |
| 78 | +- **api_test.go** - Core API functionality tests |
| 79 | +- **integration_test.go** - End-to-end integration tests |
| 80 | +- **security_test.go** - Security validation tests |
| 81 | +- **attack_scenarios_test.go** - Security attack scenario tests |
| 82 | +- **middleware_compatibility_test.go** - Middleware compatibility tests |
| 83 | +- **provider/provider_test.go** - Token validator tests |
| 84 | + |
| 85 | +Run single test: |
| 86 | +```bash |
| 87 | +go test -v -run TestName ./... |
| 88 | +``` |
| 89 | + |
| 90 | +## Important Notes |
| 91 | + |
| 92 | +1. **User Context**: Always use `GetUserFromContext(ctx)` in tool handlers to access authenticated user |
| 93 | +2. **Token Caching**: Tokens cached for 5 minutes - design for this TTL in testing |
| 94 | +3. **Logging**: Config.Logger is optional. If nil, uses default logger (log.Printf with level prefixes) |
| 95 | +4. **Modes**: Library supports "native" (token validation only) and "proxy" (OAuth flow proxy) modes |
| 96 | +5. **Security**: All redirect URIs validated, state parameters HMAC-signed, tokens never logged (only hash previews) |
0 commit comments