|
7 | 7 | import static net.snowflake.client.core.SFTrustManager.resetOCSPResponseCacherServerURL;
|
8 | 8 | import static net.snowflake.client.jdbc.SnowflakeUtil.systemGetProperty;
|
9 | 9 |
|
| 10 | +import com.amazonaws.util.StringUtils; |
10 | 11 | import com.fasterxml.jackson.databind.JsonNode;
|
11 | 12 | import com.fasterxml.jackson.databind.ObjectMapper;
|
12 | 13 | import com.google.common.base.Strings;
|
|
31 | 32 | import net.snowflake.client.core.auth.oauth.AccessTokenProvider;
|
32 | 33 | import net.snowflake.client.core.auth.oauth.OAuthAccessTokenForRefreshTokenProvider;
|
33 | 34 | import net.snowflake.client.core.auth.oauth.OAuthAccessTokenProviderFactory;
|
34 |
| -import net.snowflake.client.core.auth.oauth.OAuthUtil; |
35 | 35 | import net.snowflake.client.core.auth.oauth.TokenResponseDTO;
|
36 | 36 | import net.snowflake.client.jdbc.ErrorCode;
|
37 | 37 | import net.snowflake.client.jdbc.SnowflakeDriver;
|
@@ -286,20 +286,16 @@ static SFLoginOutput openSession(
|
286 | 286 | final AuthenticatorType authenticator = getAuthenticator(loginInput);
|
287 | 287 | checkIfExperimentalAuthnEnabled(authenticator);
|
288 | 288 |
|
289 |
| - if (authenticator.equals(AuthenticatorType.OAUTH) |
290 |
| - || authenticator.equals(AuthenticatorType.PROGRAMMATIC_ACCESS_TOKEN)) { |
291 |
| - // OAUTH and PAT needs either token or password |
| 289 | + if (isTokenOrPasswordRequired(authenticator)) { |
292 | 290 | AssertUtil.assertTrue(
|
293 | 291 | loginInput.getToken() != null || loginInput.getPassword() != null,
|
294 | 292 | "missing token or password for opening session");
|
295 |
| - } else { |
296 |
| - // OAuth and PAT do not require a username |
| 293 | + } |
| 294 | + if (isUsernameRequired(authenticator)) { |
297 | 295 | AssertUtil.assertTrue(
|
298 | 296 | loginInput.getUserName() != null, "missing user name for opening session");
|
299 | 297 | }
|
300 |
| - if (authenticator.equals(AuthenticatorType.EXTERNALBROWSER) |
301 |
| - || authenticator.equals(AuthenticatorType.OAUTH_AUTHORIZATION_CODE) |
302 |
| - || authenticator.equals(AuthenticatorType.OAUTH_CLIENT_CREDENTIALS)) { |
| 298 | + if (isEligibleForTokenCaching(authenticator)) { |
303 | 299 | if ((Constants.getOS() == Constants.OS.MAC || Constants.getOS() == Constants.OS.WINDOWS)
|
304 | 300 | && loginInput.isEnableClientStoreTemporaryCredential()) {
|
305 | 301 | // force to set the flag for Mac/Windows users
|
@@ -328,7 +324,7 @@ static SFLoginOutput openSession(
|
328 | 324 | }
|
329 | 325 | }
|
330 | 326 |
|
331 |
| - readCachedTokens(loginInput); |
| 327 | + readCachedTokensIfPossible(loginInput); |
332 | 328 |
|
333 | 329 | if (OAuthAccessTokenProviderFactory.isEligible(getAuthenticator(loginInput))) {
|
334 | 330 | obtainAuthAccessTokenAndUpdateInput(loginInput);
|
@@ -362,6 +358,24 @@ static void checkIfExperimentalAuthnEnabled(AuthenticatorType authenticator) thr
|
362 | 358 | }
|
363 | 359 | }
|
364 | 360 |
|
| 361 | + private static boolean isEligibleForTokenCaching(AuthenticatorType authenticator) { |
| 362 | + return authenticator.equals(AuthenticatorType.EXTERNALBROWSER) |
| 363 | + || authenticator.equals(AuthenticatorType.OAUTH_AUTHORIZATION_CODE) |
| 364 | + || authenticator.equals(AuthenticatorType.OAUTH_CLIENT_CREDENTIALS); |
| 365 | + } |
| 366 | + |
| 367 | + private static boolean isTokenOrPasswordRequired(AuthenticatorType authenticator) { |
| 368 | + return authenticator.equals(AuthenticatorType.OAUTH) |
| 369 | + || authenticator.equals(AuthenticatorType.PROGRAMMATIC_ACCESS_TOKEN); |
| 370 | + } |
| 371 | + |
| 372 | + private static boolean isUsernameRequired(AuthenticatorType authenticator) { |
| 373 | + return !authenticator.equals(AuthenticatorType.OAUTH) |
| 374 | + && !authenticator.equals(AuthenticatorType.PROGRAMMATIC_ACCESS_TOKEN) |
| 375 | + && !authenticator.equals(AuthenticatorType.OAUTH_AUTHORIZATION_CODE) |
| 376 | + && !authenticator.equals(AuthenticatorType.OAUTH_CLIENT_CREDENTIALS); |
| 377 | + } |
| 378 | + |
365 | 379 | private static void obtainAuthAccessTokenAndUpdateInput(SFLoginInput loginInput)
|
366 | 380 | throws SFException {
|
367 | 381 | if (loginInput.getOauthAccessToken() != null) { // Access Token was cached
|
@@ -410,11 +424,13 @@ private static void refreshOAuthAccessTokenAndUpdateInput(SFLoginInput loginInpu
|
410 | 424 | }
|
411 | 425 | }
|
412 | 426 |
|
413 |
| - private static void readCachedTokens(SFLoginInput loginInput) throws SFException { |
| 427 | + private static void readCachedTokensIfPossible(SFLoginInput loginInput) throws SFException { |
414 | 428 | if (asBoolean(loginInput.getSessionParameters().get(CLIENT_STORE_TEMPORARY_CREDENTIAL))) {
|
415 |
| - CredentialManager.fillCachedIdToken(loginInput); |
416 |
| - CredentialManager.fillCachedOAuthAccessToken(loginInput); |
417 |
| - CredentialManager.fillCachedOAuthRefreshToken(loginInput); |
| 429 | + if (!StringUtils.isNullOrEmpty(loginInput.getUserName())) { |
| 430 | + CredentialManager.fillCachedIdToken(loginInput); |
| 431 | + CredentialManager.fillCachedOAuthAccessToken(loginInput); |
| 432 | + CredentialManager.fillCachedOAuthRefreshToken(loginInput); |
| 433 | + } |
418 | 434 | }
|
419 | 435 |
|
420 | 436 | if (asBoolean(loginInput.getSessionParameters().get(CLIENT_REQUEST_MFA_TOKEN))) {
|
@@ -607,10 +623,6 @@ static SFLoginOutput newSession(
|
607 | 623 | if (authenticatorType == AuthenticatorType.OAUTH
|
608 | 624 | && loginInput.getOriginalAuthenticator() != null) {
|
609 | 625 | data.put(ClientAuthnParameter.OAUTH_TYPE.name(), loginInput.getOriginalAuthenticator());
|
610 |
| - URI idpUri = |
611 |
| - OAuthUtil.getTokenRequestUrl( |
612 |
| - loginInput.getOauthLoginInput(), loginInput.getServerUrl()); |
613 |
| - data.put(ClientAuthnParameter.IDP_HOST.name(), idpUri.getHost()); |
614 | 626 | }
|
615 | 627 |
|
616 | 628 | // map of client environment parameters, including connection parameters
|
|
0 commit comments