From 55ed8926520320483b8fc7a9d6bf0f27cb0ae4af Mon Sep 17 00:00:00 2001 From: Lucas Hinderberger Date: Thu, 6 Jun 2024 12:36:32 +0200 Subject: [PATCH] Moving fill-in of OAuth Client field out of template functions --- config.go | 14 ++++++++++++++ pkg/lib/template/template_loader.go | 12 ++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index 3687d636..72fa4b44 100644 --- a/config.go +++ b/config.go @@ -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 } @@ -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 + } + } +} diff --git a/pkg/lib/template/template_loader.go b/pkg/lib/template/template_loader.go index 9fd41a58..8f31b0a1 100644 --- a/pkg/lib/template/template_loader.go +++ b/pkg/lib/template/template_loader.go @@ -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) }, @@ -392,7 +392,7 @@ 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) { @@ -400,7 +400,7 @@ func (loader *Loader) Render( 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) { @@ -408,7 +408,7 @@ func (loader *Loader) Render( 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) { @@ -416,7 +416,7 @@ func (loader *Loader) Render( 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) { @@ -424,7 +424,7 @@ func (loader *Loader) Render( 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 {