Skip to content

Commit f6785c2

Browse files
authored
Merge pull request markbates#107 from jcbwlkr/github-404
Handle non-200 responses from GitHub API endpoints
2 parents fe9a7e2 + 0da86db commit f6785c2

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

providers/github/github.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"bytes"
77
"encoding/json"
88
"errors"
9+
"fmt"
910
"io"
1011
"io/ioutil"
1112
"net/http"
@@ -24,6 +25,7 @@ import (
2425
// github.AuthURL = "https://github.acme.com/login/oauth/authorize
2526
// github.TokenURL = "https://github.acme.com/login/oauth/access_token
2627
// github.ProfileURL = "https://github.acme.com/api/v3/user
28+
// github.EmailURL = "https://github.acme.com/api/v3/user/emails
2729
var (
2830
AuthURL = "https://github.com/login/oauth/authorize"
2931
TokenURL = "https://github.com/login/oauth/access_token"
@@ -86,6 +88,10 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
8688
}
8789
defer response.Body.Close()
8890

91+
if response.StatusCode != http.StatusOK {
92+
return user, fmt.Errorf("GitHub API responded with a %d trying to fetch user information", response.StatusCode)
93+
}
94+
8995
bits, err := ioutil.ReadAll(response.Body)
9096
if err != nil {
9197
return user, err
@@ -144,13 +150,18 @@ func userFromReader(reader io.Reader, user *goth.User) error {
144150

145151
func getPrivateMail(p *Provider, sess *Session) (email string, err error) {
146152
response, err := http.Get(EmailURL + "?access_token=" + url.QueryEscape(sess.AccessToken))
147-
defer response.Body.Close()
148153
if err != nil {
149154
if response != nil {
150155
response.Body.Close()
151156
}
152157
return email, err
153158
}
159+
defer response.Body.Close()
160+
161+
if response.StatusCode != http.StatusOK {
162+
return email, fmt.Errorf("GitHub API responded with a %d trying to fetch user email", response.StatusCode)
163+
}
164+
154165
var mailList = []struct {
155166
Email string `json:"email"`
156167
Primary bool `json:"primary"`

0 commit comments

Comments
 (0)