From 635d2963f6f8c46d323b915b4fee248f175a39af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 13 Jan 2020 20:13:13 +0100 Subject: [PATCH] Add more explicit error handling around the OAuth flow --- auth/oauth.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/auth/oauth.go b/auth/oauth.go index 8325519fd..5e03d9a3b 100644 --- a/auth/oauth.go +++ b/auth/oauth.go @@ -2,6 +2,7 @@ package auth import ( "crypto/rand" + "errors" "fmt" "io" "io/ioutil" @@ -81,8 +82,13 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) { if err != nil { return } - defer resp.Body.Close() + + if resp.StatusCode != 200 { + err = fmt.Errorf("HTTP %d error while obtaining OAuth access token", resp.StatusCode) + return + } + body, err := ioutil.ReadAll(resp.Body) if err != nil { return @@ -92,6 +98,9 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) { return } accessToken = tokenValues.Get("access_token") + if accessToken == "" { + err = errors.New("the access token could not be read from HTTP response") + } return }