-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement wrapper for clientcredentials.Config
Implement a wrapper for clientcredentials.Config in preparation for custom behavior and logic when issuing new tokens.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
extension/oauth2clientauthextension/clientcredentialsconfig.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package oauth2clientauthextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/oauth2clientauthextension" | ||
|
||
import ( | ||
"context" | ||
|
||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/clientcredentials" | ||
) | ||
|
||
// clientCredentialsConfig is a clientcredentials.Config wrapper. | ||
type clientCredentialsConfig struct { | ||
clientcredentials.Config | ||
} | ||
|
||
type clientCredentialsTokenSource struct { | ||
ctx context.Context | ||
config *clientCredentialsConfig | ||
} | ||
|
||
// clientCredentialsTokenSource implements TokenSource | ||
var _ oauth2.TokenSource = (*clientCredentialsTokenSource)(nil) | ||
|
||
// createConfig creates a proper clientcredentials.Config with values retrieved | ||
// from files, if the user has specified a "file:" prefix | ||
func (c *clientCredentialsConfig) createConfig() (*clientcredentials.Config, error) { | ||
return &clientcredentials.Config{ | ||
ClientID: c.ClientID, | ||
ClientSecret: c.ClientSecret, | ||
TokenURL: c.TokenURL, | ||
Scopes: c.Scopes, | ||
EndpointParams: c.EndpointParams, | ||
}, nil | ||
} | ||
|
||
func (c *clientCredentialsConfig) TokenSource(ctx context.Context) oauth2.TokenSource { | ||
return oauth2.ReuseTokenSource(nil, clientCredentialsTokenSource{ctx: ctx, config: c}) | ||
} | ||
|
||
func (ts clientCredentialsTokenSource) Token() (*oauth2.Token, error) { | ||
cfg, err := ts.config.createConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cfg.TokenSource(ts.ctx).Token() | ||
} |