browser-authenticator.js/test.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-10-23 07:11:29 +00:00
// forgive the suckiness, but whatever
(function (exports) {
'use strict';
var key;
var Authenticator = exports.Authenticator;
var $ = function (x) {
return document.querySelector(x);
};
function generate(ke) {
Authenticator.generateKey().then(function (k) {
key = ke || k;
var companyName = $('.js-company-name').value;
var userAccount = $('.js-user-account').value;
2015-10-23 08:39:12 +00:00
var otpauth = 'otpauth://totp/'
+ encodeURI(companyName) + ':' + encodeURI(userAccount)
+ '?secret=' + key.replace(/\s+/g, '').toUpperCase()
;
2015-10-23 08:46:04 +00:00
/*
2015-10-23 08:34:39 +00:00
var otpauth = encodeURI('otpauth://totp/'
+ companyName + ':' + userAccount
+ '?secret=') + key.replace(/\s+/g, '').toUpperCase()
2015-10-23 07:58:19 +00:00
;
2015-10-23 08:39:12 +00:00
*/
// TODO figure out the right escaping
2015-10-23 08:46:04 +00:00
/*
2015-10-23 08:39:12 +00:00
var otpauth = 'otpauth://totp/'
+ companyName + ':' + userAccount
+ '?secret=' + key.replace(/\s+/g, '').toUpperCase()
;
2015-10-23 08:46:04 +00:00
*/
2015-10-23 08:39:12 +00:00
// obviously don't use this in production, but it's not so bad for the demo
2015-10-23 08:42:09 +00:00
var src = 'https://chart.googleapis.com/chart?chs=166x166&chld=L|0&cht=qr&chl=' + encodeURIComponent(otpauth);
2015-10-23 07:11:29 +00:00
2015-10-23 07:58:19 +00:00
$('.js-otpauth').innerHTML = otpauth; // safe to inject because I created it
2015-10-23 07:11:29 +00:00
$('.js-key').innerHTML = key; // safe to inject because I created it
$('img.js-qrcode').src = src;
Authenticator.generateToken(key).then(function (token) {
console.log('token', token);
Authenticator.verifyToken(key, token).then(function (result) {
console.log('verify', result);
Authenticator.verifyToken(key, '000 000').then(function (result) {
console.log('reject', result);
});
});
});
});
}
$('.js-verify').addEventListener('click', function () {
var token = $('.js-token').value;
Authenticator.verifyToken(key, token).then(function (result) {
var msg;
if (result) {
msg = 'Correct!';
} else {
msg = 'FAIL!';
}
2015-10-23 09:42:47 +00:00
console.info('verify', msg);
2015-10-23 07:11:29 +00:00
window.alert(msg);
2015-10-23 09:50:35 +00:00
}, function (err) {
console.error('ERROR');
console.error(err);
2015-10-23 07:11:29 +00:00
});
});
$('.js-generate').addEventListener('click', function () {
generate(null);
});
$('.js-company-name').value = 'ACME Co';
$('.js-user-account').value = 'john@example.com';
generate('hxdm vjec jjws rb3h wizr 4ifu gftm xboz');
}(
'undefined' !== typeof window ? window : module.exports
));