mirror of https://github.com/rclone/rclone.git
jwtutil: Fix error handling
This commit is contained in:
parent
0c0ed2fe04
commit
e56976839a
|
@ -6,10 +6,14 @@ import (
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/rclone/rclone/fs"
|
||||||
"github.com/rclone/rclone/fs/config/configmap"
|
"github.com/rclone/rclone/fs/config/configmap"
|
||||||
"github.com/rclone/rclone/lib/oauthutil"
|
"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 {
|
if err != nil {
|
||||||
return errors.Wrap(err, "jwtutil: failed making auth request")
|
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 {
|
if resp.StatusCode != 200 {
|
||||||
|
err = errors.New(resp.Status)
|
||||||
return errors.Wrap(err, "jwtutil: failed making auth request")
|
return errors.Wrap(err, "jwtutil: failed making auth request")
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -65,8 +75,11 @@ func Config(id, name string, claims *jws.ClaimSet, header *jws.Header, queryPara
|
||||||
}()
|
}()
|
||||||
|
|
||||||
result := &response{}
|
result := &response{}
|
||||||
err = json.NewDecoder(resp.Body).Decode(result)
|
err = json.NewDecoder(strings.NewReader(s)).Decode(result)
|
||||||
if err != nil || result.AccessToken == "" {
|
if result.AccessToken == "" && err == nil {
|
||||||
|
err = errors.New("No AccessToken in Response")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
return errors.Wrap(err, "jwtutil: failed to get token")
|
return errors.Wrap(err, "jwtutil: failed to get token")
|
||||||
}
|
}
|
||||||
token := &oauth2.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)
|
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 {
|
type response struct {
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
TokenType string `json:"token_type"`
|
TokenType string `json:"token_type"`
|
||||||
|
|
Loading…
Reference in New Issue