Skip to content

Commit

Permalink
Implement wrapper for clientcredentials.Config
Browse files Browse the repository at this point in the history
Implement a wrapper for clientcredentials.Config in preparation for
custom behavior and logic when issuing new tokens.
  • Loading branch information
elikatsis committed Sep 6, 2023
1 parent f85204a commit 147ea12
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions extension/oauth2clientauthextension/clientcredentialsconfig.go
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()
}

0 comments on commit 147ea12

Please sign in to comment.