2022-08-28 11:21:57 +00:00
|
|
|
// Package jwtutil provides JWT utilities.
|
2019-09-20 12:26:29 +00:00
|
|
|
package jwtutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2021-11-04 10:12:57 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-04-22 14:52:35 +00:00
|
|
|
"io"
|
2019-09-20 12:26:29 +00:00
|
|
|
"net/http"
|
2020-04-22 14:52:35 +00:00
|
|
|
"strings"
|
2019-09-20 12:26:29 +00:00
|
|
|
"time"
|
|
|
|
|
2022-06-25 13:33:12 +00:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2020-04-22 14:52:35 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-09-20 12:26:29 +00:00
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/lib/oauthutil"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RandomHex creates a random string of the given length
|
|
|
|
func RandomHex(n int) (string, error) {
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(bytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config configures rclone using JWT
|
2022-06-25 13:33:12 +00:00
|
|
|
func Config(id, name, url string, claims jwt.Claims, headerParams map[string]interface{}, queryParams map[string]string, privateKey *rsa.PrivateKey, m configmap.Mapper, client *http.Client) (err error) {
|
|
|
|
jwtToken := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
|
|
|
|
for key, value := range headerParams {
|
|
|
|
jwtToken.Header[key] = value
|
|
|
|
}
|
|
|
|
payload, err := jwtToken.SignedString(privateKey)
|
2019-09-20 12:26:29 +00:00
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("jwtutil: failed to encode payload: %w", err)
|
2019-09-20 12:26:29 +00:00
|
|
|
}
|
2022-06-25 13:33:12 +00:00
|
|
|
req, err := http.NewRequest("POST", url, nil)
|
2019-09-20 12:26:29 +00:00
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("jwtutil: failed to create new request: %w", err)
|
2019-09-20 12:26:29 +00:00
|
|
|
}
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer")
|
|
|
|
q.Add("assertion", payload)
|
|
|
|
for key, value := range queryParams {
|
|
|
|
q.Add(key, value)
|
|
|
|
}
|
|
|
|
queryString := q.Encode()
|
|
|
|
|
2022-06-25 13:33:12 +00:00
|
|
|
req, err = http.NewRequest("POST", url, bytes.NewBuffer([]byte(queryString)))
|
2019-09-20 12:26:29 +00:00
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("jwtutil: failed to create new request: %w", err)
|
2019-09-20 12:26:29 +00:00
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("jwtutil: failed making auth request: %w", err)
|
2019-09-20 12:26:29 +00:00
|
|
|
}
|
2020-04-22 14:52:35 +00:00
|
|
|
|
|
|
|
s, err := bodyToString(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(nil, "jwtutil: failed to get response body")
|
|
|
|
}
|
2019-09-20 12:26:29 +00:00
|
|
|
if resp.StatusCode != 200 {
|
2020-04-22 14:52:35 +00:00
|
|
|
err = errors.New(resp.Status)
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("jwtutil: failed making auth request: %w", err)
|
2019-09-20 12:26:29 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
2022-08-14 02:56:32 +00:00
|
|
|
deferredErr := resp.Body.Close()
|
|
|
|
if deferredErr != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
err = fmt.Errorf("jwtutil: failed to close resp.Body: %w", err)
|
2019-09-20 12:26:29 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
result := &response{}
|
2020-04-22 14:52:35 +00:00
|
|
|
err = json.NewDecoder(strings.NewReader(s)).Decode(result)
|
|
|
|
if result.AccessToken == "" && err == nil {
|
2022-06-08 20:54:39 +00:00
|
|
|
err = errors.New("no AccessToken in Response")
|
2020-04-22 14:52:35 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("jwtutil: failed to get token: %w", err)
|
2019-09-20 12:26:29 +00:00
|
|
|
}
|
|
|
|
token := &oauth2.Token{
|
|
|
|
AccessToken: result.AccessToken,
|
|
|
|
TokenType: result.TokenType,
|
|
|
|
}
|
|
|
|
e := result.ExpiresIn
|
|
|
|
if e != 0 {
|
|
|
|
token.Expiry = time.Now().Add(time.Duration(e) * time.Second)
|
|
|
|
}
|
|
|
|
return oauthutil.PutToken(name, m, token, true)
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:52:35 +00:00
|
|
|
func bodyToString(responseBody io.Reader) (bodyString string, err error) {
|
2022-08-20 14:38:02 +00:00
|
|
|
bodyBytes, err := io.ReadAll(responseBody)
|
2020-04-22 14:52:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
bodyString = string(bodyBytes)
|
|
|
|
fs.Debugf(nil, "jwtutil: Response Body: "+bodyString)
|
|
|
|
return bodyString, nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 12:26:29 +00:00
|
|
|
type response struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
}
|