2015-08-26 20:42:02 -04:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-05-17 11:26:57 -04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-05-13 14:06:46 -04:00
|
|
|
|
2014-05-05 17:54:23 -04:00
|
|
|
// Package oauth2 provides support for making
|
2018-06-02 13:55:33 -04:00
|
|
|
// OAuth2 authorized and authenticated HTTP requests,
|
|
|
|
// as specified in RFC 6749.
|
2014-05-05 17:54:23 -04:00
|
|
|
// It can additionally grant authorization with Bearer JWT.
|
2014-11-26 14:44:45 -05:00
|
|
|
package oauth2 // import "golang.org/x/oauth2"
|
2014-05-05 17:54:23 -04:00
|
|
|
|
|
|
|
import (
|
2014-12-09 18:17:33 -05:00
|
|
|
"bytes"
|
2018-11-01 11:53:36 -04:00
|
|
|
"context"
|
2014-05-05 17:54:23 -04:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2014-12-09 18:17:33 -05:00
|
|
|
"sync"
|
|
|
|
|
2015-01-16 17:43:33 -05:00
|
|
|
"golang.org/x/oauth2/internal"
|
2014-05-05 17:54:23 -04:00
|
|
|
)
|
|
|
|
|
2015-02-26 00:53:51 -05:00
|
|
|
// NoContext is the default context you should supply if not using
|
|
|
|
// your own context.Context (see https://golang.org/x/net/context).
|
2016-08-24 18:40:36 -04:00
|
|
|
//
|
|
|
|
// Deprecated: Use context.Background() or context.TODO() instead.
|
2015-02-26 00:53:51 -05:00
|
|
|
var NoContext = context.TODO()
|
2014-12-11 02:30:13 -05:00
|
|
|
|
2019-01-14 17:52:49 -05:00
|
|
|
// RegisterBrokenAuthHeaderProvider previously did something. It is now a no-op.
|
|
|
|
//
|
|
|
|
// Deprecated: this function no longer does anything. Caller code that
|
|
|
|
// wants to avoid potential extra HTTP requests made during
|
|
|
|
// auto-probing of the provider's auth style should set
|
|
|
|
// Endpoint.AuthStyle.
|
|
|
|
func RegisterBrokenAuthHeaderProvider(tokenURL string) {}
|
2015-11-16 16:49:40 -05:00
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Config describes a typical 3-legged OAuth2 flow, with both the
|
2014-12-30 16:25:01 -05:00
|
|
|
// client application information and the server's endpoint URLs.
|
2016-08-01 16:31:26 -04:00
|
|
|
// For the client credentials 2-legged OAuth2 flow, see the clientcredentials
|
|
|
|
// package (https://golang.org/x/oauth2/clientcredentials).
|
2014-12-09 18:17:33 -05:00
|
|
|
type Config struct {
|
|
|
|
// ClientID is the application's ID.
|
|
|
|
ClientID string
|
|
|
|
|
|
|
|
// ClientSecret is the application's secret.
|
|
|
|
ClientSecret string
|
|
|
|
|
|
|
|
// Endpoint contains the resource server's token endpoint
|
2014-12-30 16:25:01 -05:00
|
|
|
// URLs. These are constants specific to each server and are
|
|
|
|
// often available via site-specific packages, such as
|
|
|
|
// google.Endpoint or github.Endpoint.
|
2014-12-09 18:17:33 -05:00
|
|
|
Endpoint Endpoint
|
|
|
|
|
|
|
|
// RedirectURL is the URL to redirect users going through
|
|
|
|
// the OAuth flow, after the resource owner's URLs.
|
|
|
|
RedirectURL string
|
|
|
|
|
|
|
|
// Scope specifies optional requested permissions.
|
|
|
|
Scopes []string
|
2014-11-12 23:41:14 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// A TokenSource is anything that can return a token.
|
|
|
|
type TokenSource interface {
|
|
|
|
// Token returns a token or an error.
|
2014-12-30 16:25:01 -05:00
|
|
|
// Token must be safe for concurrent use by multiple goroutines.
|
2014-12-30 17:30:46 -05:00
|
|
|
// The returned Token must not be modified.
|
2014-12-09 18:17:33 -05:00
|
|
|
Token() (*Token, error)
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
2014-05-05 17:54:23 -04:00
|
|
|
|
2019-01-14 17:52:49 -05:00
|
|
|
// Endpoint represents an OAuth 2.0 provider's authorization and token
|
2014-12-09 18:17:33 -05:00
|
|
|
// endpoint URLs.
|
|
|
|
type Endpoint struct {
|
|
|
|
AuthURL string
|
|
|
|
TokenURL string
|
2019-01-14 17:52:49 -05:00
|
|
|
|
|
|
|
// AuthStyle optionally specifies how the endpoint wants the
|
|
|
|
// client ID & client secret sent. The zero value means to
|
|
|
|
// auto-detect.
|
|
|
|
AuthStyle AuthStyle
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
2019-01-14 17:52:49 -05:00
|
|
|
// AuthStyle represents how requests for tokens are authenticated
|
|
|
|
// to the server.
|
|
|
|
type AuthStyle int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// AuthStyleAutoDetect means to auto-detect which authentication
|
|
|
|
// style the provider wants by trying both ways and caching
|
|
|
|
// the successful way for the future.
|
|
|
|
AuthStyleAutoDetect AuthStyle = 0
|
|
|
|
|
|
|
|
// AuthStyleInParams sends the "client_id" and "client_secret"
|
|
|
|
// in the POST body as application/x-www-form-urlencoded parameters.
|
|
|
|
AuthStyleInParams AuthStyle = 1
|
|
|
|
|
|
|
|
// AuthStyleInHeader sends the client_id and client_password
|
|
|
|
// using HTTP Basic Authorization. This is an optional style
|
|
|
|
// described in the OAuth2 RFC 6749 section 2.3.1.
|
|
|
|
AuthStyleInHeader AuthStyle = 2
|
|
|
|
)
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
var (
|
|
|
|
// AccessTypeOnline and AccessTypeOffline are options passed
|
|
|
|
// to the Options.AuthCodeURL method. They modify the
|
|
|
|
// "access_type" field that gets sent in the URL returned by
|
|
|
|
// AuthCodeURL.
|
|
|
|
//
|
2015-02-04 22:49:57 -05:00
|
|
|
// Online is the default if neither is specified. If your
|
|
|
|
// application needs to refresh access tokens when the user
|
|
|
|
// is not present at the browser, then use offline. This will
|
|
|
|
// result in your application obtaining a refresh token the
|
|
|
|
// first time your application exchanges an authorization
|
2014-12-09 18:17:33 -05:00
|
|
|
// code for a user.
|
2015-04-06 09:53:19 -04:00
|
|
|
AccessTypeOnline AuthCodeOption = SetAuthURLParam("access_type", "online")
|
|
|
|
AccessTypeOffline AuthCodeOption = SetAuthURLParam("access_type", "offline")
|
2014-12-09 18:17:33 -05:00
|
|
|
|
|
|
|
// ApprovalForce forces the users to view the consent dialog
|
|
|
|
// and confirm the permissions request at the URL returned
|
|
|
|
// from AuthCodeURL, even if they've already done so.
|
2019-04-08 23:31:09 -04:00
|
|
|
ApprovalForce AuthCodeOption = SetAuthURLParam("prompt", "consent")
|
2014-12-09 18:17:33 -05:00
|
|
|
)
|
|
|
|
|
2015-03-26 00:22:14 -04:00
|
|
|
// An AuthCodeOption is passed to Config.AuthCodeURL.
|
|
|
|
type AuthCodeOption interface {
|
|
|
|
setValue(url.Values)
|
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
type setParam struct{ k, v string }
|
|
|
|
|
|
|
|
func (p setParam) setValue(m url.Values) { m.Set(p.k, p.v) }
|
|
|
|
|
2015-04-06 09:53:19 -04:00
|
|
|
// SetAuthURLParam builds an AuthCodeOption which passes key/value parameters
|
2015-03-26 00:22:14 -04:00
|
|
|
// to a provider's authorization endpoint.
|
2015-04-06 09:53:19 -04:00
|
|
|
func SetAuthURLParam(key, value string) AuthCodeOption {
|
2015-03-26 00:22:14 -04:00
|
|
|
return setParam{key, value}
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page
|
|
|
|
// that asks for permissions for the required scopes explicitly.
|
2014-09-05 01:43:23 -04:00
|
|
|
//
|
|
|
|
// State is a token to protect the user from CSRF attacks. You must
|
2018-01-17 18:05:36 -05:00
|
|
|
// always provide a non-empty string and validate that it matches the
|
2023-03-24 14:40:42 -04:00
|
|
|
// state query parameter on your redirect callback.
|
2014-09-05 01:43:23 -04:00
|
|
|
// See http://tools.ietf.org/html/rfc6749#section-10.12 for more info.
|
|
|
|
//
|
2014-12-09 18:17:33 -05:00
|
|
|
// Opts may include AccessTypeOnline or AccessTypeOffline, as well
|
|
|
|
// as ApprovalForce.
|
2019-02-20 10:45:37 -05:00
|
|
|
// It can also be used to pass the PKCE challenge.
|
2018-05-04 16:45:12 -04:00
|
|
|
// See https://www.oauth.com/oauth2-servers/pkce/ for more info.
|
2014-12-09 18:17:33 -05:00
|
|
|
func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString(c.Endpoint.AuthURL)
|
2014-09-04 16:28:18 -04:00
|
|
|
v := url.Values{
|
2014-12-09 18:17:33 -05:00
|
|
|
"response_type": {"code"},
|
|
|
|
"client_id": {c.ClientID},
|
2017-12-21 17:01:18 -05:00
|
|
|
}
|
|
|
|
if c.RedirectURL != "" {
|
|
|
|
v.Set("redirect_uri", c.RedirectURL)
|
|
|
|
}
|
|
|
|
if len(c.Scopes) > 0 {
|
|
|
|
v.Set("scope", strings.Join(c.Scopes, " "))
|
|
|
|
}
|
|
|
|
if state != "" {
|
|
|
|
// TODO(light): Docs say never to omit state; don't allow empty.
|
|
|
|
v.Set("state", state)
|
2014-12-09 18:17:33 -05:00
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.setValue(v)
|
2014-07-29 15:39:33 -04:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
if strings.Contains(c.Endpoint.AuthURL, "?") {
|
|
|
|
buf.WriteByte('&')
|
2014-05-05 17:54:23 -04:00
|
|
|
} else {
|
2014-12-09 18:17:33 -05:00
|
|
|
buf.WriteByte('?')
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
2014-12-09 18:17:33 -05:00
|
|
|
buf.WriteString(v.Encode())
|
|
|
|
return buf.String()
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
2015-02-04 03:22:21 -05:00
|
|
|
// PasswordCredentialsToken converts a resource owner username and password
|
|
|
|
// pair into a token.
|
|
|
|
//
|
|
|
|
// Per the RFC, this grant type should only be used "when there is a high
|
|
|
|
// degree of trust between the resource owner and the client (e.g., the client
|
|
|
|
// is part of the device operating system or a highly privileged application),
|
|
|
|
// and when other authorization grant types are not available."
|
|
|
|
// See https://tools.ietf.org/html/rfc6749#section-4.3 for more info.
|
|
|
|
//
|
2018-09-10 18:10:05 -04:00
|
|
|
// The provided context optionally controls which HTTP client is used. See the HTTPClient variable.
|
2015-02-26 00:53:51 -05:00
|
|
|
func (c *Config) PasswordCredentialsToken(ctx context.Context, username, password string) (*Token, error) {
|
2017-12-21 17:01:18 -05:00
|
|
|
v := url.Values{
|
2015-02-04 03:22:21 -05:00
|
|
|
"grant_type": {"password"},
|
|
|
|
"username": {username},
|
|
|
|
"password": {password},
|
2017-12-21 17:01:18 -05:00
|
|
|
}
|
|
|
|
if len(c.Scopes) > 0 {
|
|
|
|
v.Set("scope", strings.Join(c.Scopes, " "))
|
|
|
|
}
|
|
|
|
return retrieveToken(ctx, c, v)
|
2015-02-04 03:22:21 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Exchange converts an authorization code into a token.
|
|
|
|
//
|
|
|
|
// It is used after a resource provider redirects the user back
|
|
|
|
// to the Redirect URI (the URL obtained from AuthCodeURL).
|
|
|
|
//
|
2018-09-10 18:10:05 -04:00
|
|
|
// The provided context optionally controls which HTTP client is used. See the HTTPClient variable.
|
2014-12-09 18:17:33 -05:00
|
|
|
//
|
|
|
|
// The code will be in the *http.Request.FormValue("code"). Before
|
|
|
|
// calling Exchange, be sure to validate FormValue("state").
|
2018-05-04 16:45:12 -04:00
|
|
|
//
|
|
|
|
// Opts may include the PKCE verifier code if previously used in AuthCodeURL.
|
|
|
|
// See https://www.oauth.com/oauth2-servers/pkce/ for more info.
|
|
|
|
func (c *Config) Exchange(ctx context.Context, code string, opts ...AuthCodeOption) (*Token, error) {
|
2017-12-21 17:01:18 -05:00
|
|
|
v := url.Values{
|
|
|
|
"grant_type": {"authorization_code"},
|
|
|
|
"code": {code},
|
|
|
|
}
|
|
|
|
if c.RedirectURL != "" {
|
|
|
|
v.Set("redirect_uri", c.RedirectURL)
|
|
|
|
}
|
2018-05-04 16:45:12 -04:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt.setValue(v)
|
|
|
|
}
|
2017-12-21 17:01:18 -05:00
|
|
|
return retrieveToken(ctx, c, v)
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Client returns an HTTP client using the provided token.
|
|
|
|
// The token will auto-refresh as necessary. The underlying
|
|
|
|
// HTTP transport will be obtained using the provided context.
|
|
|
|
// The returned client and its Transport should not be modified.
|
2015-02-26 00:53:51 -05:00
|
|
|
func (c *Config) Client(ctx context.Context, t *Token) *http.Client {
|
2014-12-16 18:39:29 -05:00
|
|
|
return NewClient(ctx, c.TokenSource(ctx, t))
|
2014-06-26 20:27:53 -04:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// TokenSource returns a TokenSource that returns t until t expires,
|
|
|
|
// automatically refreshing it as necessary using the provided context.
|
|
|
|
//
|
|
|
|
// Most users will use Config.Client instead.
|
2015-02-26 00:53:51 -05:00
|
|
|
func (c *Config) TokenSource(ctx context.Context, t *Token) TokenSource {
|
2015-02-05 14:58:01 -05:00
|
|
|
tkr := &tokenRefresher{
|
|
|
|
ctx: ctx,
|
|
|
|
conf: c,
|
|
|
|
}
|
|
|
|
if t != nil {
|
|
|
|
tkr.refreshToken = t.RefreshToken
|
|
|
|
}
|
|
|
|
return &reuseTokenSource{
|
|
|
|
t: t,
|
|
|
|
new: tkr,
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// tokenRefresher is a TokenSource that makes "grant_type"=="refresh_token"
|
|
|
|
// HTTP requests to renew a token using a RefreshToken.
|
|
|
|
type tokenRefresher struct {
|
2015-02-26 00:53:51 -05:00
|
|
|
ctx context.Context // used to get HTTP requests
|
2015-02-05 14:58:01 -05:00
|
|
|
conf *Config
|
|
|
|
refreshToken string
|
2014-12-09 18:17:33 -05:00
|
|
|
}
|
2014-11-06 19:36:41 -05:00
|
|
|
|
2015-02-07 16:27:57 -05:00
|
|
|
// WARNING: Token is not safe for concurrent access, as it
|
|
|
|
// updates the tokenRefresher's refreshToken field.
|
|
|
|
// Within this package, it is used by reuseTokenSource which
|
|
|
|
// synchronizes calls to this method with its own mutex.
|
2015-02-05 14:58:01 -05:00
|
|
|
func (tf *tokenRefresher) Token() (*Token, error) {
|
|
|
|
if tf.refreshToken == "" {
|
2014-12-09 18:17:33 -05:00
|
|
|
return nil, errors.New("oauth2: token expired and refresh token is not set")
|
|
|
|
}
|
2015-02-05 14:58:01 -05:00
|
|
|
|
|
|
|
tk, err := retrieveToken(tf.ctx, tf.conf, url.Values{
|
2014-12-09 18:17:33 -05:00
|
|
|
"grant_type": {"refresh_token"},
|
2015-02-05 14:58:01 -05:00
|
|
|
"refresh_token": {tf.refreshToken},
|
2014-12-09 18:17:33 -05:00
|
|
|
})
|
2015-02-05 14:58:01 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-07 16:27:57 -05:00
|
|
|
if tf.refreshToken != tk.RefreshToken {
|
2015-02-05 14:58:01 -05:00
|
|
|
tf.refreshToken = tk.RefreshToken
|
|
|
|
}
|
|
|
|
return tk, err
|
2014-12-09 18:17:33 -05:00
|
|
|
}
|
2014-11-06 19:36:41 -05:00
|
|
|
|
2014-12-30 16:25:01 -05:00
|
|
|
// reuseTokenSource is a TokenSource that holds a single token in memory
|
2014-12-09 18:17:33 -05:00
|
|
|
// and validates its expiry before each call to retrieve it with
|
|
|
|
// Token. If it's expired, it will be auto-refreshed using the
|
|
|
|
// new TokenSource.
|
2014-12-30 16:25:01 -05:00
|
|
|
type reuseTokenSource struct {
|
2014-12-09 18:17:33 -05:00
|
|
|
new TokenSource // called when t is expired.
|
2014-11-12 23:41:14 -05:00
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
mu sync.Mutex // guards t
|
|
|
|
t *Token
|
|
|
|
}
|
2014-11-06 19:36:41 -05:00
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// Token returns the current token if it's still valid, else will
|
|
|
|
// refresh the current token (using r.Context for HTTP client
|
|
|
|
// information) and return the new one.
|
2014-12-30 16:25:01 -05:00
|
|
|
func (s *reuseTokenSource) Token() (*Token, error) {
|
2014-12-09 18:17:33 -05:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2014-12-30 16:25:01 -05:00
|
|
|
if s.t.Valid() {
|
2014-12-09 18:17:33 -05:00
|
|
|
return s.t, nil
|
|
|
|
}
|
|
|
|
t, err := s.new.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.t = t
|
|
|
|
return t, nil
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
2015-05-08 17:14:37 -04:00
|
|
|
// StaticTokenSource returns a TokenSource that always returns the same token.
|
|
|
|
// Because the provided token t is never refreshed, StaticTokenSource is only
|
|
|
|
// useful for tokens that never expire.
|
|
|
|
func StaticTokenSource(t *Token) TokenSource {
|
|
|
|
return staticTokenSource{t}
|
|
|
|
}
|
|
|
|
|
|
|
|
// staticTokenSource is a TokenSource that always returns the same Token.
|
|
|
|
type staticTokenSource struct {
|
|
|
|
t *Token
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s staticTokenSource) Token() (*Token, error) {
|
|
|
|
return s.t, nil
|
|
|
|
}
|
|
|
|
|
2014-12-09 18:17:33 -05:00
|
|
|
// HTTPClient is the context key to use with golang.org/x/net/context's
|
|
|
|
// WithValue function to associate an *http.Client value with a context.
|
2015-01-16 17:43:33 -05:00
|
|
|
var HTTPClient internal.ContextKey
|
2014-12-16 18:39:29 -05:00
|
|
|
|
|
|
|
// NewClient creates an *http.Client from a Context and TokenSource.
|
2014-12-30 16:25:01 -05:00
|
|
|
// The returned client is not valid beyond the lifetime of the context.
|
2014-12-30 18:11:02 -05:00
|
|
|
//
|
2017-02-07 20:56:20 -05:00
|
|
|
// Note that if a custom *http.Client is provided via the Context it
|
|
|
|
// is used only for token acquisition and is not used to configure the
|
|
|
|
// *http.Client returned from NewClient.
|
|
|
|
//
|
2014-12-30 18:11:02 -05:00
|
|
|
// As a special case, if src is nil, a non-OAuth2 client is returned
|
|
|
|
// using the provided context. This exists to support related OAuth2
|
|
|
|
// packages.
|
2015-02-26 00:53:51 -05:00
|
|
|
func NewClient(ctx context.Context, src TokenSource) *http.Client {
|
2014-12-30 18:11:02 -05:00
|
|
|
if src == nil {
|
2018-01-02 19:32:36 -05:00
|
|
|
return internal.ContextClient(ctx)
|
2014-12-30 18:11:02 -05:00
|
|
|
}
|
2014-12-16 18:39:29 -05:00
|
|
|
return &http.Client{
|
|
|
|
Transport: &Transport{
|
2018-01-02 19:32:36 -05:00
|
|
|
Base: internal.ContextClient(ctx).Transport,
|
2014-12-30 16:25:01 -05:00
|
|
|
Source: ReuseTokenSource(nil, src),
|
2014-12-16 18:39:29 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 16:25:01 -05:00
|
|
|
|
|
|
|
// ReuseTokenSource returns a TokenSource which repeatedly returns the
|
|
|
|
// same token as long as it's valid, starting with t.
|
|
|
|
// When its cached token is invalid, a new token is obtained from src.
|
|
|
|
//
|
|
|
|
// ReuseTokenSource is typically used to reuse tokens from a cache
|
|
|
|
// (such as a file on disk) between runs of a program, rather than
|
|
|
|
// obtaining new tokens unnecessarily.
|
|
|
|
//
|
|
|
|
// The initial token t may be nil, in which case the TokenSource is
|
|
|
|
// wrapped in a caching version if it isn't one already. This also
|
|
|
|
// means it's always safe to wrap ReuseTokenSource around any other
|
|
|
|
// TokenSource without adverse effects.
|
|
|
|
func ReuseTokenSource(t *Token, src TokenSource) TokenSource {
|
|
|
|
// Don't wrap a reuseTokenSource in itself. That would work,
|
|
|
|
// but cause an unnecessary number of mutex operations.
|
|
|
|
// Just build the equivalent one.
|
|
|
|
if rt, ok := src.(*reuseTokenSource); ok {
|
|
|
|
if t == nil {
|
|
|
|
// Just use it directly.
|
|
|
|
return rt
|
|
|
|
}
|
|
|
|
src = rt.new
|
|
|
|
}
|
|
|
|
return &reuseTokenSource{
|
|
|
|
t: t,
|
|
|
|
new: src,
|
|
|
|
}
|
|
|
|
}
|