Skip to content

Commit

Permalink
Read Body in RequestToken and AccessToken methods
Browse files Browse the repository at this point in the history
* For non-nil RequestToken or AccessToken requests, read the
body before checking the status code, in case it has useful
information to include

Co-authored-by: Dennis Schridde <dennis.schridde@freiheit.com>
Co-authored-by: Dalton Hubble <dghubble@mgmail.com>
  • Loading branch information
2 people authored and dghubble committed Oct 2, 2021
1 parent 3422448 commit 08b3906
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -4,6 +4,8 @@ Notable changes between releases.

## Latest

* Show body when `RequestToken` or `AccessToken` requests return an invalid status code ([#54](https://github.com/dghubble/oauth1/pull/54))

## v0.7.0

* Add an `HMAC256Signer` ([#40](https://github.com/dghubble/oauth1/pull/40))
Expand Down
20 changes: 12 additions & 8 deletions config.go
Expand Up @@ -79,13 +79,15 @@ func (c *Config) RequestToken() (requestToken, requestSecret string, err error)
}
// when err is nil, resp contains a non-nil resp.Body which must be closed
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return "", "", fmt.Errorf("oauth1: Server returned status %d", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", "", err
return "", "", fmt.Errorf("oauth1: error reading Body: %v", err)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return "", "", fmt.Errorf("oauth1: invalid status %d: %s", resp.StatusCode, body)
}

// ParseQuery to decode URL-encoded application/x-www-form-urlencoded body
values, err := url.ParseQuery(string(body))
if err != nil {
Expand Down Expand Up @@ -156,13 +158,15 @@ func (c *Config) AccessToken(requestToken, requestSecret, verifier string) (acce
}
// when err is nil, resp contains a non-nil resp.Body which must be closed
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return "", "", fmt.Errorf("oauth1: Server returned status %d", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", "", err
return "", "", fmt.Errorf("oauth1: error reading Body: %v", err)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return "", "", fmt.Errorf("oauth1: invalid status %d: %s", resp.StatusCode, body)
}

// ParseQuery to decode URL-encoded application/x-www-form-urlencoded body
values, err := url.ParseQuery(string(body))
if err != nil {
Expand Down

0 comments on commit 08b3906

Please sign in to comment.