Skip to content

Commit

Permalink
Merge pull request #50 from mikepb/v2
Browse files Browse the repository at this point in the history
Fix totp.verifyDelta() returning incorrect delta
  • Loading branch information
markbao committed Jan 27, 2016
2 parents 8a922e6 + 9aa3a7a commit 06cae65
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ exports.hotp.verifyDelta = function hotpVerifyDelta (options) {
// parse token to integer
token = parseInt(token, 10);

// fail if token is NA
if (isNaN(token)) {
return;
}

// loop from C to C + W inclusive
for (i = counter; i <= counter + window; ++i) {
options.counter = i;
Expand Down Expand Up @@ -339,7 +344,14 @@ exports.totp.verifyDelta = function totpVerifyDelta (options) {
options.window += window;

// pass to hotp.verifyDelta
return exports.hotp.verifyDelta(options);
var delta = exports.hotp.verifyDelta(options);

// adjust for two-sided window
if (delta) {
delta.delta -= window;
}

return delta;
};

/**
Expand Down
26 changes: 26 additions & 0 deletions test/hotp_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,30 @@ describe('HOTP Counter-Based Algorithm Test', function () {
assert.equal(topic, '90693936');
});
});

describe('hotp.verifyDelta()', function () {
var secret = 'rNONHRni6BAk7y2TiKrv';
it('should get current TOTP value', function () {
this.token = speakeasy.totp({secret: secret, counter: 1});
assert.equal(this.token, '314097');
});
it('should get delta with varying window lengths', function () {
var delta;

delta = speakeasy.totp.verifyDelta({
secret: secret, token: '314097', counter: 1, window: 0
});
assert.isObject(delta); assert.strictEqual(delta.delta, 0);

delta = speakeasy.totp.verifyDelta({
secret: secret, token: '314097', counter: 1, window: 2
});
assert.isObject(delta); assert.strictEqual(delta.delta, 0);

delta = speakeasy.totp.verifyDelta({
secret: secret, token: '314097', counter: 1, window: 3
});
assert.isObject(delta); assert.strictEqual(delta.delta, 0);
});
});
});

0 comments on commit 06cae65

Please sign in to comment.