Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #13 from justcolin/initializeStatefulVariables
Browse files Browse the repository at this point in the history
Allow multiple calls to init()
  • Loading branch information
subyraman authored Dec 18, 2018
2 parents ee0a7ad + e8fa528 commit 410a918
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 6 deletions.
34 changes: 30 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,36 @@
'duomobile.s3-us-west-1.amazonaws.com'
];

var iframeId = 'duo_iframe',
postAction = '',
postArgument = 'sig_response',
var iframeId,
postAction,
postArgument,
host,
sigRequest,
duoSig,
appSig,
iframe,
submitCallback;

// We use this function instead of setting initial values in the var
// declarations to make sure the initial values and subsequent
// re-initializations are always the same.
initializeStatefulVariables();

/**
* Set local variables to whatever they should be before you call init().
*/
function initializeStatefulVariables() {
iframeId = 'duo_iframe';
postAction = '';
postArgument = 'sig_response';
host = undefined;
sigRequest = undefined;
duoSig = undefined;
appSig = undefined;
iframe = undefined;
submitCallback = undefined;
}

function throwError(message, url) {
throw new Error(
'Duo Web SDK error: ' + message +
Expand Down Expand Up @@ -225,6 +245,12 @@
* submit_callback can be used to prevent the webpage from reloading.
*/
function init(options) {
// If init() is called more than once we have to reset all the local
// variables to ensure init() will work the same way every time. This
// helps people making single page applications. SPAs may periodically
// remove the iframe and add a new one that has to be initialized.
initializeStatefulVariables();

if (options) {
if (options.host) {
host = options.host;
Expand Down Expand Up @@ -375,7 +401,7 @@
iframe.src = [
'https://', host, '/frame/web/v1/auth?tx=', duoSig,
'&parent=', encodeURIComponent(document.location.href),
'&v=2.6'
'&v=2.7'
].join('');

// listen for the 'message' event
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "duo_web_sdk",
"version": "2.6.0",
"version": "2.7.0",
"description": "Provides the Duo Web Javascript in an ES6 module format that can be installed via npm and bundled into your web application.",
"main": "index.js",
"dependencies": {},
Expand Down
94 changes: 93 additions & 1 deletion test/test-duo-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ describe('Duo Web', function() {
/* Dummy sig_request, passes validation. */
var sig_request = 'AUTH|duo_sig:app_sig';

beforeEach(function() {
function addIframe() {
/* Create some document elements that doPostBack expects. */
iframe = document.createElement('iframe');
var form = document.createElement('duo_form');
form.id = 'duo_form';
document.head.appendChild(iframe);
iframe.appendChild(form);
}

beforeEach(function() {
addIframe();
});

it('Should totes work with submitCallback', function() {
Expand All @@ -34,6 +38,18 @@ describe('Duo Web', function() {
assert.isTrue(submitCallbackCalled);
});

it('should include the host in the src', function() {
var host = 'this-is-a-host.example';

Duo.init({
host: host,
iframe: iframe,
sig_request: sig_request
});

assert.include(iframe.src, host);
});

it('should URI encode the parent URL', function() {
Duo.init({
host: 'example.com',
Expand All @@ -48,6 +64,82 @@ describe('Duo Web', function() {
iframe.src, 'parent=https://',
'should URI encode the parent href')
})

describe('Calling init() a second time', function() {
it('Should overwrite the old callback', function() {
var firstSubmitCallbackCalled = false;
var secondSubmitCallbackCalled = false;

Duo.init({
host: 'example.com',
iframe: iframe,
sig_request: sig_request,
submit_callback: function() {
firstSubmitCallbackCalled = true;
}
});

Duo.init({
host: 'example.com',
iframe: iframe,
sig_request: sig_request,
submit_callback: function() {
secondSubmitCallbackCalled = true;
}
});

Duo._doPostBack("Great Success!");

assert.isFalse(
firstSubmitCallbackCalled,
'did not overwrite the previous callback'
);
assert.isTrue(
secondSubmitCallbackCalled,
'did not call the new callback'
);
});

it('Should overwrite the iframe src', function() {
const firstHostName = 'first-host.example';
const secondHostName = 'second-host.example';
const thirdHostName = 'third-host.example';

// Initialize with one host
Duo.init({
host: firstHostName,
iframe: iframe,
sig_request: sig_request
});

// Initialize with a different host
Duo.init({
host: secondHostName,
iframe: iframe,
sig_request: sig_request
});

assert.include(
iframe.src, secondHostName,
'did not change the src on the second init'
);


// Create a new iframe and initialze again to make sure the new
// iframe is targeted.
addIframe();
Duo.init({
host: thirdHostName,
iframe: iframe,
sig_request: sig_request
});

assert.include(
iframe.src, thirdHostName,
'did not change the src on the third init'
);
});
});
});

describe('parseSigRequest', function() {
Expand Down

0 comments on commit 410a918

Please sign in to comment.