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: Dalton Hubble <dghubble@mgmail.com>
  • Loading branch information
2 people authored and dghubble committed Oct 2, 2021
1 parent 3422448 commit 0a5525c
Showing 1 changed file with 12 additions and 8 deletions.
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 0a5525c

Please sign in to comment.