Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1663901: Enable terminal fallback for external browser authentication when browser launch fails #1894

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public interface AuthExternalBrowserHandlers {

// output
void output(String msg);

// input
String input();
}

static class DefaultAuthExternalBrowserHandlers implements AuthExternalBrowserHandlers {
Expand Down Expand Up @@ -97,6 +100,11 @@ public void openBrowser(String ssoUrl) throws SFException {
public void output(String msg) {
System.out.println(msg);
}

@Override
public String input() {
return System.console().readLine();
}
}

private final ObjectMapper mapper;
Expand Down Expand Up @@ -275,40 +283,58 @@ void authenticate() throws SFException, SnowflakeSQLException {
int port = this.getLocalPort(ssocket);
logger.debug("Listening localhost: {}", port);

String authUrl = getSSOUrl(port);
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);
authUrl = getSSOUrl(port);
} 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);
authUrl = getConsoleLoginUrl(port);
}

while (true) {
Socket socket = ssocket.accept(); // start accepting the request
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...");
try {
this.handlers.openBrowser(authUrl);
while (true) {
Socket socket = ssocket.accept(); // start accepting the request
try {
BufferedReader in =
new BufferedReader(new InputStreamReader(socket.getInputStream(), UTF8_CHARSET));
char[] buf = new char[16384];
int strLen = in.read(buf);
String[] rets = new String(buf, 0, strLen).split("\r\n");
if (!processOptions(rets, socket)) {
processSamlToken(rets, socket);
break;
}
} finally {
socket.close();
}
}
} catch (SFException e) {
this.handlers.output(authUrl);
this.handlers.output(
"We were unable to open a browser window for you, "
+ "please open the url above manually then paste the "
+ "URL you are redirected to into the terminal.");
String redirectUrl = this.handlers.input();
try {
BufferedReader in =
new BufferedReader(new InputStreamReader(socket.getInputStream(), UTF8_CHARSET));
char[] buf = new char[16384];
int strLen = in.read(buf);
String[] rets = new String(buf, 0, strLen).split("\r\n");
if (!processOptions(rets, socket)) {
processSamlToken(rets, socket);
break;
URI inputParameter = new URI(redirectUrl);
for (NameValuePair urlParam : URLEncodedUtils.parse(inputParameter, UTF8_CHARSET)) {
if ("token".equals(urlParam.getName())) {
this.token = urlParam.getValue();
break;
}
}
} finally {
socket.close();
} catch (URISyntaxException ex0) {
throw new SFException(
ErrorCode.NETWORK_ERROR,
String.format(
"Invalid redirect url. No token is given from the browser. %s, err: %s",
redirectUrl, ex0));
}
}
} catch (SocketTimeoutException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public void openBrowser(String ssoUrl) throws SFException {
public void output(String msg) {
// nop. No output
}

@Override
public String input() {
// nop. No input
return null;
}
}

/** Simulates SessionUtilExternalBrower without popping up a browser window */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public void openBrowser(String ssoUrl) throws SFException {
public void output(String msg) {
// nop. No output
}

@Override
public String input() {
// nop. No input
return null;
}
}

class FakeSessionUtilExternalBrowser extends SessionUtilExternalBrowser {
Expand Down