@@ -26,6 +26,7 @@ import (
2626 "testing"
2727
2828 "github.com/Keyfactor/keyfactor-auth-client-go/auth_providers"
29+ "golang.org/x/oauth2"
2930)
3031
3132func TestOAuthAuthenticator_GetHttpClient (t * testing.T ) {
@@ -346,6 +347,18 @@ func TestCommandConfigOauth_Authenticate(t *testing.T) {
346347 authOauthTest (t , "with invalid creds implicit config file" , true , invCmdHost , invHostExpectedError ... )
347348}
348349
350+ func TestCommandConfigOauth_GetAccessToken (t * testing.T ) {
351+ clientID , clientSecret , tokenURL := exportOAuthEnvVariables ()
352+ t .Log ("Testing auth with w/ full params variables" )
353+ fullParamsConfig := & auth_providers.CommandConfigOauth {
354+ ClientID : clientID ,
355+ ClientSecret : clientSecret ,
356+ TokenURL : tokenURL ,
357+ }
358+ fullParamsConfig .WithSkipVerify (true )
359+ authOauthTest (t , "w/ GetAccessToken w/ full params variables" , false , fullParamsConfig )
360+ }
361+
349362func TestCommandConfigOauth_Build (t * testing.T ) {
350363 // Skip test if TEST_KEYFACTOR_AD_AUTH is set to 1 or true
351364 if os .Getenv ("TEST_KEYFACTOR_AD_AUTH" ) == "1" || os .Getenv ("TEST_KEYFACTOR_AD_AUTH" ) == "true" {
@@ -376,6 +389,33 @@ func authOauthTest(
376389 t .Run (
377390 fmt .Sprintf ("oAuth Auth Test %s" , testName ), func (t * testing.T ) {
378391
392+ // oauth credentials should always generate an access token
393+ oauthToken , tErr := config .GetAccessToken ()
394+ if tErr != nil {
395+ t .Errorf ("oAuth auth test '%s' failed to get token source with %v" , testName , tErr )
396+ t .FailNow ()
397+ return
398+ }
399+ if oauthToken == nil {
400+ t .Errorf ("oAuth auth test '%s' failed to get token source" , testName )
401+ t .FailNow ()
402+ return
403+ }
404+ var at * oauth2.Token
405+ var tkErr error
406+ at , tkErr = oauthToken .Token ()
407+ if tkErr != nil {
408+ t .Errorf ("oAuth auth test '%s' failed to get token source" , testName )
409+ t .FailNow ()
410+ }
411+ if at == nil || at .AccessToken == "" {
412+ t .Errorf ("oAuth auth test '%s' failed to get token source" , testName )
413+ t .FailNow ()
414+ return
415+ }
416+ //t.Logf("token %s", at.AccessToken)
417+ t .Logf ("oAuth auth test '%s' succeeded" , testName )
418+
379419 err := config .Authenticate ()
380420 if allowFail {
381421 if err == nil {
0 commit comments