Skip to content

Commit

Permalink
Build the console login url
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-igarish committed Sep 30, 2023
1 parent 32e2080 commit 75248b5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/main/java/net/snowflake/client/core/SFLoginInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class SFLoginInput {
private String privateKeyFilePwd;
private String inFlightCtx; // Opaque string sent for Snowsight account activation

private boolean disableConsoleLogin = true;

// Additional headers to add for Snowsight.
Map<String, String> additionalHttpHeadersForSnowsight;

Expand All @@ -63,6 +65,15 @@ SFLoginInput setServerUrl(String serverUrl) {
return this;
}

public boolean getDisableConsoleLogin() {
return disableConsoleLogin;
}

SFLoginInput setDisableConsoleLogin(boolean disableConsoleLogin) {
this.disableConsoleLogin = disableConsoleLogin;
return this;
}

String getDatabaseName() {
return databaseName;
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/snowflake/client/core/SFSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,12 @@ public synchronized void open() throws SFException, SnowflakeSQLException {
.setApplication((String) connectionPropertiesMap.get(SFSessionProperty.APPLICATION))
.setServiceName(getServiceName())
.setOCSPMode(getOCSPMode())
.setHttpClientSettingsKey(httpClientSettingsKey);
.setHttpClientSettingsKey(httpClientSettingsKey)
.setDisableConsoleLogin(
connectionPropertiesMap.get(SFSessionProperty.DISABLE_CONSOLE_LOGIN) != null
? getBooleanValue(
connectionPropertiesMap.get(SFSessionProperty.DISABLE_CONSOLE_LOGIN))
: true);

// Enable or disable OOB telemetry based on connection parameter. Default is disabled.
// The value may still change later when session parameters from the server are read.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public enum SFSessionProperty {

MAX_HTTP_RETRIES("maxHttpRetries", false, Integer.class),

DISABLE_CONSOLE_LOGIN("disableConsoleLogin", false, Boolean.class),

PUT_GET_MAX_RETRIES("putGetMaxRetries", false, Integer.class);

// property key in string
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/snowflake/client/core/SessionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class SessionUtil {
private static final String SF_PATH_LOGIN_REQUEST = "/session/v1/login-request";
private static final String SF_PATH_TOKEN_REQUEST = "/session/token-request";
public static final String SF_PATH_AUTHENTICATOR_REQUEST = "/session/authenticator-request";
public static final String SF_PATH_CONSOLE_LOGIN_REQUEST = "/console/login";

public static final String SF_QUERY_SESSION_DELETE = "delete";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ private String getSSOUrl(int port) throws SFException, SnowflakeSQLException {
}
}

private String getConsoleLoginUrl(int port) throws SFException {
try {
String serverUrl = loginInput.getServerUrl();
String consoleLoginUrl = serverUrl;
consoleLoginUrl += SessionUtil.SF_PATH_CONSOLE_LOGIN_REQUEST;
consoleLoginUrl += "?login_name=" + loginInput.getUserName();
consoleLoginUrl += "&client_port=" + port;

logger.debug("console login url: {}", consoleLoginUrl);

return consoleLoginUrl;
} catch (Exception ex) {
throw new SFException(ex, ErrorCode.INTERNAL_ERROR, ex.getMessage());
}
}

/**
* Authenticate
*
Expand All @@ -227,13 +243,26 @@ void authenticate() throws SFException, SnowflakeSQLException {
// main procedure
int port = this.getLocalPort(ssocket);
logger.debug("Listening localhost:{}", port);
String ssoUrl = getSSOUrl(port);
this.handlers.output(
"Initiating login request with your identity provider. A "
+ "browser window should have opened for you to complete the "
+ "login. If you can't see it, check existing browser windows, "
+ "or your OS settings. Press CTRL+C to abort and try again...");
this.handlers.openBrowser(ssoUrl);

if (loginInput.getDisableConsoleLogin()) {
// Access GS to get SSO URL
String ssoUrl = getSSOUrl(port);
this.handlers.output(
"Initiating login request with your identity provider. A "
+ "browser window should have opened for you to complete the "
+ "login. If you can't see it, check existing browser windows, "
+ "or your OS settings. Press CTRL+C to abort and try again...");
this.handlers.openBrowser(ssoUrl);
} else {
// Multiple SAML way to do authentication via console login
String consoleLoginUrl = getConsoleLoginUrl(port);
this.handlers.output(
"Initiating login request with your identity provider(s). A "
+ "browser window should have opened for you to complete the "
+ "login. If you can't see it, check existing browser windows, "
+ "or your OS settings. Press CTRL+C to abort and try again...");
this.handlers.openBrowser(consoleLoginUrl);
}

while (true) {
Socket socket = ssocket.accept(); // start accepting the request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ private SFLoginInput initMockLoginInput() {
.thenReturn(ClientAuthnDTO.AuthenticatorType.EXTERNALBROWSER.name());
when(loginInput.getAccountName()).thenReturn("testaccount");
when(loginInput.getUserName()).thenReturn("testuser");
when(loginInput.getDisableConsoleLogin()).thenReturn(true);
return loginInput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ private SFLoginInput initMockLoginInput() {
.thenReturn(ClientAuthnDTO.AuthenticatorType.EXTERNALBROWSER.name());
when(loginInput.getAccountName()).thenReturn("testaccount");
when(loginInput.getUserName()).thenReturn("testuser");
when(loginInput.getDisableConsoleLogin()).thenReturn(true);
return loginInput;
}

Expand Down

0 comments on commit 75248b5

Please sign in to comment.