- Notifications
You must be signed in to change notification settings - Fork1k
feat: implement RFC 6750 Bearer token authentication#18644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -214,6 +214,31 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon | ||
return nil, nil, false | ||
} | ||
// Add WWW-Authenticate header for 401/403 responses (RFC 6750) | ||
if code == http.StatusUnauthorized || code == http.StatusForbidden { | ||
var wwwAuth string | ||
switch code { | ||
case http.StatusUnauthorized: | ||
// Map 401 to invalid_token with specific error descriptions | ||
switch { | ||
case strings.Contains(response.Message, "expired") || strings.Contains(response.Detail, "expired"): | ||
wwwAuth = `Bearer realm="coder", error="invalid_token", error_description="The access token has expired"` | ||
case strings.Contains(response.Message, "audience") || strings.Contains(response.Message, "mismatch"): | ||
wwwAuth = `Bearer realm="coder", error="invalid_token", error_description="The access token audience does not match this resource"` | ||
Comment on lines +225 to +228 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Very unfortunate we have to do string matches 😢 | ||
default: | ||
wwwAuth = `Bearer realm="coder", error="invalid_token", error_description="The access token is invalid"` | ||
} | ||
case http.StatusForbidden: | ||
// Map 403 to insufficient_scope per RFC 6750 | ||
wwwAuth = `Bearer realm="coder", error="insufficient_scope", error_description="The request requires higher privileges than provided by the access token"` | ||
default: | ||
wwwAuth = `Bearer realm="coder"` | ||
} | ||
rw.Header().Set("WWW-Authenticate", wwwAuth) | ||
} | ||
httpapi.Write(ctx, rw, code, response) | ||
return nil, nil, false | ||
} | ||
@@ -653,9 +678,14 @@ func UserRBACSubject(ctx context.Context, db database.Store, userID uuid.UUID, s | ||
// 1: The cookie | ||
// 2. The coder_session_token query parameter | ||
// 3. The custom auth header | ||
// 4. RFC 6750 Authorization: Bearer header | ||
// 5. RFC 6750 access_token query parameter | ||
// | ||
// API tokens for apps are read from workspaceapps/cookies.go. | ||
func APITokenFromRequest(r *http.Request) string { | ||
// Prioritize existing Coder custom authentication methods first | ||
// to maintain backward compatibility and existing behavior | ||
cookie, err := r.Cookie(codersdk.SessionTokenCookie) | ||
if err == nil && cookie.Value != "" { | ||
return cookie.Value | ||
@@ -671,7 +701,18 @@ func APITokenFromRequest(r *http.Request) string { | ||
return headerValue | ||
} | ||
// RFC 6750 Bearer Token support (added as fallback methods) | ||
// Check Authorization: Bearer <token> header (case-insensitive per RFC 6750) | ||
authHeader := r.Header.Get("Authorization") | ||
if strings.HasPrefix(strings.ToLower(authHeader), "bearer ") { | ||
return authHeader[7:] // Skip "Bearer " (7 characters) | ||
} | ||
// Check access_token query parameter | ||
accessToken := r.URL.Query().Get("access_token") | ||
if accessToken != "" { | ||
return accessToken | ||
} | ||
return "" | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.