cleanup event listeners for u2f

This commit is contained in:
Kyle Spearrin 2017-07-26 00:20:30 -04:00
parent 21d4f9e193
commit 01ccd21ef3
2 changed files with 82 additions and 63 deletions

View File

@ -7,16 +7,19 @@
utilsService.initListSectionItemListeners($(document), angular); utilsService.initListSectionItemListeners($(document), angular);
var u2f = new U2f(function (data) { var u2f = new U2f(function (data) {
$timeout(function () {
$scope.login(data); $scope.login(data);
$scope.$apply(); });
}, function (error) { }, function (error) {
$timeout(function () {
toastr.error(error, i18nService.errorsOccurred); toastr.error(error, i18nService.errorsOccurred);
$scope.$apply(); });
}, function (info) { }, function (info) {
$timeout(function () {
if (info === 'ready') { if (info === 'ready') {
$scope.u2fReady = true; $scope.u2fReady = true;
} }
$scope.$apply(); });
}); });
var constants = constantsService; var constants = constantsService;
@ -62,7 +65,6 @@
$scope.loginPromise.then(function () { $scope.loginPromise.then(function () {
$analytics.eventTrack('Logged In From Two-step'); $analytics.eventTrack('Logged In From Two-step');
$state.go('tabs.vault', { animation: 'in-slide-left', syncOnLoad: true }); $state.go('tabs.vault', { animation: 'in-slide-left', syncOnLoad: true });
u2f = null;
}, function () { }, function () {
u2f.start(); u2f.start();
}); });
@ -87,8 +89,6 @@
}; };
$scope.anotherMethod = function () { $scope.anotherMethod = function () {
u2f.stop();
u2f = null;
$state.go('twoFactorMethods', { $state.go('twoFactorMethods', {
animation: 'in-slide-up', animation: 'in-slide-up',
email: email, email: email,
@ -99,13 +99,17 @@
}; };
$scope.back = function () { $scope.back = function () {
u2f.stop();
u2f = null;
$state.go('login', { $state.go('login', {
animation: 'out-slide-right' animation: 'out-slide-right'
}); });
}; };
$scope.$on('$destroy', function () {
u2f.stop();
u2f.cleanup();
u2f = null;
});
function getDefaultProvider(twoFactorProviders) { function getDefaultProvider(twoFactorProviders) {
var keys = Object.keys(twoFactorProviders); var keys = Object.keys(twoFactorProviders);
var providerType = null; var providerType = null;
@ -127,6 +131,7 @@
function init() { function init() {
u2f.stop(); u2f.stop();
u2f.cleanup();
$timeout(function () { $timeout(function () {
$('#code').focus(); $('#code').focus();

View File

@ -6,8 +6,11 @@
this.connectorLink = document.createElement('a'); this.connectorLink = document.createElement('a');
} }
(function () {
var thisU2f = null;
U2f.prototype.init = function (data) { U2f.prototype.init = function (data) {
var self = this; var self = thisU2f = this;
self.connectorLink.href = 'https://vault.bitwarden.com/u2f-connector.html' + self.connectorLink.href = 'https://vault.bitwarden.com/u2f-connector.html' +
'?data=' + this.base64Encode(JSON.stringify(data)) + '?data=' + this.base64Encode(JSON.stringify(data)) +
@ -17,25 +20,7 @@ U2f.prototype.init = function (data) {
self.iframe = document.getElementById('u2f_iframe'); self.iframe = document.getElementById('u2f_iframe');
self.iframe.src = self.connectorLink.href; self.iframe.src = self.connectorLink.href;
window.addEventListener('message', function (event) { window.addEventListener('message', parseMessage, false);
if (!self.validMessage(event)) {
self.error('Invalid message.');
return;
}
var parts = event.data.split('|');
if (parts[0] === 'success' && self.success) {
self.success(parts[1]);
}
else if (parts[0] === 'error' && self.error) {
self.error(parts[1]);
}
else if (parts[0] === 'info') {
if (self.info) {
self.info(parts[1]);
}
}
}, false);
}; };
U2f.prototype.validMessage = function (event) { U2f.prototype.validMessage = function (event) {
@ -68,3 +53,32 @@ U2f.prototype.base64Encode = function (str) {
return String.fromCharCode('0x' + p1); return String.fromCharCode('0x' + p1);
})); }));
}; };
U2f.prototype.cleanup = function () {
window.removeEventListener('message', parseMessage, false);
};
function parseMessage(event) {
if (!thisU2f) {
return;
}
if (!thisU2f.validMessage(event)) {
thisU2f.error('Invalid message.');
return;
}
var parts = event.data.split('|');
if (parts[0] === 'success' && thisU2f.success) {
thisU2f.success(parts[1]);
}
else if (parts[0] === 'error' && thisU2f.error) {
thisU2f.error(parts[1]);
}
else if (parts[0] === 'info') {
if (thisU2f.info) {
thisU2f.info(parts[1]);
}
}
};
})();