mirror of
https://github.com/rclone/rclone
synced 2024-12-26 18:23:45 +01:00
jwtutil: Fix error handling
This commit is contained in:
parent
0c0ed2fe04
commit
e56976839a
@ -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"`
|
||||
|
Loading…
Reference in New Issue
Block a user