Merge pull request #17 from thedufer/master

Make `opt` optional, as the docs describe
This commit is contained in:
Guy Halford-Thompson 2014-10-13 17:13:12 -07:00
commit 266e0f3ece
2 changed files with 17 additions and 1 deletions

View File

@ -19,7 +19,8 @@ var hotp = {};
* *
*/ */
hotp.gen = function(key, opt) { hotp.gen = function(key, opt) {
var key = key || ''; key = key || '';
opt = opt || {};
var counter = opt.counter || 0; var counter = opt.counter || 0;
var p = 6; var p = 6;
@ -77,6 +78,7 @@ hotp.gen = function(key, opt) {
* *
*/ */
hotp.verify = function(token, key, opt) { hotp.verify = function(token, key, opt) {
opt = opt || {};
var window = opt.window || 50; var window = opt.window || 50;
var counter = opt.counter || 0; var counter = opt.counter || 0;
@ -115,6 +117,7 @@ var totp = {};
* *
*/ */
totp.gen = function(key, opt) { totp.gen = function(key, opt) {
opt = opt || {};
var time = opt.time || 30; var time = opt.time || 30;
var _t = new Date().getTime();; var _t = new Date().getTime();;
@ -165,6 +168,7 @@ totp.gen = function(key, opt) {
* *
*/ */
totp.verify = function(token, key, opt) { totp.verify = function(token, key, opt) {
opt = opt || {};
var time = opt.time || 30; var time = opt.time || 30;
var _t = new Date().getTime(); var _t = new Date().getTime();

View File

@ -50,6 +50,9 @@ exports.testHOTP = function() {
}; };
var HOTP = ['755224', '287082','359152', '969429', '338314', '254676', '287922', '162583', '399871', '520489']; var HOTP = ['755224', '287082','359152', '969429', '338314', '254676', '287922', '162583', '399871', '520489'];
// make sure we can not pass in opt
notp.hotp.verify('WILL NOT PASS', key);
// counterheck for failure // counterheck for failure
opt.counter = 0; opt.counter = 0;
assert.ok(!notp.hotp.verify('WILL NOT PASS', key, opt), 'Should not pass'); assert.ok(!notp.hotp.verify('WILL NOT PASS', key, opt), 'Should not pass');
@ -76,6 +79,9 @@ exports.testTOTtoken = function() {
window : 0, window : 0,
}; };
// make sure we can not pass in opt
notp.totp.verify(token, key);
// counterheck for failure // counterheck for failure
opt.time = 0; opt.time = 0;
var token = 'windowILLNOTtokenASS'; var token = 'windowILLNOTtokenASS';
@ -172,6 +178,9 @@ exports.hotp_gen = function() {
var HOTP = ['755224', '287082','359152', '969429', '338314', '254676', '287922', '162583', '399871', '520489']; var HOTP = ['755224', '287082','359152', '969429', '338314', '254676', '287922', '162583', '399871', '520489'];
// make sure we can not pass in opt
notp.hotp.gen(key);
// counterheck for passes // counterheck for passes
for(i=0;i<HOTP.length;i++) { for(i=0;i<HOTP.length;i++) {
opt.counter = i; opt.counter = i;
@ -186,6 +195,9 @@ exports.totp_gen = function() {
window : 0, window : 0,
}; };
// make sure we can not pass in opt
notp.totp.gen(key);
// counterheck for test vector at 59s // counterheck for test vector at 59s
opt._t = 59*1000; opt._t = 59*1000;
assert.equal(notp.totp.gen(key, opt), '287082', 'TOTtoken values should match'); assert.equal(notp.totp.gen(key, opt), '287082', 'TOTtoken values should match');