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

[APPS-45767] Let an external browser callback be provided to use, supports Snowflake VS Code Extension SSO #1003

Merged
merged 2 commits into from
Feb 7, 2025
Merged
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
12 changes: 7 additions & 5 deletions lib/authentication/auth_idtoken.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const AuthWeb = require('./auth_web');
/**
* Creates an ID token authenticator.
*
* @param {String} token
*
* @returns {Object}
* @param {Object} connectionConfig
* @param {Object} httpClient
* @param {module} webbrowser
*
* @returns {Object} the authenticator
* @constructor
*/
function AuthIDToken(connectionConfig, httpClient) {
function AuthIDToken(connectionConfig, httpClient, webbrowser) {

this.idToken = connectionConfig.idToken;

Expand All @@ -31,7 +33,7 @@ function AuthIDToken(connectionConfig, httpClient) {
this.authenticate = async function () {};

this.reauthenticate = async function (body) {
const auth = new AuthWeb(connectionConfig, httpClient);
const auth = new AuthWeb(connectionConfig, httpClient, webbrowser);
await auth.authenticate(connectionConfig.getAuthenticator(),
connectionConfig.getServiceName(),
connectionConfig.account,
Expand Down
5 changes: 3 additions & 2 deletions lib/authentication/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ exports.formAuthJSON = function formAuthJSON(
*/
exports.getAuthenticator = function getAuthenticator(connectionConfig, httpClient) {
const authType = connectionConfig.getAuthenticator();
const openExternalBrowserCallback = connectionConfig.openExternalBrowserCallback; // Important for SSO in the Snowflake VS Code extension
let auth;
if (authType === AuthenticationTypes.DEFAULT_AUTHENTICATOR || authType === AuthenticationTypes.USER_PWD_MFA_AUTHENTICATOR) {
auth = new AuthDefault(connectionConfig);
} else if (authType === AuthenticationTypes.EXTERNAL_BROWSER_AUTHENTICATOR) {
if (connectionConfig.getClientStoreTemporaryCredential() && !!connectionConfig.idToken) {
auth = new AuthIDToken(connectionConfig, httpClient);
auth = new AuthIDToken(connectionConfig, httpClient, openExternalBrowserCallback);
} else {
auth = new AuthWeb(connectionConfig, httpClient);
auth = new AuthWeb(connectionConfig, httpClient, openExternalBrowserCallback);
}
} else if (authType === AuthenticationTypes.KEY_PAIR_AUTHENTICATOR) {
auth = new AuthKeypair(connectionConfig);
Expand Down
1 change: 1 addition & 0 deletions lib/connection/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ function ConnectionConfig(options, validateCredentials, qaMode, clientInfo) {
this.masterTokenExpirationTime = options.masterTokenExpirationTime;
this.sessionTokenExpirationTime = options.sessionTokenExpirationTime;
this.clientConfigFile = options.clientConfigFile;
this.openExternalBrowserCallback = options.openExternalBrowserCallback;

// create the parameters array
const parameters = createParameters();
Expand Down
13 changes: 12 additions & 1 deletion test/unit/authentication/authentication_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ describe('external browser authentication', function () {
body['data']['AUTHENTICATOR'], AuthenticationTypes.EXTERNAL_BROWSER_AUTHENTICATOR, 'Authenticator should be EXTERNALBROWSER');
});

it('external browser - id token', async function () {
it('external browser - id token, no webbrowser', async function () {
const auth = new AuthIDToken(connectionOptionsIdToken, httpclient);
await auth.authenticate(credentials.authenticator, '', credentials.account, credentials.username, credentials.host);

Expand All @@ -241,6 +241,17 @@ describe('external browser authentication', function () {
assert.strictEqual(body['data']['TOKEN'], connectionOptionsIdToken.idToken);
assert.strictEqual(body['data']['AUTHENTICATOR'], AuthenticationTypes.ID_TOKEN_AUTHENTICATOR);
});

it('external browser - id token, webbrowser cb provided', async function () {
const auth = new AuthIDToken(connectionOptionsIdToken, httpclient, webbrowser.open);
await auth.authenticate(credentials.authenticator, '', credentials.account, credentials.username, credentials.host);

const body = { data: {} };
auth.updateBody(body);

assert.strictEqual(body['data']['TOKEN'], connectionOptionsIdToken.idToken);
assert.strictEqual(body['data']['AUTHENTICATOR'], AuthenticationTypes.ID_TOKEN_AUTHENTICATOR);
});
});

describe('key-pair authentication', function () {
Expand Down
Loading