Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

jwtutil: Fix error handling

Davide56976839a23
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"`