1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +01:00

u2f connector updates

This commit is contained in:
Kyle Spearrin 2017-06-26 23:52:49 -04:00
parent 5d87fae906
commit 0ee928cdce

View File

@ -3,9 +3,20 @@
}); });
var parentUrl = null, var parentUrl = null,
version = null; parentOrigin = null,
version = null,
stop = false,
sentSuccess = false;
function init() { function init() {
start();
onMessage();
info('ready');
}
function start() {
sentSuccess = false;
if (!u2f.isSupported) { if (!u2f.isSupported) {
error('U2F is not supported in this browser.'); error('U2F is not supported in this browser.');
return; return;
@ -22,6 +33,11 @@ function init() {
error('No parent.'); error('No parent.');
return; return;
} }
else {
var link = document.createElement('a');
link.href = parentUrl;
parentOrigin = link.origin;
}
var versionQs = getQsParam('v'); var versionQs = getQsParam('v');
if (!versionQs) { if (!versionQs) {
@ -44,11 +60,15 @@ function init() {
return; return;
} }
stop = false
initU2f(json); initU2f(json);
info('ready');
} }
function initU2f(obj) { function initU2f(obj) {
if (stop) {
return;
}
u2f.sign(obj.appId, obj.challenge, obj.keys, function (data) { u2f.sign(obj.appId, obj.challenge, obj.keys, function (data) {
if (data.errorCode) { if (data.errorCode) {
if (data.errorCode === 5) { if (data.errorCode === 5) {
@ -64,13 +84,33 @@ function initU2f(obj) {
}, 5); }, 5);
} }
function onMessage() {
window.addEventListener('message', function (event) {
if (!event.origin || event.origin === '' || event.origin !== parentOrigin) {
return;
}
if (event.data === 'stop') {
stop = true;
}
else if (event.data === 'start' && stop) {
start();
}
}, false);
}
function error(message) { function error(message) {
parent.postMessage('error|' + message, parentUrl); parent.postMessage('error|' + message, parentUrl);
} }
function success(data) { function success(data) {
if (sentSuccess) {
return;
}
var dataString = JSON.stringify(data); var dataString = JSON.stringify(data);
parent.postMessage('success|' + dataString, parentUrl); parent.postMessage('success|' + dataString, parentUrl);
sentSuccess = true;
} }
function info(message) { function info(message) {