Skip to content

Commit 017f23f

Browse files
committed
feat(oauth2): implement RFC 6750 Bearer Token Support for MCP compliance
- Add RFC 6750 bearer token extraction to APITokenFromRequest as fallback methods - Support Authorization: Bearer <token> header and access_token query parameter - Maintain backward compatibility by prioritizing existing custom methods first - Add WWW-Authenticate headers to 401/403 responses per RFC 6750 - Update Protected Resource Metadata to advertise bearer_methods_supported - Add comprehensive test suite for RFC 6750 compliance in rfc6750_test.go - Update MCP test scripts with bearer token authentication tests - Enhance CLAUDE.md with improved Go LSP tool usage guidelines Implements RFC 6750 Section 2.1 (Authorization Request Header Field) and 2.3 (URI Query Parameter). Maintains full backward compatibility with existing Coder authentication methods. Completes major MCP OAuth2 compliance milestone. Change-Id: Ic9c9057153b40728ad91b377d753a7ffd566add7 Signed-off-by: Thomas Kosiewski <tk@coder.com>
1 parent 01a10ef commit 017f23f

File tree

6 files changed

+1004
-7
lines changed

6 files changed

+1004
-7
lines changed

coderd/httpmw/apikey.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,26 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
214214
return nil, nil, false
215215
}
216216

217+
// Add WWW-Authenticate header for 401/403 responses (RFC 6750)
218+
if code == http.StatusUnauthorized || code == http.StatusForbidden {
219+
// Basic Bearer challenge with realm
220+
wwwAuth := `Bearer realm="coder"`
221+
222+
// Add error details based on the type of error
223+
switch {
224+
case strings.Contains(response.Message, "invalid") || strings.Contains(response.Detail, "invalid"):
225+
wwwAuth = `Bearer realm="coder", error="invalid_token", error_description="The access token is invalid"`
226+
case strings.Contains(response.Message, "expired") || strings.Contains(response.Detail, "expired"):
227+
wwwAuth = `Bearer realm="coder", error="invalid_token", error_description="The access token has expired"`
228+
case strings.Contains(response.Message, "audience") || strings.Contains(response.Message, "mismatch"):
229+
wwwAuth = `Bearer realm="coder", error="invalid_token", error_description="The access token audience does not match this resource"`
230+
case code == http.StatusForbidden:
231+
wwwAuth = `Bearer realm="coder", error="insufficient_scope", error_description="The request requires higher privileges than provided by the access token"`
232+
}
233+
234+
rw.Header().Set("WWW-Authenticate", wwwAuth)
235+
}
236+
217237
httpapi.Write(ctx, rw, code, response)
218238
return nil, nil, false
219239
}
@@ -720,9 +740,14 @@ func UserRBACSubject(ctx context.Context, db database.Store, userID uuid.UUID, s
720740
// 1: The cookie
721741
// 2. The coder_session_token query parameter
722742
// 3. The custom auth header
743+
// 4. RFC 6750 Authorization: Bearer header
744+
// 5. RFC 6750 access_token query parameter
723745
//
724746
// API tokens for apps are read from workspaceapps/cookies.go.
725747
func APITokenFromRequest(r *http.Request) string {
748+
// Prioritize existing Coder custom authentication methods first
749+
// to maintain backward compatibility and existing behavior
750+
726751
cookie, err := r.Cookie(codersdk.SessionTokenCookie)
727752
if err == nil && cookie.Value != "" {
728753
return cookie.Value
@@ -738,7 +763,18 @@ func APITokenFromRequest(r *http.Request) string {
738763
return headerValue
739764
}
740765

741-
// TODO(ThomasK33): Implement RFC 6750
766+
// RFC 6750 Bearer Token support (added as fallback methods)
767+
// Check Authorization: Bearer <token> header
768+
authHeader := r.Header.Get("Authorization")
769+
if strings.HasPrefix(authHeader, "Bearer ") {
770+
return strings.TrimPrefix(authHeader, "Bearer ")
771+
}
772+
773+
// Check access_token query parameter
774+
accessToken := r.URL.Query().Get("access_token")
775+
if accessToken != "" {
776+
return accessToken
777+
}
742778

743779
return ""
744780
}

0 commit comments

Comments
 (0)