Skip to content

Commit

Permalink
oauth2: return error, error_description, and error_uri when error fie…
Browse files Browse the repository at this point in the history
…ld is present in token response
  • Loading branch information
beyang committed Oct 11, 2020
1 parent bf48bf1 commit 6057702
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions internal/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ type Token struct {
}

// tokenJSON is the struct representing the HTTP response from OAuth2
// providers returning a token in JSON form.
// providers returning a token or error in JSON form.
type tokenJSON struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
ExpiresIn expirationTime `json:"expires_in"` // at least PayPal returns string, while most return number
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
ExpiresIn expirationTime `json:"expires_in"` // at least PayPal returns string, while most return number
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
ErrorURI string `json:"error_uri"`
}

func (e *tokenJSON) expiry() (t time.Time) {
Expand Down Expand Up @@ -253,6 +256,13 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
if err != nil {
return nil, err
}
if tokenError := vals.Get("error"); tokenError != "" {
return nil, &TokenError{
Err: tokenError,
ErrorDescription: vals.Get("error_description"),
ErrorURI: vals.Get("error_uri"),
}
}
token = &Token{
AccessToken: vals.Get("access_token"),
TokenType: vals.Get("token_type"),
Expand All @@ -269,6 +279,13 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
if err = json.Unmarshal(body, &tj); err != nil {
return nil, err
}
if tj.Error != "" {
return nil, &TokenError{
Err: tj.Error,
ErrorDescription: tj.ErrorDescription,
ErrorURI: tj.ErrorURI,
}
}
token = &Token{
AccessToken: tj.AccessToken,
TokenType: tj.TokenType,
Expand All @@ -292,3 +309,13 @@ type RetrieveError struct {
func (r *RetrieveError) Error() string {
return fmt.Sprintf("oauth2: cannot fetch token: %v\nResponse: %s", r.Response.Status, r.Body)
}

type TokenError struct {
Err string
ErrorDescription string
ErrorURI string
}

func (t *TokenError) Error() string {
return fmt.Sprintf("oauth2: error in token fetch repsonse: %s\nerror_description: %s\nerror_uri: %s", t.Err, t.ErrorDescription, t.ErrorURI)
}

0 comments on commit 6057702

Please sign in to comment.