mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
0d3b7703f7
Change-Id: Ic33bc383d00d0e354c25a0dd6080a4307d9862b6 Signed-off-by: Thomas Kosiewski <tk@coder.com>
5.8 KiB
5.8 KiB
OAuth2 Development Guide
RFC Compliance Development
Implementing Standard Protocols
When implementing standard protocols (OAuth2, OpenID Connect, etc.):
-
Fetch and Analyze Official RFCs:
- Always read the actual RFC specifications before implementation
- Use WebFetch tool to get current RFC content for compliance verification
- Document RFC requirements in code comments
-
Default Values Matter:
- Pay close attention to RFC-specified default values
- Example: RFC 7591 specifies
client_secret_basicas default, notclient_secret_post - Ensure consistency between database migrations and application code
-
Security Requirements:
- Follow RFC security considerations precisely
- Example: RFC 7592 prohibits returning registration access tokens in GET responses
- Implement proper error responses per protocol specifications
-
Validation Compliance:
- Implement comprehensive validation per RFC requirements
- Support protocol-specific features (e.g., custom schemes for native OAuth2 apps)
- Test edge cases defined in specifications
OAuth2 Provider Implementation
OAuth2 Spec Compliance
-
Follow RFC 6749 for token responses
- Use
expires_in(seconds) notexpiry(timestamp) in token responses - Return proper OAuth2 error format:
{"error": "code", "error_description": "details"}
- Use
-
Error Response Format
- Create OAuth2-compliant error responses for token endpoint
- Use standard error codes:
invalid_client,invalid_grant,invalid_request - Avoid generic error responses for OAuth2 endpoints
PKCE Implementation
- Support both with and without PKCE for backward compatibility
- Use S256 method for code challenge
- Properly validate code_verifier against stored code_challenge
UI Authorization Flow
- Use POST requests for consent, not GET with links
- Avoid dependency on referer headers for security decisions
- Support proper state parameter validation
RFC 8707 Resource Indicators
- Store resource parameters in database for server-side validation (opaque tokens)
- Validate resource consistency between authorization and token requests
- Support audience validation in refresh token flows
- Resource parameter is optional but must be consistent when provided
OAuth2 Error Handling Pattern
// Define specific OAuth2 errors
var (
errInvalidPKCE = xerrors.New("invalid code_verifier")
)
// Use OAuth2-compliant error responses
type OAuth2Error struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description,omitempty"`
}
// Return proper OAuth2 errors
if errors.Is(err, errInvalidPKCE) {
writeOAuth2Error(ctx, rw, http.StatusBadRequest, "invalid_grant", "The PKCE code verifier is invalid")
return
}
Testing OAuth2 Features
Test Scripts
Located in ./scripts/oauth2/:
test-mcp-oauth2.sh- Full automated test suitesetup-test-app.sh- Create test OAuth2 appcleanup-test-app.sh- Remove test appgenerate-pkce.sh- Generate PKCE parameterstest-manual-flow.sh- Manual browser testing
Always run the full test suite after OAuth2 changes:
./scripts/oauth2/test-mcp-oauth2.sh
RFC Protocol Testing
-
Compliance Test Coverage:
- Test all RFC-defined error codes and responses
- Validate proper HTTP status codes for different scenarios
- Test protocol-specific edge cases (URI formats, token formats, etc.)
-
Security Boundary Testing:
- Test client isolation and privilege separation
- Verify information disclosure protections
- Test token security and proper invalidation
Common OAuth2 Issues
- OAuth2 endpoints returning wrong error format - Ensure OAuth2 endpoints return RFC 6749 compliant errors
- Resource indicator validation failing - Ensure database stores and retrieves resource parameters correctly
- PKCE tests failing - Verify both authorization code storage and token exchange handle PKCE fields
- RFC compliance failures - Verify against actual RFC specifications, not assumptions
- Authorization context errors in public endpoints - Use
dbauthz.AsSystemRestricted(ctx)pattern - Default value mismatches - Ensure database migrations match application code defaults
- Bearer token authentication issues - Check token extraction precedence and format validation
- URI validation failures - Support both standard schemes and custom schemes per protocol requirements
Authorization Context Patterns
// Public endpoints needing system access (OAuth2 registration)
app, err := api.Database.GetOAuth2ProviderAppByClientID(dbauthz.AsSystemRestricted(ctx), clientID)
// Authenticated endpoints with user context
app, err := api.Database.GetOAuth2ProviderAppByClientID(ctx, clientID)
// System operations in middleware
roles, err := db.GetAuthorizationUserRoles(dbauthz.AsSystemRestricted(ctx), userID)
OAuth2/Authentication Work Patterns
- Types go in
codersdk/oauth2.goor similar - Handlers go in
coderd/oauth2.goorcoderd/identityprovider/ - Database fields need migration + audit table updates
- Always support backward compatibility
Protocol Implementation Checklist
Before completing OAuth2 or authentication feature work:
- Verify RFC compliance by reading actual specifications
- Implement proper error response formats per protocol
- Add comprehensive validation for all protocol fields
- Test security boundaries and token handling
- Update RBAC permissions for new resources
- Add audit logging support if applicable
- Create database migrations with proper defaults
- Add comprehensive test coverage including edge cases
- Verify linting compliance
- Test both positive and negative scenarios
- Document protocol-specific patterns and requirements