browser-authenticator.js/demo/demo.js

109 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-11-03 08:24:16 +00:00
// This is crap demo code. Forgive me.
2015-10-23 07:11:29 +00:00
(function (exports) {
2015-10-25 01:50:00 +00:00
'use strict';
window.addEventListener("load", function () {
window.document.body.className += " in";
});
2015-10-23 07:11:29 +00:00
2015-10-26 20:48:33 +00:00
var defaultKey = 'hxdm vjec jjws rb3h wizr 4ifu gftm xboz';
2015-10-23 07:11:29 +00:00
var key;
var Authenticator = exports.Authenticator;
var $ = function (x) {
return document.querySelector(x);
};
function generate(ke) {
Authenticator.generateKey().then(function (k) {
2015-10-26 20:48:33 +00:00
var $keyEl = $('.js-key');
if (ke) {
key = ke;
}
else if ($keyEl.value) {
key = $keyEl.value;
$keyEl.placeholder = key;
$keyEl.value = '';
}
else {
key = k;
$keyEl.placeholder = key;
}
2015-10-23 07:11:29 +00:00
var companyName = $('.js-company-name').value;
var userAccount = $('.js-user-account').value;
var algo = $('.js-totp-algo').value || 'SHA1';
var digits = parseInt($('.js-totp-digits').value, 10) || 6;
var period = parseInt($('.js-totp-period').value, 10) || 30;
2015-10-23 07:11:29 +00:00
var otpauth = Authenticator.generateTotpUri(key, userAccount, companyName, algo, digits, period);
/*
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:39:12 +00:00
// obviously don't use this in production, but it's not so bad for the demo
2015-10-26 20:48:33 +00:00
// (hmm... no one has ever said those words and regretted them... TODO XXX generate QR locally)
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-26 20:48:33 +00:00
$('.js-otpauth').innerHTML = otpauth; // only safe to inject because I created it
2015-10-23 07:11:29 +00:00
$('img.js-qrcode').src = src;
2015-11-03 08:24:16 +00:00
$('.js-otp-iframe').src = 'demo/phone.html?otpuri=' + encodeURIComponent(otpauth);
2015-10-23 07:11:29 +00:00
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;
if (!/.*\d{3}.*\d{3}.*/.test(token)) {
window.alert("must have a 6 digit token");
return;
}
2015-10-23 07:11:29 +00:00
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) {
window.alert('[ERROR]:' + err.message);
window.alert('[ERROR]:' + err.stack);
2015-10-23 09:50:35 +00:00
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';
2015-10-26 20:48:33 +00:00
$('.js-key').placeholder = defaultKey;
generate(defaultKey);
2015-10-23 07:11:29 +00:00
}(
'undefined' !== typeof window ? window : module.exports
));