1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 08:43:33 +00:00

u2f connector frame

This commit is contained in:
Kyle Spearrin
2017-06-26 14:49:20 -04:00
parent bc988181f9
commit 74d8e595f2
4 changed files with 134 additions and 2 deletions

View File

@@ -18,6 +18,12 @@
*/
var u2f = u2f || {};
/**
* Modification:
* Check if browser supports U2F API before this wrapper was added.
*/
u2f.isSupported = ((typeof u2f !== 'undefined') && u2f.register) || (chrome && chrome.runtime);
/**
* FIDO U2F Javascript API Version
* @number

96
src/js/u2f-connector.js Normal file
View File

@@ -0,0 +1,96 @@
document.addEventListener('DOMContentLoaded', function (event) {
init();
});
var parentUrl = null,
version = null;
function init() {
info('initing');
if (!u2f.isSupported) {
error('U2F is not supported in this browser.');
return;
}
var data = getQsParam('data');
if (!data) {
error('No data.');
return;
}
parentUrl = getQsParam('parent');
if (!parentUrl) {
error('No parent.');
return;
}
var versionQs = getQsParam('v');
if (!versionQs) {
error('No version.');
return;
}
try {
version = parseInt(versionQs);
var jsonString = b64Decode(data);
var json = JSON.parse(jsonString);
}
catch (e) {
error('Cannot parse data.');
return;
}
if (!json.appId || !json.challenge || !json.keys || !json.keys.length) {
error('Invalid data parameters.');
return;
}
initU2f(json);
info('ready');
}
function initU2f(obj) {
u2f.sign(obj.appId, obj.challenge, obj.keys, function (data) {
if (data.errorCode) {
if (data.errorCode === 5) {
initU2f(obj);
return;
}
error('U2F Error: ' + data.errorCode);
return;
}
success(data);
}, 5);
}
function error(message) {
parent.postMessage('error|' + message, parentUrl);
}
function success(data) {
var dataString = JSON.stringify(data);
parent.postMessage('success|' + dataString, parentUrl);
}
function info(message) {
parent.postMessage('info|' + message, parentUrl);
}
function getQsParam(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
function b64Decode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}

16
src/u2f-connector.html Normal file
View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>U2F Connector</title>
</head>
<body>
<!-- @if true !>
<script src="js/u2f.min.js?v=<!-- @echo cacheTag !>"></script>
<!-- @endif -->
<!-- @exclude -->
<script src="js/u2f-api.js"></script>
<script src="js/u2f-connector.js"></script>
<!-- @endexclude -->
</body>
</html>