@@ -60,6 +60,8 @@ func authEntryPoint(invocationCtx workflow.InvocationContext, _ []workflow.Data)
6060 logger := invocationCtx .GetEnhancedLogger ()
6161 engine := invocationCtx .GetEngine ()
6262
63+ config .ClearCache ()
64+
6365 httpClient := invocationCtx .GetNetworkAccess ().GetUnauthorizedHttpClient ()
6466 authenticator := auth .NewOAuth2AuthenticatorWithOpts (
6567 config ,
@@ -107,10 +109,13 @@ func entryPointDI(invocationCtx workflow.InvocationContext, logger *zerolog.Logg
107109 logger .Printf ("Authentication Type: %s" , authType )
108110 analytics .AddExtensionStringValue (authTypeParameter , authType )
109111
110- if strings .EqualFold (authType , auth .AUTH_TYPE_OAUTH ) { // OAUTH flow
111- logger .Printf ("Unset legacy token key %q from config" , configuration .AUTHENTICATION_TOKEN )
112- config .Unset (configuration .AUTHENTICATION_TOKEN )
112+ existingSnykToken := config .GetString (configuration .AUTHENTICATION_TOKEN )
113+ // always attempt to clear existing tokens before triggering auth
114+ logger .Print ("Unset existing auth keys" )
115+ config .Unset (configuration .AUTHENTICATION_TOKEN )
116+ config .Unset (auth .CONFIG_KEY_OAUTH_TOKEN )
113117
118+ if strings .EqualFold (authType , auth .AUTH_TYPE_OAUTH ) { // OAUTH flow
114119 headless := config .GetBool (headlessFlag )
115120 logger .Printf ("Headless: %v" , headless )
116121
@@ -125,25 +130,19 @@ func entryPointDI(invocationCtx workflow.InvocationContext, logger *zerolog.Logg
125130 }
126131 } else if strings .EqualFold (authType , auth .AUTH_TYPE_PAT ) { // PAT flow
127132 engine .GetConfiguration ().PersistInStorage (auth .CONFIG_KEY_TOKEN )
128-
129- oldToken := config .GetString (configuration .AUTHENTICATION_TOKEN )
130133 pat := config .GetString (ConfigurationNewAuthenticationToken )
131134
132- logger .Print ("Unset existing auth keys from config" )
133- config .Unset (auth .CONFIG_KEY_OAUTH_TOKEN )
134- config .Unset (configuration .AUTHENTICATION_TOKEN )
135-
136135 logger .Print ("Validating pat" )
137136 whoamiConfig := config .Clone ()
138- // we don't want to use the cache here, so this is a workaround
139137 whoamiConfig .ClearCache ()
138+ // we don't want to use the cache here, so this is a workaround
140139 whoamiConfig .Set (configuration .FLAG_EXPERIMENTAL , true )
141140 whoamiConfig .Set (configuration .AUTHENTICATION_TOKEN , pat )
142141 _ , whoamiErr := engine .InvokeWithConfig (workflow .NewWorkflowIdentifier ("whoami" ), whoamiConfig )
143142 if whoamiErr != nil {
144143 // reset config file
145- if len (oldToken ) > 0 {
146- config .Set (auth .CONFIG_KEY_TOKEN , oldToken )
144+ if len (existingSnykToken ) > 0 {
145+ config .Set (auth .CONFIG_KEY_TOKEN , existingSnykToken )
147146 }
148147 return whoamiErr
149148 }
@@ -158,9 +157,6 @@ func entryPointDI(invocationCtx workflow.InvocationContext, logger *zerolog.Logg
158157 logger .Debug ().Err (err ).Msg ("Failed to output authenticated message" )
159158 }
160159 } else { // LEGACY flow
161- logger .Printf ("Unset oauth key %q from config" , auth .CONFIG_KEY_OAUTH_TOKEN )
162- config .Unset (auth .CONFIG_KEY_OAUTH_TOKEN )
163-
164160 config .Set (configuration .RAW_CMD_ARGS , os .Args [1 :])
165161 config .Set (configuration .WORKFLOW_USE_STDIO , true )
166162 config .Set (configuration .AUTHENTICATION_TOKEN , "" ) // clear token to avoid using it during authentication
0 commit comments