@@ -26,42 +26,6 @@ import (
2626// This is used in SEP-990 to exchange an ID-JAG for an access token at the MCP Server.
2727const GrantTypeJWTBearer = "urn:ietf:params:oauth:grant-type:jwt-bearer"
2828
29- // JWTBearerResponse represents the response from a JWT Bearer grant request
30- // per RFC 7523. This uses the standard OAuth 2.0 token response format.
31- type JWTBearerResponse struct {
32- // AccessToken is the OAuth access token issued by the MCP Server's
33- // authorization server.
34- AccessToken string `json:"access_token"`
35- // TokenType is the type of token issued. This is typically "Bearer".
36- TokenType string `json:"token_type"`
37- // ExpiresIn is the lifetime in seconds of the access token.
38- ExpiresIn int `json:"expires_in,omitempty"`
39- // RefreshToken is the refresh token, which can be used to obtain new
40- // access tokens using the same authorization grant.
41- RefreshToken string `json:"refresh_token,omitempty"`
42- // Scope is the scope of the access token as described by RFC 6749 Section 3.3.
43- Scope string `json:"scope,omitempty"`
44- }
45-
46- // JWTBearerError represents an error response from a JWT Bearer grant request.
47- type JWTBearerError struct {
48- // ErrorCode is the error code as defined in RFC 6749 Section 5.2.
49- // The JSON field name is "error" per the RFC specification.
50- ErrorCode string `json:"error"`
51- // ErrorDescription is a human-readable description of the error.
52- ErrorDescription string `json:"error_description,omitempty"`
53- // ErrorURI is a URI identifying a human-readable web page with information
54- // about the error.
55- ErrorURI string `json:"error_uri,omitempty"`
56- }
57-
58- func (e * JWTBearerError ) Error () string {
59- if e .ErrorDescription != "" {
60- return fmt .Sprintf ("JWT bearer grant failed: %s (%s)" , e .ErrorCode , e .ErrorDescription )
61- }
62- return fmt .Sprintf ("JWT bearer grant failed: %s" , e .ErrorCode )
63- }
64-
6529// ExchangeJWTBearer exchanges an Identity Assertion JWT Authorization Grant (ID-JAG)
6630// for an access token using JWT Bearer Grant per RFC 7523. This is the second step
6731// in Enterprise Managed Authorization (SEP-990) after obtaining the ID-JAG from the
@@ -151,7 +115,13 @@ func ExchangeJWTBearer(
151115 }
152116 // Handle success response (200 OK per OAuth 2.0)
153117 if httpResp .StatusCode == http .StatusOK {
154- var resp JWTBearerResponse
118+ var resp struct {
119+ AccessToken string `json:"access_token"`
120+ TokenType string `json:"token_type"`
121+ ExpiresIn int `json:"expires_in,omitempty`
122+ RefreshToken string `json:"refresh_token,omitempty"`
123+ Scope string `json:"scope,omitempty"`
124+ }
155125 if err := json .Unmarshal (body , & resp ); err != nil {
156126 return nil , fmt .Errorf ("failed to parse JWT bearer grant response: %w (body: %s)" , err , string (body ))
157127 }
@@ -182,12 +152,25 @@ func ExchangeJWTBearer(
182152 }
183153 // Handle error response (400 Bad Request per RFC 6749)
184154 if httpResp .StatusCode == http .StatusBadRequest {
185- var errResp JWTBearerError
155+ var errResp struct {
156+ Error string `json:"error"`
157+ ErrorDescription string `json:"error_description,omitempty"`
158+ ErrorURI string `json:"error_uri,omitempty"`
159+ }
186160 if err := json .Unmarshal (body , & errResp ); err != nil {
187161 return nil , fmt .Errorf ("failed to parse error response: %w (body: %s)" , err , string (body ))
188162 }
189- return nil , & errResp
163+ return nil , & oauth2.RetrieveError {
164+ Response : httpResp ,
165+ Body : body ,
166+ ErrorCode : errResp .Error ,
167+ ErrorDescription : errResp .ErrorDescription ,
168+ ErrorURI : errResp .ErrorURI ,
169+ }
190170 }
191171 // Handle unexpected status codes
192- return nil , fmt .Errorf ("unexpected status code %d: %s" , httpResp .StatusCode , string (body ))
172+ return nil , & oauth2.RetrieveError {
173+ Response : httpResp ,
174+ Body : body ,
175+ }
193176}
0 commit comments