@@ -58,11 +58,17 @@ type Token struct {
58
58
59
59
// tokenJSON is the struct representing the HTTP response from OAuth2
60
60
// providers returning a token in JSON form.
61
+ // https://datatracker.ietf.org/doc/html/rfc6749#section-5.1
61
62
type tokenJSON struct {
62
63
AccessToken string `json:"access_token"`
63
64
TokenType string `json:"token_type"`
64
65
RefreshToken string `json:"refresh_token"`
65
66
ExpiresIn expirationTime `json:"expires_in"` // at least PayPal returns string, while most return number
67
+ // error fields
68
+ // https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
69
+ Error string `json:"error"`
70
+ ErrorDescription string `json:"error_description"`
71
+ ErrorUri string `json:"error_uri"`
66
72
}
67
73
68
74
func (e * tokenJSON ) expiry () (t time.Time ) {
@@ -238,11 +244,12 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
238
244
if err != nil {
239
245
return nil , fmt .Errorf ("oauth2: cannot fetch token: %v" , err )
240
246
}
241
- if code := r .StatusCode ; code < 200 || code > 299 {
242
- return nil , & RetrieveError {
243
- Response : r ,
244
- Body : body ,
245
- }
247
+
248
+ failureStatus := r .StatusCode < 200 || r .StatusCode > 299
249
+ retrieveError := & RetrieveError {
250
+ Response : r ,
251
+ Body : body ,
252
+ // error details populated below
246
253
}
247
254
248
255
var token * Token
@@ -251,8 +258,11 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
251
258
case "application/x-www-form-urlencoded" , "text/plain" :
252
259
vals , err := url .ParseQuery (string (body ))
253
260
if err != nil {
254
- return nil , err
261
+ return nil , fmt . Errorf ( "oauth2: cannot parse response: %v" , err )
255
262
}
263
+ retrieveError .ErrorCode = vals .Get ("error" )
264
+ retrieveError .ErrorDescription = vals .Get ("error_description" )
265
+ retrieveError .ErrorCode = vals .Get ("error" )
256
266
token = & Token {
257
267
AccessToken : vals .Get ("access_token" ),
258
268
TokenType : vals .Get ("token_type" ),
@@ -267,8 +277,14 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
267
277
default :
268
278
var tj tokenJSON
269
279
if err = json .Unmarshal (body , & tj ); err != nil {
270
- return nil , err
280
+ if failureStatus {
281
+ return nil , retrieveError
282
+ }
283
+ return nil , fmt .Errorf ("oauth2: cannot parse json: %v" , err )
271
284
}
285
+ retrieveError .ErrorCode = tj .Error
286
+ retrieveError .ErrorDescription = tj .ErrorDescription
287
+ retrieveError .ErrorUri = tj .ErrorUri
272
288
token = & Token {
273
289
AccessToken : tj .AccessToken ,
274
290
TokenType : tj .TokenType ,
@@ -278,17 +294,27 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
278
294
}
279
295
json .Unmarshal (body , & token .Raw ) // no error checks for optional fields
280
296
}
297
+ if failureStatus || retrieveError .ErrorCode != "" {
298
+ return nil , retrieveError
299
+ }
281
300
if token .AccessToken == "" {
282
301
return nil , errors .New ("oauth2: server response missing access_token" )
283
302
}
284
303
return token , nil
285
304
}
286
305
306
+ // mirrors oauth2.RetrieveError
287
307
type RetrieveError struct {
288
- Response * http.Response
289
- Body []byte
308
+ Response * http.Response
309
+ Body []byte
310
+ ErrorCode string
311
+ ErrorDescription string
312
+ ErrorUri string
290
313
}
291
314
292
315
func (r * RetrieveError ) Error () string {
316
+ if r .ErrorCode != "" {
317
+ return fmt .Sprintf ("oauth2: cannot fetch token: %s %s %s" , r .ErrorCode , r .ErrorDescription , r .ErrorUri )
318
+ }
293
319
return fmt .Sprintf ("oauth2: cannot fetch token: %v\n Response: %s" , r .Response .Status , r .Body )
294
320
}
0 commit comments