From e56976839a233f27d1867d2c6dd7a2428362d7c7 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 22 Apr 2020 15:52:35 +0100 Subject: [PATCH] jwtutil: Fix error handling --- lib/jwtutil/jwtutil.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/jwtutil/jwtutil.go b/lib/jwtutil/jwtutil.go index e2f356c26..f99b36b97 100644 --- a/lib/jwtutil/jwtutil.go +++ b/lib/jwtutil/jwtutil.go @@ -6,10 +6,14 @@ import ( "crypto/rsa" "encoding/hex" "encoding/json" + "io" + "io/ioutil" "net/http" + "strings" "time" "github.com/pkg/errors" + "github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs/config/configmap" "github.com/rclone/rclone/lib/oauthutil" @@ -54,7 +58,13 @@ func Config(id, name string, claims *jws.ClaimSet, header *jws.Header, queryPara if err != nil { return errors.Wrap(err, "jwtutil: failed making auth request") } + + s, err := bodyToString(resp.Body) + if err != nil { + fs.Debugf(nil, "jwtutil: failed to get response body") + } if resp.StatusCode != 200 { + err = errors.New(resp.Status) return errors.Wrap(err, "jwtutil: failed making auth request") } defer func() { @@ -65,8 +75,11 @@ func Config(id, name string, claims *jws.ClaimSet, header *jws.Header, queryPara }() result := &response{} - err = json.NewDecoder(resp.Body).Decode(result) - if err != nil || result.AccessToken == "" { + err = json.NewDecoder(strings.NewReader(s)).Decode(result) + if result.AccessToken == "" && err == nil { + err = errors.New("No AccessToken in Response") + } + if err != nil { return errors.Wrap(err, "jwtutil: failed to get token") } token := &oauth2.Token{ @@ -80,6 +93,16 @@ func Config(id, name string, claims *jws.ClaimSet, header *jws.Header, queryPara return oauthutil.PutToken(name, m, token, true) } +func bodyToString(responseBody io.Reader) (bodyString string, err error) { + bodyBytes, err := ioutil.ReadAll(responseBody) + if err != nil { + return "", err + } + bodyString = string(bodyBytes) + fs.Debugf(nil, "jwtutil: Response Body: "+bodyString) + return bodyString, nil +} + type response struct { AccessToken string `json:"access_token"` TokenType string `json:"token_type"`