Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oauth2: add an optional interface to add Context support to TokenSource #393

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions clientcredentials/clientcredentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,25 @@ func (c *Config) Client(ctx context.Context) *http.Client {
// Most users will use Config.Client instead.
func (c *Config) TokenSource(ctx context.Context) oauth2.TokenSource {
source := &tokenSource{
ctx: ctx,
conf: c,
httpclient: internal.ContextClient(ctx),
conf: c,
}
return oauth2.ReuseTokenSource(nil, source)
}

type tokenSource struct {
ctx context.Context
conf *Config
httpclient *http.Client
conf *Config
}

// Token refreshes the token by using a new client credentials request.
// tokens received this way do not include a refresh token
func (c *tokenSource) Token() (*oauth2.Token, error) {
return c.TokenContext(context.Background())
}

// TokenContext implements oauth2.tokenSourceContext.
func (c *tokenSource) TokenContext(ctx context.Context) (*oauth2.Token, error) {
v := url.Values{
"grant_type": {"client_credentials"},
}
Expand All @@ -103,7 +108,8 @@ func (c *tokenSource) Token() (*oauth2.Token, error) {
v[k] = p
}

tk, err := internal.RetrieveToken(c.ctx, c.conf.ClientID, c.conf.ClientSecret, c.conf.TokenURL, v, internal.AuthStyle(c.conf.AuthStyle))
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.httpclient)
tk, err := internal.RetrieveToken(ctx, c.conf.ClientID, c.conf.ClientSecret, c.conf.TokenURL, v, internal.AuthStyle(c.conf.AuthStyle))
if err != nil {
if rErr, ok := err.(*internal.RetrieveError); ok {
return nil, (*oauth2.RetrieveError)(rErr)
Expand Down
22 changes: 21 additions & 1 deletion oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type TokenSource interface {
Token() (*Token, error)
}

type tokenSourceContext interface {
TokenContext(context.Context) (*Token, error)
}

// Endpoint represents an OAuth 2.0 provider's authorization and token
// endpoint URLs.
type Endpoint struct {
Expand Down Expand Up @@ -296,15 +300,31 @@ type reuseTokenSource struct {
// refresh the current token (using r.Context for HTTP client
// information) and return the new one.
func (s *reuseTokenSource) Token() (*Token, error) {
return s.TokenContext(context.Background())
}

// TokenContext implements tokenSourceContext.
func (s *reuseTokenSource) TokenContext(ctx context.Context) (*Token, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.t.Valid() {
return s.t, nil
}
t, err := s.new.Token()

var (
t *Token
err error
)
tsctx, ok := s.new.(tokenSourceContext)
if ok {
t, err = tsctx.TokenContext(ctx)
} else {
t, err = s.new.Token()
}
if err != nil {
return nil, err
}

s.t = t
return t, nil
}
Expand Down
12 changes: 11 additions & 1 deletion transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.Source == nil {
return nil, errors.New("oauth2: Transport's Source is nil")
}
token, err := t.Source.Token()

var (
token *Token
err error
)
tsctx, ok := t.Source.(tokenSourceContext)
if ok {
token, err = tsctx.TokenContext(req.Context())
} else {
token, err = t.Source.Token()
}
if err != nil {
return nil, err
}
Expand Down