Skip to content

Commit

Permalink
Add autorest.BearerAuthorizerCallback support to auth.CachedAuthorizer
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Nov 29, 2021
1 parent 5f7dc4a commit 89407ca
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions auth/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (c *CachedAuthorizer) AuxiliaryTokens() ([]*oauth2.Token, error) {
return c.auxTokens, nil
}

// WithAuthorization implements the autorest.Authorizer interface
func (c *CachedAuthorizer) WithAuthorization() autorest.PrepareDecorator {
return func(p autorest.Preparer) autorest.Preparer {
return autorest.PreparerFunc(func(req *http.Request) (*http.Request, error) {
Expand Down Expand Up @@ -100,6 +101,21 @@ func (c *CachedAuthorizer) WithAuthorization() autorest.PrepareDecorator {
}
}

// BearerAuthorizerCallback is a helper that returns an *autorest.BearerAuthorizerCallback for use in data plane API clients in the Azure SDK
func (c *CachedAuthorizer) BearerAuthorizerCallback() *autorest.BearerAuthorizerCallback {
return autorest.NewBearerAuthorizerCallback(nil, func(_, resource string) (*autorest.BearerAuthorizer, error) {
token, err := c.Token()
if err != nil {
return nil, fmt.Errorf("obtaining token: %v", err)
}

return autorest.NewBearerAuthorizer(&servicePrincipalTokenWrapper{
tokenType: "Bearer",
tokenValue: token.AccessToken,
}), nil
})
}

// NewCachedAuthorizer returns an Authorizer that caches an access token for the duration of its validity.
// If the cached token expires, a new one is acquired and cached.
func NewCachedAuthorizer(src Authorizer) Authorizer {
Expand Down

0 comments on commit 89407ca

Please sign in to comment.