Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Body in RequestToken and AccessToken methods #54

Merged
merged 1 commit into from Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
dghubble marked this conversation as resolved.
Show resolved Hide resolved
}
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