Remove u2f from globalnamespace
This commit is contained in:
parent
d6170ce4d8
commit
41b430b2cf
10 changed files with 325 additions and 378 deletions
|
@ -78,6 +78,7 @@ import initChangesDropdown from './init_changes_dropdown';
|
||||||
import AbuseReports from './abuse_reports';
|
import AbuseReports from './abuse_reports';
|
||||||
import { ajaxGet, convertPermissionToBoolean } from './lib/utils/common_utils';
|
import { ajaxGet, convertPermissionToBoolean } from './lib/utils/common_utils';
|
||||||
import AjaxLoadingSpinner from './ajax_loading_spinner';
|
import AjaxLoadingSpinner from './ajax_loading_spinner';
|
||||||
|
import U2FAuthenticate from './u2f/authenticate';
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var Dispatcher;
|
var Dispatcher;
|
||||||
|
@ -535,14 +536,16 @@ import AjaxLoadingSpinner from './ajax_loading_spinner';
|
||||||
case 'sessions':
|
case 'sessions':
|
||||||
case 'omniauth_callbacks':
|
case 'omniauth_callbacks':
|
||||||
if (!gon.u2f) break;
|
if (!gon.u2f) break;
|
||||||
gl.u2fAuthenticate = new gl.U2FAuthenticate(
|
const u2fAuthenticate = new U2FAuthenticate(
|
||||||
$('#js-authenticate-u2f'),
|
$('#js-authenticate-u2f'),
|
||||||
'#js-login-u2f-form',
|
'#js-login-u2f-form',
|
||||||
gon.u2f,
|
gon.u2f,
|
||||||
document.querySelector('#js-login-2fa-device'),
|
document.querySelector('#js-login-2fa-device'),
|
||||||
document.querySelector('.js-2fa-form'),
|
document.querySelector('.js-2fa-form'),
|
||||||
);
|
);
|
||||||
gl.u2fAuthenticate.start();
|
u2fAuthenticate.start();
|
||||||
|
// needed in rspec
|
||||||
|
gl.u2fAuthenticate = u2fAuthenticate;
|
||||||
case 'admin':
|
case 'admin':
|
||||||
new Admin();
|
new Admin();
|
||||||
switch (path[1]) {
|
switch (path[1]) {
|
||||||
|
|
|
@ -47,12 +47,6 @@ import './lib/utils/url_utility';
|
||||||
// behaviors
|
// behaviors
|
||||||
import './behaviors/';
|
import './behaviors/';
|
||||||
|
|
||||||
// u2f
|
|
||||||
import './u2f/authenticate';
|
|
||||||
import './u2f/error';
|
|
||||||
import './u2f/register';
|
|
||||||
import './u2f/util';
|
|
||||||
|
|
||||||
// everything else
|
// everything else
|
||||||
import './activities';
|
import './activities';
|
||||||
import './admin';
|
import './admin';
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
/* global U2FRegister */
|
import U2FRegister from './u2f/register';
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const twoFactorNode = document.querySelector('.js-two-factor-auth');
|
const twoFactorNode = document.querySelector('.js-two-factor-auth');
|
||||||
const skippable = twoFactorNode.dataset.twoFactorSkippable === 'true';
|
const skippable = twoFactorNode.dataset.twoFactorSkippable === 'true';
|
||||||
|
|
|
@ -1,19 +1,15 @@
|
||||||
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, prefer-arrow-callback, no-else-return, quotes, quote-props, comma-dangle, one-var, one-var-declaration-per-line, max-len */
|
/* eslint-disable func-names, wrap-iife */
|
||||||
/* global u2f */
|
/* global u2f */
|
||||||
/* global U2FError */
|
|
||||||
/* global U2FUtil */
|
|
||||||
|
|
||||||
import _ from 'underscore';
|
import _ from 'underscore';
|
||||||
|
import isU2FSupported from './util';
|
||||||
|
import U2FError from './error';
|
||||||
|
|
||||||
// Authenticate U2F (universal 2nd factor) devices for users to authenticate with.
|
// Authenticate U2F (universal 2nd factor) devices for users to authenticate with.
|
||||||
//
|
//
|
||||||
// State Flow #1: setup -> in_progress -> authenticated -> POST to server
|
// State Flow #1: setup -> in_progress -> authenticated -> POST to server
|
||||||
// State Flow #2: setup -> in_progress -> error -> setup
|
// State Flow #2: setup -> in_progress -> error -> setup
|
||||||
(function() {
|
export default class U2FAuthenticate {
|
||||||
const global = window.gl || (window.gl = {});
|
constructor(container, form, u2fParams, fallbackButton, fallbackUI) {
|
||||||
|
|
||||||
global.U2FAuthenticate = (function() {
|
|
||||||
function U2FAuthenticate(container, form, u2fParams, fallbackButton, fallbackUI) {
|
|
||||||
this.container = container;
|
this.container = container;
|
||||||
this.renderNotSupported = this.renderNotSupported.bind(this);
|
this.renderNotSupported = this.renderNotSupported.bind(this);
|
||||||
this.renderAuthenticated = this.renderAuthenticated.bind(this);
|
this.renderAuthenticated = this.renderAuthenticated.bind(this);
|
||||||
|
@ -27,8 +23,10 @@ import _ from 'underscore';
|
||||||
this.form = form;
|
this.form = form;
|
||||||
this.fallbackButton = fallbackButton;
|
this.fallbackButton = fallbackButton;
|
||||||
this.fallbackUI = fallbackUI;
|
this.fallbackUI = fallbackUI;
|
||||||
if (this.fallbackButton) this.fallbackButton.addEventListener('click', this.switchToFallbackUI.bind(this));
|
if (this.fallbackButton) {
|
||||||
this.signRequests = u2fParams.sign_requests.map(function(request) {
|
this.fallbackButton.addEventListener('click', this.switchToFallbackUI.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
// The U2F Javascript API v1.1 requires a single challenge, with
|
// The U2F Javascript API v1.1 requires a single challenge, with
|
||||||
// _no challenges per-request_. The U2F Javascript API v1.0 requires a
|
// _no challenges per-request_. The U2F Javascript API v1.0 requires a
|
||||||
// challenge per-request, which is done by copying the single challenge
|
// challenge per-request, which is done by copying the single challenge
|
||||||
|
@ -40,79 +38,71 @@ import _ from 'underscore';
|
||||||
// Note: The server library fixes this behaviour in (unreleased) version 1.0.0.
|
// Note: The server library fixes this behaviour in (unreleased) version 1.0.0.
|
||||||
// This can be removed once we upgrade.
|
// This can be removed once we upgrade.
|
||||||
// https://github.com/castle/ruby-u2f/commit/103f428071a81cd3d5f80c2e77d522d5029946a4
|
// https://github.com/castle/ruby-u2f/commit/103f428071a81cd3d5f80c2e77d522d5029946a4
|
||||||
return _(request).omit('challenge');
|
this.signRequests = u2fParams.sign_requests.map(request => _(request).omit('challenge'));
|
||||||
});
|
|
||||||
|
this.templates = {
|
||||||
|
notSupported: '#js-authenticate-u2f-not-supported',
|
||||||
|
setup: '#js-authenticate-u2f-setup',
|
||||||
|
inProgress: '#js-authenticate-u2f-in-progress',
|
||||||
|
error: '#js-authenticate-u2f-error',
|
||||||
|
authenticated: '#js-authenticate-u2f-authenticated',
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
U2FAuthenticate.prototype.start = function() {
|
start() {
|
||||||
if (U2FUtil.isU2FSupported()) {
|
if (isU2FSupported()) {
|
||||||
return this.renderInProgress();
|
return this.renderInProgress();
|
||||||
} else {
|
}
|
||||||
return this.renderNotSupported();
|
return this.renderNotSupported();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
U2FAuthenticate.prototype.authenticate = function() {
|
authenticate() {
|
||||||
return u2f.sign(this.appId, this.challenge, this.signRequests, (function(_this) {
|
return u2f.sign(this.appId, this.challenge, this.signRequests, (function (_this) {
|
||||||
return function(response) {
|
return function (response) {
|
||||||
var error;
|
|
||||||
if (response.errorCode) {
|
if (response.errorCode) {
|
||||||
error = new U2FError(response.errorCode, 'authenticate');
|
const error = new U2FError(response.errorCode, 'authenticate');
|
||||||
return _this.renderError(error);
|
return _this.renderError(error);
|
||||||
} else {
|
|
||||||
return _this.renderAuthenticated(JSON.stringify(response));
|
|
||||||
}
|
}
|
||||||
|
return _this.renderAuthenticated(JSON.stringify(response));
|
||||||
};
|
};
|
||||||
})(this), 10);
|
})(this), 10);
|
||||||
};
|
}
|
||||||
|
|
||||||
// Rendering #
|
renderTemplate(name, params) {
|
||||||
U2FAuthenticate.prototype.templates = {
|
const templateString = $(this.templates[name]).html();
|
||||||
"notSupported": "#js-authenticate-u2f-not-supported",
|
const template = _.template(templateString);
|
||||||
"setup": '#js-authenticate-u2f-setup',
|
|
||||||
"inProgress": '#js-authenticate-u2f-in-progress',
|
|
||||||
"error": '#js-authenticate-u2f-error',
|
|
||||||
"authenticated": '#js-authenticate-u2f-authenticated'
|
|
||||||
};
|
|
||||||
|
|
||||||
U2FAuthenticate.prototype.renderTemplate = function(name, params) {
|
|
||||||
var template, templateString;
|
|
||||||
templateString = $(this.templates[name]).html();
|
|
||||||
template = _.template(templateString);
|
|
||||||
return this.container.html(template(params));
|
return this.container.html(template(params));
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FAuthenticate.prototype.renderInProgress = function() {
|
renderInProgress() {
|
||||||
this.renderTemplate('inProgress');
|
this.renderTemplate('inProgress');
|
||||||
return this.authenticate();
|
return this.authenticate();
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FAuthenticate.prototype.renderError = function(error) {
|
renderError(error) {
|
||||||
this.renderTemplate('error', {
|
this.renderTemplate('error', {
|
||||||
error_message: error.message(),
|
error_message: error.message(),
|
||||||
error_code: error.errorCode
|
error_code: error.errorCode,
|
||||||
});
|
});
|
||||||
return this.container.find('#js-u2f-try-again').on('click', this.renderInProgress);
|
return this.container.find('#js-u2f-try-again').on('click', this.renderInProgress);
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FAuthenticate.prototype.renderAuthenticated = function(deviceResponse) {
|
renderAuthenticated(deviceResponse) {
|
||||||
this.renderTemplate('authenticated');
|
this.renderTemplate('authenticated');
|
||||||
const container = this.container[0];
|
const container = this.container[0];
|
||||||
container.querySelector('#js-device-response').value = deviceResponse;
|
container.querySelector('#js-device-response').value = deviceResponse;
|
||||||
container.querySelector(this.form).submit();
|
container.querySelector(this.form).submit();
|
||||||
this.fallbackButton.classList.add('hidden');
|
this.fallbackButton.classList.add('hidden');
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FAuthenticate.prototype.renderNotSupported = function() {
|
renderNotSupported() {
|
||||||
return this.renderTemplate('notSupported');
|
return this.renderTemplate('notSupported');
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FAuthenticate.prototype.switchToFallbackUI = function() {
|
switchToFallbackUI() {
|
||||||
this.fallbackButton.classList.add('hidden');
|
this.fallbackButton.classList.add('hidden');
|
||||||
this.container[0].classList.add('hidden');
|
this.container[0].classList.add('hidden');
|
||||||
this.fallbackUI.classList.remove('hidden');
|
this.fallbackUI.classList.remove('hidden');
|
||||||
};
|
}
|
||||||
|
|
||||||
return U2FAuthenticate;
|
}
|
||||||
})();
|
|
||||||
})();
|
|
||||||
|
|
|
@ -1,25 +1,22 @@
|
||||||
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, no-console, quotes, prefer-template, max-len */
|
export default class U2FError {
|
||||||
/* global u2f */
|
constructor(errorCode, u2fFlowType) {
|
||||||
|
|
||||||
(function() {
|
|
||||||
this.U2FError = (function() {
|
|
||||||
function U2FError(errorCode, u2fFlowType) {
|
|
||||||
this.errorCode = errorCode;
|
this.errorCode = errorCode;
|
||||||
this.message = this.message.bind(this);
|
this.message = this.message.bind(this);
|
||||||
this.httpsDisabled = window.location.protocol !== 'https:';
|
this.httpsDisabled = window.location.protocol !== 'https:';
|
||||||
this.u2fFlowType = u2fFlowType;
|
this.u2fFlowType = u2fFlowType;
|
||||||
}
|
}
|
||||||
|
|
||||||
U2FError.prototype.message = function() {
|
message() {
|
||||||
if (this.errorCode === u2f.ErrorCodes.BAD_REQUEST && this.httpsDisabled) {
|
if (this.errorCode === window.u2f.ErrorCodes.BAD_REQUEST && this.httpsDisabled) {
|
||||||
return 'U2F only works with HTTPS-enabled websites. Contact your administrator for more details.';
|
return 'U2F only works with HTTPS-enabled websites. Contact your administrator for more details.';
|
||||||
} else if (this.errorCode === u2f.ErrorCodes.DEVICE_INELIGIBLE) {
|
} else if (this.errorCode === window.u2f.ErrorCodes.DEVICE_INELIGIBLE) {
|
||||||
if (this.u2fFlowType === 'authenticate') return 'This device has not been registered with us.';
|
if (this.u2fFlowType === 'authenticate') {
|
||||||
if (this.u2fFlowType === 'register') return 'This device has already been registered with us.';
|
return 'This device has not been registered with us.';
|
||||||
}
|
}
|
||||||
return "There was a problem communicating with your device.";
|
if (this.u2fFlowType === 'register') {
|
||||||
};
|
return 'This device has already been registered with us.';
|
||||||
|
}
|
||||||
return U2FError;
|
}
|
||||||
})();
|
return 'There was a problem communicating with your device.';
|
||||||
}).call(window);
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, no-else-return, quotes, quote-props, comma-dangle, one-var, one-var-declaration-per-line, max-len */
|
/* eslint-disable func-names, wrap-iife */
|
||||||
/* global u2f */
|
/* global u2f */
|
||||||
/* global U2FError */
|
|
||||||
/* global U2FUtil */
|
|
||||||
|
|
||||||
import _ from 'underscore';
|
import _ from 'underscore';
|
||||||
|
import isU2FSupported from './util';
|
||||||
|
import U2FError from './error';
|
||||||
|
|
||||||
// Register U2F (universal 2nd factor) devices for users to authenticate with.
|
// Register U2F (universal 2nd factor) devices for users to authenticate with.
|
||||||
//
|
//
|
||||||
// State Flow #1: setup -> in_progress -> registered -> POST to server
|
// State Flow #1: setup -> in_progress -> registered -> POST to server
|
||||||
// State Flow #2: setup -> in_progress -> error -> setup
|
// State Flow #2: setup -> in_progress -> error -> setup
|
||||||
(function() {
|
export default class U2FRegister {
|
||||||
this.U2FRegister = (function() {
|
constructor(container, u2fParams) {
|
||||||
function U2FRegister(container, u2fParams) {
|
|
||||||
this.container = container;
|
this.container = container;
|
||||||
this.renderNotSupported = this.renderNotSupported.bind(this);
|
this.renderNotSupported = this.renderNotSupported.bind(this);
|
||||||
this.renderRegistered = this.renderRegistered.bind(this);
|
this.renderRegistered = this.renderRegistered.bind(this);
|
||||||
|
@ -24,75 +23,67 @@ import _ from 'underscore';
|
||||||
this.appId = u2fParams.app_id;
|
this.appId = u2fParams.app_id;
|
||||||
this.registerRequests = u2fParams.register_requests;
|
this.registerRequests = u2fParams.register_requests;
|
||||||
this.signRequests = u2fParams.sign_requests;
|
this.signRequests = u2fParams.sign_requests;
|
||||||
|
|
||||||
|
this.templates = {
|
||||||
|
notSupported: '#js-register-u2f-not-supported',
|
||||||
|
setup: '#js-register-u2f-setup',
|
||||||
|
inProgress: '#js-register-u2f-in-progress',
|
||||||
|
error: '#js-register-u2f-error',
|
||||||
|
registered: '#js-register-u2f-registered',
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
U2FRegister.prototype.start = function() {
|
start() {
|
||||||
if (U2FUtil.isU2FSupported()) {
|
if (isU2FSupported()) {
|
||||||
return this.renderSetup();
|
return this.renderSetup();
|
||||||
} else {
|
}
|
||||||
return this.renderNotSupported();
|
return this.renderNotSupported();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
U2FRegister.prototype.register = function() {
|
register() {
|
||||||
return u2f.register(this.appId, this.registerRequests, this.signRequests, (function(_this) {
|
return u2f.register(this.appId, this.registerRequests, this.signRequests, (function (_this) {
|
||||||
return function(response) {
|
return function (response) {
|
||||||
var error;
|
|
||||||
if (response.errorCode) {
|
if (response.errorCode) {
|
||||||
error = new U2FError(response.errorCode, 'register');
|
const error = new U2FError(response.errorCode, 'register');
|
||||||
return _this.renderError(error);
|
return _this.renderError(error);
|
||||||
} else {
|
|
||||||
return _this.renderRegistered(JSON.stringify(response));
|
|
||||||
}
|
}
|
||||||
|
return _this.renderRegistered(JSON.stringify(response));
|
||||||
};
|
};
|
||||||
})(this), 10);
|
})(this), 10);
|
||||||
};
|
}
|
||||||
|
|
||||||
// Rendering #
|
renderTemplate(name, params) {
|
||||||
U2FRegister.prototype.templates = {
|
const templateString = $(this.templates[name]).html();
|
||||||
"notSupported": "#js-register-u2f-not-supported",
|
const template = _.template(templateString);
|
||||||
"setup": '#js-register-u2f-setup',
|
|
||||||
"inProgress": '#js-register-u2f-in-progress',
|
|
||||||
"error": '#js-register-u2f-error',
|
|
||||||
"registered": '#js-register-u2f-registered'
|
|
||||||
};
|
|
||||||
|
|
||||||
U2FRegister.prototype.renderTemplate = function(name, params) {
|
|
||||||
var template, templateString;
|
|
||||||
templateString = $(this.templates[name]).html();
|
|
||||||
template = _.template(templateString);
|
|
||||||
return this.container.html(template(params));
|
return this.container.html(template(params));
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FRegister.prototype.renderSetup = function() {
|
renderSetup() {
|
||||||
this.renderTemplate('setup');
|
this.renderTemplate('setup');
|
||||||
return this.container.find('#js-setup-u2f-device').on('click', this.renderInProgress);
|
return this.container.find('#js-setup-u2f-device').on('click', this.renderInProgress);
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FRegister.prototype.renderInProgress = function() {
|
renderInProgress() {
|
||||||
this.renderTemplate('inProgress');
|
this.renderTemplate('inProgress');
|
||||||
return this.register();
|
return this.register();
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FRegister.prototype.renderError = function(error) {
|
renderError(error) {
|
||||||
this.renderTemplate('error', {
|
this.renderTemplate('error', {
|
||||||
error_message: error.message(),
|
error_message: error.message(),
|
||||||
error_code: error.errorCode
|
error_code: error.errorCode,
|
||||||
});
|
});
|
||||||
return this.container.find('#js-u2f-try-again').on('click', this.renderSetup);
|
return this.container.find('#js-u2f-try-again').on('click', this.renderSetup);
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FRegister.prototype.renderRegistered = function(deviceResponse) {
|
renderRegistered(deviceResponse) {
|
||||||
this.renderTemplate('registered');
|
this.renderTemplate('registered');
|
||||||
// Prefer to do this instead of interpolating using Underscore templates
|
// Prefer to do this instead of interpolating using Underscore templates
|
||||||
// because of JSON escaping issues.
|
// because of JSON escaping issues.
|
||||||
return this.container.find("#js-device-response").val(deviceResponse);
|
return this.container.find('#js-device-response').val(deviceResponse);
|
||||||
};
|
}
|
||||||
|
|
||||||
U2FRegister.prototype.renderNotSupported = function() {
|
renderNotSupported() {
|
||||||
return this.renderTemplate('notSupported');
|
return this.renderTemplate('notSupported');
|
||||||
};
|
}
|
||||||
|
}
|
||||||
return U2FRegister;
|
|
||||||
})();
|
|
||||||
}).call(window);
|
|
||||||
|
|
|
@ -1,12 +1,3 @@
|
||||||
/* eslint-disable func-names, space-before-function-paren, wrap-iife */
|
export default function isU2FSupported() {
|
||||||
(function() {
|
|
||||||
this.U2FUtil = (function() {
|
|
||||||
function U2FUtil() {}
|
|
||||||
|
|
||||||
U2FUtil.isU2FSupported = function() {
|
|
||||||
return window.u2f;
|
return window.u2f;
|
||||||
};
|
}
|
||||||
|
|
||||||
return U2FUtil;
|
|
||||||
})();
|
|
||||||
}).call(window);
|
|
||||||
|
|
|
@ -1,29 +1,22 @@
|
||||||
/* eslint-disable space-before-function-paren, new-parens, quotes, comma-dangle, no-var, one-var, one-var-declaration-per-line, max-len */
|
import U2FAuthenticate from '~/u2f/authenticate';
|
||||||
/* global MockU2FDevice */
|
|
||||||
/* global U2FAuthenticate */
|
|
||||||
|
|
||||||
import '~/u2f/authenticate';
|
|
||||||
import '~/u2f/util';
|
|
||||||
import '~/u2f/error';
|
|
||||||
import 'vendor/u2f';
|
import 'vendor/u2f';
|
||||||
import './mock_u2f_device';
|
import MockU2FDevice from './mock_u2f_device';
|
||||||
|
|
||||||
(function() {
|
describe('U2FAuthenticate', () => {
|
||||||
describe('U2FAuthenticate', function() {
|
|
||||||
preloadFixtures('u2f/authenticate.html.raw');
|
preloadFixtures('u2f/authenticate.html.raw');
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(() => {
|
||||||
loadFixtures('u2f/authenticate.html.raw');
|
loadFixtures('u2f/authenticate.html.raw');
|
||||||
this.u2fDevice = new MockU2FDevice;
|
this.u2fDevice = new MockU2FDevice();
|
||||||
this.container = $("#js-authenticate-u2f");
|
this.container = $('#js-authenticate-u2f');
|
||||||
this.component = new window.gl.U2FAuthenticate(
|
this.component = new U2FAuthenticate(
|
||||||
this.container,
|
this.container,
|
||||||
'#js-login-u2f-form',
|
'#js-login-u2f-form',
|
||||||
{
|
{
|
||||||
sign_requests: []
|
sign_requests: [],
|
||||||
},
|
},
|
||||||
document.querySelector('#js-login-2fa-device'),
|
document.querySelector('#js-login-2fa-device'),
|
||||||
document.querySelector('.js-2fa-form')
|
document.querySelector('.js-2fa-form'),
|
||||||
);
|
);
|
||||||
|
|
||||||
// bypass automatic form submission within renderAuthenticated
|
// bypass automatic form submission within renderAuthenticated
|
||||||
|
@ -31,42 +24,40 @@ import './mock_u2f_device';
|
||||||
|
|
||||||
return this.component.start();
|
return this.component.start();
|
||||||
});
|
});
|
||||||
it('allows authenticating via a U2F device', function() {
|
|
||||||
var inProgressMessage;
|
it('allows authenticating via a U2F device', () => {
|
||||||
inProgressMessage = this.container.find("p");
|
const inProgressMessage = this.container.find('p');
|
||||||
expect(inProgressMessage.text()).toContain("Trying to communicate with your device");
|
expect(inProgressMessage.text()).toContain('Trying to communicate with your device');
|
||||||
this.u2fDevice.respondToAuthenticateRequest({
|
this.u2fDevice.respondToAuthenticateRequest({
|
||||||
deviceData: "this is data from the device"
|
deviceData: 'this is data from the device',
|
||||||
});
|
});
|
||||||
expect(this.component.renderAuthenticated).toHaveBeenCalledWith('{"deviceData":"this is data from the device"}');
|
expect(this.component.renderAuthenticated).toHaveBeenCalledWith('{"deviceData":"this is data from the device"}');
|
||||||
});
|
});
|
||||||
return describe("errors", function() {
|
|
||||||
it("displays an error message", function() {
|
return describe('errors', () => {
|
||||||
var errorMessage, setupButton;
|
it('displays an error message', () => {
|
||||||
setupButton = this.container.find("#js-login-u2f-device");
|
const setupButton = this.container.find('#js-login-u2f-device');
|
||||||
setupButton.trigger('click');
|
setupButton.trigger('click');
|
||||||
this.u2fDevice.respondToAuthenticateRequest({
|
this.u2fDevice.respondToAuthenticateRequest({
|
||||||
errorCode: "error!"
|
errorCode: 'error!',
|
||||||
});
|
});
|
||||||
errorMessage = this.container.find("p");
|
const errorMessage = this.container.find('p');
|
||||||
return expect(errorMessage.text()).toContain("There was a problem communicating with your device");
|
return expect(errorMessage.text()).toContain('There was a problem communicating with your device');
|
||||||
});
|
});
|
||||||
return it("allows retrying authentication after an error", function() {
|
return it('allows retrying authentication after an error', () => {
|
||||||
var retryButton, setupButton;
|
let setupButton = this.container.find('#js-login-u2f-device');
|
||||||
setupButton = this.container.find("#js-login-u2f-device");
|
|
||||||
setupButton.trigger('click');
|
setupButton.trigger('click');
|
||||||
this.u2fDevice.respondToAuthenticateRequest({
|
this.u2fDevice.respondToAuthenticateRequest({
|
||||||
errorCode: "error!"
|
errorCode: 'error!',
|
||||||
});
|
});
|
||||||
retryButton = this.container.find("#js-u2f-try-again");
|
const retryButton = this.container.find('#js-u2f-try-again');
|
||||||
retryButton.trigger('click');
|
retryButton.trigger('click');
|
||||||
setupButton = this.container.find("#js-login-u2f-device");
|
setupButton = this.container.find('#js-login-u2f-device');
|
||||||
setupButton.trigger('click');
|
setupButton.trigger('click');
|
||||||
this.u2fDevice.respondToAuthenticateRequest({
|
this.u2fDevice.respondToAuthenticateRequest({
|
||||||
deviceData: "this is data from the device"
|
deviceData: 'this is data from the device',
|
||||||
});
|
});
|
||||||
expect(this.component.renderAuthenticated).toHaveBeenCalledWith('{"deviceData":"this is data from the device"}');
|
expect(this.component.renderAuthenticated).toHaveBeenCalledWith('{"deviceData":"this is data from the device"}');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}).call(window);
|
|
||||||
|
|
|
@ -1,31 +1,28 @@
|
||||||
/* eslint-disable space-before-function-paren, no-var, prefer-rest-params, wrap-iife, no-unused-expressions, no-return-assign, no-param-reassign, max-len */
|
/* eslint-disable prefer-rest-params, wrap-iife,
|
||||||
|
no-unused-expressions, no-return-assign, no-param-reassign*/
|
||||||
|
|
||||||
(function() {
|
export default class MockU2FDevice {
|
||||||
this.MockU2FDevice = (function() {
|
constructor() {
|
||||||
function MockU2FDevice() {
|
|
||||||
this.respondToAuthenticateRequest = this.respondToAuthenticateRequest.bind(this);
|
this.respondToAuthenticateRequest = this.respondToAuthenticateRequest.bind(this);
|
||||||
this.respondToRegisterRequest = this.respondToRegisterRequest.bind(this);
|
this.respondToRegisterRequest = this.respondToRegisterRequest.bind(this);
|
||||||
window.u2f || (window.u2f = {});
|
window.u2f || (window.u2f = {});
|
||||||
window.u2f.register = (function(_this) {
|
window.u2f.register = (function (_this) {
|
||||||
return function(appId, registerRequests, signRequests, callback) {
|
return function (appId, registerRequests, signRequests, callback) {
|
||||||
return _this.registerCallback = callback;
|
return _this.registerCallback = callback;
|
||||||
};
|
};
|
||||||
})(this);
|
})(this);
|
||||||
window.u2f.sign = (function(_this) {
|
window.u2f.sign = (function (_this) {
|
||||||
return function(appId, challenges, signRequests, callback) {
|
return function (appId, challenges, signRequests, callback) {
|
||||||
return _this.authenticateCallback = callback;
|
return _this.authenticateCallback = callback;
|
||||||
};
|
};
|
||||||
})(this);
|
})(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
MockU2FDevice.prototype.respondToRegisterRequest = function(params) {
|
respondToRegisterRequest(params) {
|
||||||
return this.registerCallback(params);
|
return this.registerCallback(params);
|
||||||
};
|
}
|
||||||
|
|
||||||
MockU2FDevice.prototype.respondToAuthenticateRequest = function(params) {
|
respondToAuthenticateRequest(params) {
|
||||||
return this.authenticateCallback(params);
|
return this.authenticateCallback(params);
|
||||||
};
|
}
|
||||||
|
}
|
||||||
return MockU2FDevice;
|
|
||||||
})();
|
|
||||||
}).call(window);
|
|
||||||
|
|
|
@ -1,77 +1,69 @@
|
||||||
/* eslint-disable space-before-function-paren, new-parens, quotes, no-var, one-var, one-var-declaration-per-line, comma-dangle, max-len */
|
import U2FRegister from '~/u2f/register';
|
||||||
/* global MockU2FDevice */
|
|
||||||
/* global U2FRegister */
|
|
||||||
|
|
||||||
import '~/u2f/register';
|
|
||||||
import '~/u2f/util';
|
|
||||||
import '~/u2f/error';
|
|
||||||
import 'vendor/u2f';
|
import 'vendor/u2f';
|
||||||
import './mock_u2f_device';
|
import MockU2FDevice from './mock_u2f_device';
|
||||||
|
|
||||||
(function() {
|
describe('U2FRegister', () => {
|
||||||
describe('U2FRegister', function() {
|
|
||||||
preloadFixtures('u2f/register.html.raw');
|
preloadFixtures('u2f/register.html.raw');
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(() => {
|
||||||
loadFixtures('u2f/register.html.raw');
|
loadFixtures('u2f/register.html.raw');
|
||||||
this.u2fDevice = new MockU2FDevice;
|
this.u2fDevice = new MockU2FDevice();
|
||||||
this.container = $("#js-register-u2f");
|
this.container = $('#js-register-u2f');
|
||||||
this.component = new U2FRegister(this.container, $("#js-register-u2f-templates"), {}, "token");
|
this.component = new U2FRegister(this.container, $('#js-register-u2f-templates'), {}, 'token');
|
||||||
return this.component.start();
|
return this.component.start();
|
||||||
});
|
});
|
||||||
it('allows registering a U2F device', function() {
|
|
||||||
var deviceResponse, inProgressMessage, registeredMessage, setupButton;
|
it('allows registering a U2F device', () => {
|
||||||
setupButton = this.container.find("#js-setup-u2f-device");
|
const setupButton = this.container.find('#js-setup-u2f-device');
|
||||||
expect(setupButton.text()).toBe('Setup new U2F device');
|
expect(setupButton.text()).toBe('Setup new U2F device');
|
||||||
setupButton.trigger('click');
|
setupButton.trigger('click');
|
||||||
inProgressMessage = this.container.children("p");
|
const inProgressMessage = this.container.children('p');
|
||||||
expect(inProgressMessage.text()).toContain("Trying to communicate with your device");
|
expect(inProgressMessage.text()).toContain('Trying to communicate with your device');
|
||||||
this.u2fDevice.respondToRegisterRequest({
|
this.u2fDevice.respondToRegisterRequest({
|
||||||
deviceData: "this is data from the device"
|
deviceData: 'this is data from the device',
|
||||||
});
|
});
|
||||||
registeredMessage = this.container.find('p');
|
const registeredMessage = this.container.find('p');
|
||||||
deviceResponse = this.container.find('#js-device-response');
|
const deviceResponse = this.container.find('#js-device-response');
|
||||||
expect(registeredMessage.text()).toContain("Your device was successfully set up!");
|
expect(registeredMessage.text()).toContain('Your device was successfully set up!');
|
||||||
return expect(deviceResponse.val()).toBe('{"deviceData":"this is data from the device"}');
|
return expect(deviceResponse.val()).toBe('{"deviceData":"this is data from the device"}');
|
||||||
});
|
});
|
||||||
return describe("errors", function() {
|
|
||||||
it("doesn't allow the same device to be registered twice (for the same user", function() {
|
return describe('errors', () => {
|
||||||
var errorMessage, setupButton;
|
it('doesn\'t allow the same device to be registered twice (for the same user', () => {
|
||||||
setupButton = this.container.find("#js-setup-u2f-device");
|
const setupButton = this.container.find('#js-setup-u2f-device');
|
||||||
setupButton.trigger('click');
|
setupButton.trigger('click');
|
||||||
this.u2fDevice.respondToRegisterRequest({
|
this.u2fDevice.respondToRegisterRequest({
|
||||||
errorCode: 4
|
errorCode: 4,
|
||||||
});
|
});
|
||||||
errorMessage = this.container.find("p");
|
const errorMessage = this.container.find('p');
|
||||||
return expect(errorMessage.text()).toContain("already been registered with us");
|
return expect(errorMessage.text()).toContain('already been registered with us');
|
||||||
});
|
});
|
||||||
it("displays an error message for other errors", function() {
|
|
||||||
var errorMessage, setupButton;
|
it('displays an error message for other errors', () => {
|
||||||
setupButton = this.container.find("#js-setup-u2f-device");
|
const setupButton = this.container.find('#js-setup-u2f-device');
|
||||||
setupButton.trigger('click');
|
setupButton.trigger('click');
|
||||||
this.u2fDevice.respondToRegisterRequest({
|
this.u2fDevice.respondToRegisterRequest({
|
||||||
errorCode: "error!"
|
errorCode: 'error!',
|
||||||
});
|
});
|
||||||
errorMessage = this.container.find("p");
|
const errorMessage = this.container.find('p');
|
||||||
return expect(errorMessage.text()).toContain("There was a problem communicating with your device");
|
return expect(errorMessage.text()).toContain('There was a problem communicating with your device');
|
||||||
});
|
});
|
||||||
return it("allows retrying registration after an error", function() {
|
|
||||||
var registeredMessage, retryButton, setupButton;
|
return it('allows retrying registration after an error', () => {
|
||||||
setupButton = this.container.find("#js-setup-u2f-device");
|
let setupButton = this.container.find('#js-setup-u2f-device');
|
||||||
setupButton.trigger('click');
|
setupButton.trigger('click');
|
||||||
this.u2fDevice.respondToRegisterRequest({
|
this.u2fDevice.respondToRegisterRequest({
|
||||||
errorCode: "error!"
|
errorCode: 'error!',
|
||||||
});
|
});
|
||||||
retryButton = this.container.find("#U2FTryAgain");
|
const retryButton = this.container.find('#U2FTryAgain');
|
||||||
retryButton.trigger('click');
|
retryButton.trigger('click');
|
||||||
setupButton = this.container.find("#js-setup-u2f-device");
|
setupButton = this.container.find('#js-setup-u2f-device');
|
||||||
setupButton.trigger('click');
|
setupButton.trigger('click');
|
||||||
this.u2fDevice.respondToRegisterRequest({
|
this.u2fDevice.respondToRegisterRequest({
|
||||||
deviceData: "this is data from the device"
|
deviceData: 'this is data from the device',
|
||||||
});
|
});
|
||||||
registeredMessage = this.container.find("p");
|
const registeredMessage = this.container.find('p');
|
||||||
return expect(registeredMessage.text()).toContain("Your device was successfully set up!");
|
return expect(registeredMessage.text()).toContain('Your device was successfully set up!');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}).call(window);
|
|
||||||
|
|
Loading…
Reference in a new issue