debian-mirror-gitlab/app/assets/javascripts/u2f/register.js

95 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-09-10 17:25:29 +05:30
import _ from 'underscore';
2018-03-27 19:54:05 +05:30
import importU2FLibrary from './util';
2018-03-17 18:26:18 +05:30
import U2FError from './error';
2017-09-10 17:25:29 +05:30
2016-09-29 09:46:39 +05:30
// Register U2F (universal 2nd factor) devices for users to authenticate with.
//
// State Flow #1: setup -> in_progress -> registered -> POST to server
// State Flow #2: setup -> in_progress -> error -> setup
2018-03-17 18:26:18 +05:30
export default class U2FRegister {
constructor(container, u2fParams) {
2018-03-27 19:54:05 +05:30
this.u2fUtils = null;
2018-03-17 18:26:18 +05:30
this.container = container;
this.renderNotSupported = this.renderNotSupported.bind(this);
this.renderRegistered = this.renderRegistered.bind(this);
this.renderError = this.renderError.bind(this);
this.renderInProgress = this.renderInProgress.bind(this);
this.renderSetup = this.renderSetup.bind(this);
this.renderTemplate = this.renderTemplate.bind(this);
this.register = this.register.bind(this);
this.start = this.start.bind(this);
this.appId = u2fParams.app_id;
this.registerRequests = u2fParams.register_requests;
this.signRequests = u2fParams.sign_requests;
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
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',
2016-09-13 17:45:13 +05:30
};
2018-03-17 18:26:18 +05:30
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
start() {
2018-03-27 19:54:05 +05:30
return importU2FLibrary()
2018-12-13 13:39:08 +05:30
.then(utils => {
2018-03-27 19:54:05 +05:30
this.u2fUtils = utils;
this.renderSetup();
})
.catch(() => this.renderNotSupported());
2018-03-17 18:26:18 +05:30
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
register() {
2018-12-13 13:39:08 +05:30
return this.u2fUtils.register(
this.appId,
this.registerRequests,
this.signRequests,
response => {
2018-03-17 18:26:18 +05:30
if (response.errorCode) {
const error = new U2FError(response.errorCode, 'register');
2018-03-27 19:54:05 +05:30
return this.renderError(error);
2018-03-17 18:26:18 +05:30
}
2018-03-27 19:54:05 +05:30
return this.renderRegistered(JSON.stringify(response));
2018-12-13 13:39:08 +05:30
},
10,
);
2018-03-17 18:26:18 +05:30
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
renderTemplate(name, params) {
const templateString = $(this.templates[name]).html();
const template = _.template(templateString);
return this.container.html(template(params));
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
renderSetup() {
this.renderTemplate('setup');
return this.container.find('#js-setup-u2f-device').on('click', this.renderInProgress);
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
renderInProgress() {
this.renderTemplate('inProgress');
return this.register();
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
renderError(error) {
this.renderTemplate('error', {
error_message: error.message(),
error_code: error.errorCode,
});
return this.container.find('#js-u2f-try-again').on('click', this.renderSetup);
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
renderRegistered(deviceResponse) {
this.renderTemplate('registered');
// Prefer to do this instead of interpolating using Underscore templates
// because of JSON escaping issues.
return this.container.find('#js-device-response').val(deviceResponse);
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
renderNotSupported() {
return this.renderTemplate('notSupported');
}
}