2024-01-01 16:52:18 +00:00
|
|
|
// Package twitch implements a client for the Twitch APIs
|
2021-08-19 13:33:56 +00:00
|
|
|
package twitch
|
2020-12-24 12:18:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-31 12:42:37 +00:00
|
|
|
"crypto/sha256"
|
2020-12-24 12:18:30 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2021-09-10 17:09:10 +00:00
|
|
|
"net/url"
|
2021-12-31 12:42:37 +00:00
|
|
|
"strings"
|
2021-01-20 23:35:42 +00:00
|
|
|
"time"
|
2020-12-24 12:18:30 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2024-01-01 16:52:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/go_helpers/v2/backoff"
|
2020-12-24 12:18:30 +00:00
|
|
|
)
|
|
|
|
|
2021-06-02 11:21:13 +00:00
|
|
|
const (
|
|
|
|
timeDay = 24 * time.Hour
|
|
|
|
|
2022-03-20 13:32:11 +00:00
|
|
|
tokenValidityRecheckInterval = time.Hour
|
|
|
|
|
2021-09-02 21:28:15 +00:00
|
|
|
twitchMinCacheTime = time.Second * 30
|
|
|
|
|
2021-06-02 11:21:13 +00:00
|
|
|
twitchRequestRetries = 5
|
|
|
|
twitchRequestTimeout = 2 * time.Second
|
|
|
|
)
|
2021-04-03 12:11:47 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Definitions of possible / understood auth types
|
2021-12-24 18:59:20 +00:00
|
|
|
const (
|
2023-07-01 14:48:21 +00:00
|
|
|
AuthTypeUnauthorized AuthType = iota
|
|
|
|
AuthTypeAppAccessToken
|
|
|
|
AuthTypeBearerToken
|
2021-12-24 18:59:20 +00:00
|
|
|
)
|
|
|
|
|
2021-09-10 17:09:10 +00:00
|
|
|
type (
|
2024-01-01 16:52:18 +00:00
|
|
|
// Client bundles the API access methods into a Client
|
2021-09-10 17:09:10 +00:00
|
|
|
Client struct {
|
2021-12-24 18:59:20 +00:00
|
|
|
clientID string
|
|
|
|
clientSecret string
|
2021-12-31 12:42:37 +00:00
|
|
|
|
2022-03-20 13:32:11 +00:00
|
|
|
accessToken string
|
|
|
|
refreshToken string
|
|
|
|
tokenValidity time.Time
|
|
|
|
tokenValidityChecked time.Time
|
|
|
|
tokenUpdateHook func(string, string) error
|
2021-12-24 18:59:20 +00:00
|
|
|
|
|
|
|
appAccessToken string
|
2021-09-10 17:09:10 +00:00
|
|
|
|
|
|
|
apiCache *APICache
|
|
|
|
}
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ErrorResponse is a response sent by Twitch API in case there is
|
|
|
|
// an error
|
2023-07-21 18:37:20 +00:00
|
|
|
ErrorResponse struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// OAuthTokenResponse is used when requesting a token
|
2021-12-31 12:42:37 +00:00
|
|
|
OAuthTokenResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
Scope []string `json:"scope"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// OAuthTokenValidationResponse is used when validating a token
|
2021-12-31 12:42:37 +00:00
|
|
|
OAuthTokenValidationResponse struct {
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
Login string `json:"login"`
|
|
|
|
Scopes []string `json:"scopes"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// AuthType is a collection of available authorization types in the
|
|
|
|
// Twitch API
|
2023-07-01 14:48:21 +00:00
|
|
|
AuthType uint8
|
2021-12-24 18:59:20 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ClientRequestOpts contains the options to create a request to the
|
|
|
|
// Twitch APIs
|
2023-07-01 14:48:21 +00:00
|
|
|
ClientRequestOpts struct {
|
|
|
|
AuthType AuthType
|
2021-12-31 12:42:37 +00:00
|
|
|
Body io.Reader
|
|
|
|
Method string
|
|
|
|
NoRetry bool
|
|
|
|
NoValidateToken bool
|
|
|
|
OKStatus int
|
|
|
|
Out interface{}
|
|
|
|
URL string
|
2023-07-21 18:37:20 +00:00
|
|
|
ValidateFunc func(ClientRequestOpts, *http.Response) error
|
2021-12-24 18:59:20 +00:00
|
|
|
}
|
2021-09-10 17:09:10 +00:00
|
|
|
)
|
2021-03-27 17:55:38 +00:00
|
|
|
|
2023-07-21 18:37:20 +00:00
|
|
|
// ValidateStatus is the default validation function used when no
|
|
|
|
// ValidateFunc is given in the ClientRequestOpts and checks for the
|
2023-11-27 22:18:53 +00:00
|
|
|
// returned HTTP status is equal to the OKStatus.
|
|
|
|
//
|
|
|
|
// When the status is http.StatusTooManyRequests the function will
|
|
|
|
// return an error terminating any retries as retrying would not make
|
|
|
|
// sense (the error returned from Request will still be an HTTPError
|
|
|
|
// with status 429).
|
2023-07-21 18:37:20 +00:00
|
|
|
//
|
|
|
|
// When wrapping this function the body should not have been read
|
|
|
|
// before in order to have the response body available in the returned
|
|
|
|
// HTTPError
|
|
|
|
func ValidateStatus(opts ClientRequestOpts, resp *http.Response) error {
|
|
|
|
if opts.OKStatus != 0 && resp.StatusCode != opts.OKStatus {
|
2023-12-13 22:14:23 +00:00
|
|
|
// We shall not accept this!
|
|
|
|
var ret error
|
|
|
|
|
2023-07-21 18:37:20 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2023-12-13 22:14:23 +00:00
|
|
|
ret = newHTTPError(resp.StatusCode, nil, err)
|
|
|
|
} else {
|
|
|
|
ret = newHTTPError(resp.StatusCode, body, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusTooManyRequests {
|
|
|
|
// Twitch doesn't want to hear any more of this
|
2024-01-01 16:52:18 +00:00
|
|
|
return backoff.NewErrCannotRetry(ret) //nolint:wrapcheck // We'll get our internal error
|
2023-07-21 18:37:20 +00:00
|
|
|
}
|
2023-12-13 22:14:23 +00:00
|
|
|
return ret
|
2023-07-21 18:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// New creates a new Client with the given credentials
|
2021-12-31 12:42:37 +00:00
|
|
|
func New(clientID, clientSecret, accessToken, refreshToken string) *Client {
|
2021-08-19 13:33:56 +00:00
|
|
|
return &Client{
|
2021-12-24 18:59:20 +00:00
|
|
|
clientID: clientID,
|
|
|
|
clientSecret: clientSecret,
|
2021-12-31 12:42:37 +00:00
|
|
|
|
|
|
|
accessToken: accessToken,
|
|
|
|
refreshToken: refreshToken,
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-04-01 10:28:51 +00:00
|
|
|
apiCache: newTwitchAPICache(),
|
2021-03-27 17:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-24 12:18:30 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// APICache returns the internal APICache used by the Client
|
2021-12-31 12:42:37 +00:00
|
|
|
func (c *Client) APICache() *APICache { return c.apiCache }
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// GetToken returns the access-token for the configured credentials
|
|
|
|
// after validating and - if required - renewing the token
|
|
|
|
func (c *Client) GetToken(ctx context.Context) (string, error) {
|
|
|
|
if err := c.ValidateToken(ctx, false); err != nil {
|
|
|
|
if err = c.RefreshToken(ctx); err != nil {
|
2021-12-31 12:42:37 +00:00
|
|
|
return "", errors.Wrap(err, "refreshing token after validation error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token was refreshed, therefore should now be valid
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.accessToken, nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// RefreshToken takes the configured refresh-token and renews the
|
|
|
|
// corresponding access-token
|
|
|
|
func (c *Client) RefreshToken(ctx context.Context) error {
|
2021-12-31 12:42:37 +00:00
|
|
|
if c.refreshToken == "" {
|
|
|
|
return errors.New("no refresh token set")
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(url.Values)
|
|
|
|
params.Set("client_id", c.clientID)
|
|
|
|
params.Set("client_secret", c.clientSecret)
|
|
|
|
params.Set("refresh_token", c.refreshToken)
|
|
|
|
params.Set("grant_type", "refresh_token")
|
|
|
|
|
|
|
|
var resp OAuthTokenResponse
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
err := c.Request(ctx, ClientRequestOpts{
|
2023-07-01 14:48:21 +00:00
|
|
|
AuthType: AuthTypeUnauthorized,
|
2021-12-31 12:42:37 +00:00
|
|
|
Method: http.MethodPost,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &resp,
|
|
|
|
URL: fmt.Sprintf("https://id.twitch.tv/oauth2/token?%s", params.Encode()),
|
2022-03-18 21:09:56 +00:00
|
|
|
})
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
// That's fine, just continue
|
|
|
|
|
2023-07-21 18:37:20 +00:00
|
|
|
case errors.Is(err, ErrAnyHTTPError):
|
2021-12-31 12:42:37 +00:00
|
|
|
// Retried refresh failed, wipe tokens
|
2024-01-01 16:52:18 +00:00
|
|
|
logrus.WithError(err).Warning("resetting tokens after refresh-failure")
|
2021-12-31 12:42:37 +00:00
|
|
|
c.UpdateToken("", "")
|
|
|
|
if c.tokenUpdateHook != nil {
|
|
|
|
if herr := c.tokenUpdateHook("", ""); herr != nil {
|
2024-01-01 16:52:18 +00:00
|
|
|
logrus.WithError(herr).Error("Unable to store token wipe after refresh failure")
|
2021-12-31 12:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-18 21:09:56 +00:00
|
|
|
return errors.Wrap(err, "executing request")
|
2021-12-31 12:42:37 +00:00
|
|
|
|
2022-03-18 21:09:56 +00:00
|
|
|
default:
|
2021-12-31 12:42:37 +00:00
|
|
|
return errors.Wrap(err, "executing request")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UpdateToken(resp.AccessToken, resp.RefreshToken)
|
|
|
|
c.tokenValidity = time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second)
|
2024-01-01 16:52:18 +00:00
|
|
|
logrus.WithField("expiry", c.tokenValidity).Trace("Access token refreshed")
|
2021-12-31 12:42:37 +00:00
|
|
|
|
|
|
|
if c.tokenUpdateHook == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(c.tokenUpdateHook(resp.AccessToken, resp.RefreshToken), "calling token update hook")
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// SetTokenUpdateHook registers a function to listen for token changes
|
|
|
|
// after renewing the internal token. It is presented with an access-
|
|
|
|
// and a refresh-token if those changes.
|
2021-12-31 12:42:37 +00:00
|
|
|
func (c *Client) SetTokenUpdateHook(f func(string, string) error) {
|
|
|
|
c.tokenUpdateHook = f
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// UpdateToken overwrites the configured access- and refresh-tokens
|
2021-12-31 12:42:37 +00:00
|
|
|
func (c *Client) UpdateToken(accessToken, refreshToken string) {
|
|
|
|
c.accessToken = accessToken
|
|
|
|
c.refreshToken = refreshToken
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// ValidateToken executes a request against the Twitch API to validate
|
|
|
|
// the token is still valid. If the expiry is known and the force
|
|
|
|
// parameter is not supplied, the request is omitted.
|
|
|
|
//
|
|
|
|
//revive:disable-next-line:flag-parameter
|
2021-12-31 12:42:37 +00:00
|
|
|
func (c *Client) ValidateToken(ctx context.Context, force bool) error {
|
2022-03-20 13:32:11 +00:00
|
|
|
if c.tokenValidity.After(time.Now()) && time.Since(c.tokenValidityChecked) < tokenValidityRecheckInterval && !force {
|
2021-12-31 12:42:37 +00:00
|
|
|
// We do have an expiration time and it's not expired
|
|
|
|
// so we can assume we've checked the token and it should
|
|
|
|
// still be valid.
|
2022-03-20 13:32:11 +00:00
|
|
|
// To detect a token revokation early-ish we re-check the
|
|
|
|
// token in defined interval. This is not the optimal
|
|
|
|
// solution as we will get failing requests between revokation
|
|
|
|
// and recheck but it's better than nothing.
|
2021-12-31 12:42:37 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.accessToken == "" {
|
|
|
|
return errors.New("no access token present")
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp OAuthTokenValidationResponse
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err := c.Request(ctx, ClientRequestOpts{
|
2023-07-01 14:48:21 +00:00
|
|
|
AuthType: AuthTypeBearerToken,
|
2021-12-31 12:42:37 +00:00
|
|
|
Method: http.MethodGet,
|
|
|
|
NoRetry: true,
|
|
|
|
NoValidateToken: true,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &resp,
|
|
|
|
URL: "https://id.twitch.tv/oauth2/validate",
|
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrap(err, "executing request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ClientID != c.clientID {
|
|
|
|
return errors.New("token belongs to different app")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.tokenValidity = time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second)
|
2022-03-20 13:32:11 +00:00
|
|
|
c.tokenValidityChecked = time.Now()
|
2024-01-01 16:52:18 +00:00
|
|
|
logrus.WithField("expiry", c.tokenValidity).Trace("Access token validated")
|
2021-12-31 12:42:37 +00:00
|
|
|
|
|
|
|
return nil
|
2021-12-24 23:47:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// GetTwitchAppAccessToken uses client-id and -secret to generate a
|
|
|
|
// new app-access-token in case none is present or returns the cached
|
|
|
|
// token.
|
|
|
|
func (c *Client) GetTwitchAppAccessToken(ctx context.Context) (string, error) {
|
2021-12-24 18:59:20 +00:00
|
|
|
if c.appAccessToken != "" {
|
|
|
|
return c.appAccessToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var rData struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
Scope []interface{} `json:"scope"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(url.Values)
|
|
|
|
params.Set("client_id", c.clientID)
|
|
|
|
params.Set("client_secret", c.clientSecret)
|
|
|
|
params.Set("grant_type", "client_credentials")
|
|
|
|
|
|
|
|
u, _ := url.Parse("https://id.twitch.tv/oauth2/token")
|
|
|
|
u.RawQuery = params.Encode()
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
reqCtx, cancel := context.WithTimeout(ctx, twitchRequestTimeout)
|
2021-12-24 18:59:20 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err := c.Request(reqCtx, ClientRequestOpts{
|
2023-07-01 14:48:21 +00:00
|
|
|
AuthType: AuthTypeUnauthorized,
|
2021-12-24 18:59:20 +00:00
|
|
|
Method: http.MethodPost,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &rData,
|
|
|
|
URL: u.String(),
|
|
|
|
}); err != nil {
|
|
|
|
return "", errors.Wrap(err, "fetching token response")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.appAccessToken = rData.AccessToken
|
|
|
|
return rData.AccessToken, nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Request executes the request towards the Twitch API defined by the
|
|
|
|
// ClientRequestOpts and takes care of token management and response
|
|
|
|
// checking
|
|
|
|
//
|
2023-07-21 18:37:20 +00:00
|
|
|
//nolint:gocyclo // Not gonna split to keep as a logical unit
|
2024-01-01 16:52:18 +00:00
|
|
|
func (c *Client) Request(ctx context.Context, opts ClientRequestOpts) error {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
2021-12-24 18:59:20 +00:00
|
|
|
"method": opts.Method,
|
2021-12-31 12:42:37 +00:00
|
|
|
"url": c.replaceSecrets(opts.URL),
|
2021-03-27 17:55:38 +00:00
|
|
|
}).Trace("Execute Twitch API request")
|
|
|
|
|
2021-12-24 21:04:48 +00:00
|
|
|
var retries uint64 = twitchRequestRetries
|
2021-12-31 12:42:37 +00:00
|
|
|
if opts.Body != nil || opts.NoRetry {
|
2021-12-24 21:14:24 +00:00
|
|
|
// Body must be read only once, do not retry
|
2021-12-24 21:04:48 +00:00
|
|
|
retries = 1
|
|
|
|
}
|
|
|
|
|
2023-07-21 18:37:20 +00:00
|
|
|
if opts.ValidateFunc == nil {
|
|
|
|
opts.ValidateFunc = ValidateStatus
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
//nolint:wrapcheck // The backoff library returns our own errors
|
2021-12-24 21:04:48 +00:00
|
|
|
return backoff.NewBackoff().WithMaxIterations(retries).Retry(func() error {
|
2024-01-01 16:52:18 +00:00
|
|
|
reqCtx, cancel := context.WithTimeout(ctx, twitchRequestTimeout)
|
2021-06-02 11:21:13 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
req, err := http.NewRequestWithContext(reqCtx, opts.Method, opts.URL, opts.Body)
|
2021-06-02 11:21:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "assemble request")
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2021-12-24 18:59:20 +00:00
|
|
|
|
|
|
|
switch opts.AuthType {
|
2023-07-01 14:48:21 +00:00
|
|
|
case AuthTypeUnauthorized:
|
2021-12-24 18:59:20 +00:00
|
|
|
// Nothing to do
|
|
|
|
|
2023-07-01 14:48:21 +00:00
|
|
|
case AuthTypeAppAccessToken:
|
2024-01-01 16:52:18 +00:00
|
|
|
accessToken, err := c.GetTwitchAppAccessToken(ctx)
|
2021-12-24 18:59:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting app-access-token")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
|
|
req.Header.Set("Client-Id", c.clientID)
|
|
|
|
|
2023-07-01 14:48:21 +00:00
|
|
|
case AuthTypeBearerToken:
|
2021-12-31 12:42:37 +00:00
|
|
|
accessToken := c.accessToken
|
|
|
|
if !opts.NoValidateToken {
|
2024-01-01 16:52:18 +00:00
|
|
|
accessToken, err = c.GetToken(reqCtx)
|
2021-12-31 12:42:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting bearer access token")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
2021-12-24 18:59:20 +00:00
|
|
|
req.Header.Set("Client-Id", c.clientID)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.New("invalid auth type specified")
|
|
|
|
}
|
2021-06-02 11:21:13 +00:00
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "execute request")
|
|
|
|
}
|
2024-01-01 16:52:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing response body (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2021-06-02 11:21:13 +00:00
|
|
|
|
2023-07-01 14:48:21 +00:00
|
|
|
if opts.AuthType == AuthTypeAppAccessToken && resp.StatusCode == http.StatusUnauthorized {
|
2022-02-16 23:53:43 +00:00
|
|
|
// Seems our token was somehow revoked, clear the token and retry which will get a new token
|
|
|
|
c.appAccessToken = ""
|
|
|
|
return errors.New("app-access-token is invalid")
|
|
|
|
}
|
|
|
|
|
2023-07-21 18:37:20 +00:00
|
|
|
if err = opts.ValidateFunc(opts, resp); err != nil {
|
|
|
|
return err
|
2021-12-24 18:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Out == nil {
|
2021-09-10 17:09:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:21:13 +00:00
|
|
|
return errors.Wrap(
|
2021-12-24 18:59:20 +00:00
|
|
|
json.NewDecoder(resp.Body).Decode(opts.Out),
|
2021-06-02 11:21:13 +00:00
|
|
|
"parse user info",
|
|
|
|
)
|
|
|
|
})
|
2020-12-24 12:18:30 +00:00
|
|
|
}
|
2021-12-31 12:42:37 +00:00
|
|
|
|
|
|
|
func (c *Client) replaceSecrets(u string) string {
|
|
|
|
var replacements []string
|
|
|
|
|
|
|
|
for _, secret := range []string{
|
|
|
|
c.accessToken,
|
|
|
|
c.refreshToken,
|
|
|
|
c.clientSecret,
|
|
|
|
} {
|
|
|
|
if secret == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
replacements = append(replacements, secret, c.hashSecret(secret))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.NewReplacer(replacements...).Replace(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Client) hashSecret(secret string) string {
|
2024-01-01 16:52:18 +00:00
|
|
|
return fmt.Sprintf("[sha256:%x]", sha256.Sum256([]byte(secret)))
|
2021-12-31 12:42:37 +00:00
|
|
|
}
|