Skip to content

Commit

Permalink
Add the webbrowser into idtoken for external browser callback, add co…
Browse files Browse the repository at this point in the history
…nfig option that will use it
  • Loading branch information
Kelly Huntlin committed Feb 5, 2025
1 parent 9cae043 commit dcb4e2a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
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 externalBrowserCallback = connectionConfig.externalBrowserCallback;
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, externalBrowserCallback);
} else {
auth = new AuthWeb(connectionConfig, httpClient);
auth = new AuthWeb(connectionConfig, httpClient, externalBrowserCallback);
}
} 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.externalBrowserCallback = options.externalBrowserCallback;

// 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

0 comments on commit dcb4e2a

Please sign in to comment.