Skip to content

Commit

Permalink
added manual test for idtoken and refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jy committed Nov 21, 2023
1 parent 2b4707a commit c101a4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 0 additions & 2 deletions lib/connection/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,6 @@ function Connection(context)

// Get authenticator to use
const auth = Authenticator.getAuthenticator(connectionConfig, context.getHttpClient());
connectionConfig.auth = auth;
context.auth = auth;

try
{
Expand Down
5 changes: 3 additions & 2 deletions lib/services/sf.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const GSErrors = require('../constants/gs_errors')
const QueryContextCache = require('../queryContextCache');
const Logger = require('../logger');
const SecureStorage = require('../authentication/secureStorage');
const {authenticationTypes} = require('../authentication/authentication');
const {authenticationTypes, getAuthenticator} = require('../authentication/authentication');

function isRetryableNetworkError(err)
{
Expand Down Expand Up @@ -696,11 +696,12 @@ function StateAbstract(options)
if (body && !body.success)
{
var data = body.data;
const auth = getAuthenticator(connectionConfig, httpClient);

if(body.code === '390195' && data.authnMethod === 'TOKEN') {
Logger.getInstance().trace("ID Token being used has expired. Reauthenticating");
await SecureStorage.deleteCredential(connectionConfig.account, connectionConfig.username, authenticationTypes.ID_TOKEN_AUTHENTICATOR);
await connectionConfig.auth.reauthenticate(requestOptions.json);
await auth.reauthenticate(requestOptions.json);
httpClient.request(realRequestOptions);
return;
}
Expand Down
33 changes: 33 additions & 0 deletions test/integration/testManualConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require("assert");
const connOption = require("./connectionOptions");
const testUtil = require("./testUtil");
const Logger = require('../../lib/logger');
const SecureStorage = require('../../lib/authentication/secureStorage');

if (process.env.RUN_MANUAL_TESTS_ONLY == "true") {
describe.only("Run manual tests", function () {
Expand Down Expand Up @@ -70,6 +71,38 @@ if (process.env.RUN_MANUAL_TESTS_ONLY == "true") {
}
});
});

it("Connection - ID Token authenticator", async function (done) {

//Testing to obtain the id token.
await SecureStorage.deleteCredential(connectionOption.host, connectionOption.username, "ID_TOKEN");

const connectionOption = connOption.externalBrowser
const connection = snowflake.createConnection(
connectionOption
);
await connection.connectAsync(function (err) {
assert.ok(!err);
const idToken = SecureStorage.readCredential(connectionOption.host, connectionOption.username, "ID_TOKEN");
assert.ok( idToken !== null);
});
await testUtil.destroyConnectionAsync(connection);

//Testing with the id token.
const idTokenConnection = snowflake.createConnection(connectionOption);
idTokenConnection.connectAsync(function (err) {
assert.ok(!err);
});
await testUtil.destroyConnectionAsync(idTokenConnection);

//Testing with the id token.
await SecureStorage.writeCredential(connectionOption.host, connectionOption.username, "ID_TOKEN", "WRONG Token");
const wrongTokneConnection = testUtil.connectAsync(connOption);
await wrongTokneConnection.connectAsync(function (err) {
assert.ok(!err);
done();
});
})
});

describe("Connection test - oauth", function () {
Expand Down

0 comments on commit c101a4b

Please sign in to comment.