MCPcopy Create free account
hub / github.com/bytebase/bytebase / handleToken

Method handleToken

backend/api/oauth2/token.go:38–77  ·  view source on GitHub ↗
(c *echo.Context)

Source from the content-addressed store, hash-verified

36}
37
38func (s *Service) handleToken(c *echo.Context) error {
39 ctx := c.Request().Context()
40
41 var req tokenRequest
42 if err := c.Bind(&req); err != nil {
43 return oauth2Error(c, http.StatusBadRequest, "invalid_request", "failed to parse request")
44 }
45
46 // Authenticate client
47 clientID, clientSecret := s.extractClientCredentials(c, &req)
48 if clientID == "" {
49 return oauth2Error(c, http.StatusUnauthorized, "invalid_client", "client authentication required")
50 }
51
52 client, err := s.store.GetOAuth2Client(ctx, clientID)
53 if err != nil {
54 return oauth2Error(c, http.StatusInternalServerError, "server_error", "failed to lookup client")
55 }
56 if client == nil {
57 return oauth2Error(c, http.StatusUnauthorized, "invalid_client", "client not found")
58 }
59
60 // Verify client credentials based on token_endpoint_auth_method
61 // Public clients (token_endpoint_auth_method: none) don't have secrets
62 if client.Config.TokenEndpointAuthMethod != "none" {
63 if !verifySecret(client.ClientSecretHash, clientSecret) {
64 return oauth2Error(c, http.StatusUnauthorized, "invalid_client", "invalid client credentials")
65 }
66 }
67
68 // Handle grant types
69 switch req.GrantType {
70 case "authorization_code":
71 return s.handleAuthorizationCodeGrant(c, client, &req)
72 case "refresh_token":
73 return s.handleRefreshTokenGrant(c, client, &req)
74 default:
75 return oauth2Error(c, http.StatusBadRequest, "unsupported_grant_type", "grant_type must be authorization_code or refresh_token")
76 }
77}
78
79func (*Service) extractClientCredentials(c *echo.Context, req *tokenRequest) (clientID, clientSecret string) {
80 // Try Basic auth first

Callers

nothing calls this directly

Calls 6

oauth2ErrorFunction · 0.85
verifySecretFunction · 0.85
GetOAuth2ClientMethod · 0.80

Tested by

no test coverage detected