Skip to content

Commit

Permalink
Moving fill-in of OAuth Client field out of template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jun 6, 2024
1 parent aa214c1 commit 55ed892
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 14 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func NewTestToolConfig(serverURL string, rootDirectory []string, logNetwork bool
LogShort: logShort,
OAuthClient: Config.Apitest.OAuthClient,
}

config.fillInOAuthClientNames()

err = config.extractTestDirectories()
return config, err
}
Expand Down Expand Up @@ -116,3 +119,14 @@ func (config *TestToolConfig) extractTestDirectories() error {
}
return nil
}

// fillInOAuthClientNames fills in the Client field of loaded OAuthClientConfig
// structs, which the user may have left unset in the config yaml file.
func (config *TestToolConfig) fillInOAuthClientNames() {
for key, clientConfig := range config.OAuthClient {
if clientConfig.Client == "" {
clientConfig.Client = key
config.OAuthClient[key] = clientConfig
}
}
}
12 changes: 6 additions & 6 deletions pkg/lib/template/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (loader *Loader) Render(
if !ok {
return nil, errors.Errorf("OAuth client %q not configured", client)
}
oAuthClient.Client = client

return oAuthClient.GetPasswordCredentialsAuthToken(login, password)

},
Expand All @@ -392,39 +392,39 @@ func (loader *Loader) Render(
if !ok {
return nil, errors.Errorf("OAuth client %q not configured", client)
}
oAuthClient.Client = client

return oAuthClient.GetClientCredentialsAuthToken()
},
"oauth2_code_token": func(client string, params ...string) (tok *oauth2.Token, err error) {
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return nil, errors.Errorf("OAuth client %q not configured", client)
}
oAuthClient.Client = client

return oAuthClient.GetCodeAuthToken(params...)
},
"oauth2_implicit_token": func(client string, params ...string) (tok *oauth2.Token, err error) {
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return nil, errors.Errorf("OAuth client %q not configured", client)
}
oAuthClient.Client = client

return oAuthClient.GetAuthToken(params...)
},
"oauth2_client": func(client string) (c *util.OAuthClientConfig, err error) {
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return nil, errors.Errorf("OAuth client %s not configured", client)
}
oAuthClient.Client = client

return &oAuthClient, nil
},
"oauth2_basic_auth": func(client string) (string, error) {
oAuthClient, ok := loader.OAuthClient[client]
if !ok {
return "", errors.Errorf("OAuth client %s not configured", client)
}
oAuthClient.Client = client

return "Basic " + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", oAuthClient.Client, oAuthClient.Secret))), nil
},
"query_escape": func(in string) string {
Expand Down

0 comments on commit 55ed892

Please sign in to comment.