Skip to content

Commit

Permalink
Enable specifying files to get client ID/secret from
Browse files Browse the repository at this point in the history
Leverage the clientCredentialsConfig features to read client ID and/or
secret from a file to the actual extension configuration.
  • Loading branch information
elikatsis committed Sep 6, 2023
1 parent a2d4401 commit 1be111a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 4 additions & 0 deletions extension/oauth2clientauthextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ Following are the configuration fields
- [**token_url**](https://datatracker.ietf.org/doc/html/rfc6749#section-3.2) - The resource server's token endpoint URLs.
- [**client_id**](https://datatracker.ietf.org/doc/html/rfc6749#section-2.2) - The client identifier issued to the client.
- **client_id_file** - The file path to retrieve the client identifier issued to the client.
This setting takes precedence over `client_id`.
- [**client_secret**](https://datatracker.ietf.org/doc/html/rfc6749#section-2.3.1) - The secret string associated with above identifier.
- **client_secret_file** - The file path to retrieve the secret string associated with above identifier.
This setting takes precedence over `client_secret`.
- [**endpoint_params**](https://github.com/golang/oauth2/blob/master/clientcredentials/clientcredentials.go#L44) - Additional parameters that are sent to the token endpoint.
- [**scopes**](https://datatracker.ietf.org/doc/html/rfc6749#section-3.3) - **Optional** optional requested permissions associated for the client.
- [**timeout**](https://golang.org/src/net/http/client.go#L90) - **Optional** specifies the timeout on the underlying client to authorization server for fetching the tokens (initial and while refreshing).
Expand Down
10 changes: 8 additions & 2 deletions extension/oauth2clientauthextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ type Config struct {
// See https://datatracker.ietf.org/doc/html/rfc6749#section-2.2
ClientID string `mapstructure:"client_id"`

// ClientIDFile is the file path to read the application's ID from.
ClientIDFile string `mapstructure:"client_id_file"`

// ClientSecret is the application's secret.
// See https://datatracker.ietf.org/doc/html/rfc6749#section-2.3.1
ClientSecret configopaque.String `mapstructure:"client_secret"`

// ClientSecretFile is the file pathg to read the application's secret from.
ClientSecretFile string `mapstructure:"client_secret_file"`

// EndpointParams specifies additional parameters for requests to the token endpoint.
EndpointParams url.Values `mapstructure:"endpoint_params"`

Expand All @@ -54,10 +60,10 @@ var _ component.Config = (*Config)(nil)

// Validate checks if the extension configuration is valid
func (cfg *Config) Validate() error {
if cfg.ClientID == "" {
if cfg.ClientID == "" && cfg.ClientIDFile == "" {
return errNoClientIDProvided
}
if cfg.ClientSecret == "" {
if cfg.ClientSecret == "" && cfg.ClientSecretFile == "" {
return errNoClientSecretProvided
}
if cfg.TokenURL == "" {
Expand Down
6 changes: 4 additions & 2 deletions extension/oauth2clientauthextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ var _ oauth2.TokenSource = (*errorWrappingTokenSource)(nil)
var errFailedToGetSecurityToken = fmt.Errorf("failed to get security token from token endpoint")

func newClientAuthenticator(cfg *Config, logger *zap.Logger) (*clientAuthenticator, error) {
if cfg.ClientID == "" {
if cfg.ClientID == "" && cfg.ClientIDFile == "" {
return nil, errNoClientIDProvided
}
if cfg.ClientSecret == "" {
if cfg.ClientSecret == "" && cfg.ClientSecretFile == "" {
return nil, errNoClientSecretProvided
}
if cfg.TokenURL == "" {
Expand All @@ -63,6 +63,8 @@ func newClientAuthenticator(cfg *Config, logger *zap.Logger) (*clientAuthenticat
Scopes: cfg.Scopes,
EndpointParams: cfg.EndpointParams,
},
ClientIDFile: cfg.ClientIDFile,
ClientSecretFile: cfg.ClientSecretFile,
},
logger: logger,
client: &http.Client{
Expand Down

0 comments on commit 1be111a

Please sign in to comment.