-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from yeojz/feature/time-window
FEAT: added time window function
- Loading branch information
Showing
6 changed files
with
179 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import totpCheck from './totpCheck'; | ||
|
||
function getPrevWindowOption(options, windowCount) { | ||
return Object.assign(options, { | ||
epoch: options.epoch - (options.step * windowCount) | ||
}); | ||
} | ||
|
||
/** | ||
* Checks the provided OTP token against system generated token | ||
* with support for checking previous x time-step windows | ||
* | ||
* @module otplib-core/totpCheck | ||
* @param {string} token - the OTP token to check | ||
* @param {string} secret - your secret that is used to generate the token | ||
* @param {object} options - options which was used to generate it originally | ||
* @return {boolean} | ||
*/ | ||
function totpCheckWithWindow(token, secret, options) { | ||
let opt = Object.assign({}, options); | ||
const rounds = Math.floor(opt.window || 0) + 1; | ||
|
||
for (let i = 0; i < rounds; i++) { | ||
opt = getPrevWindowOption(opt, i); | ||
|
||
if (totpCheck(token, secret, opt)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default totpCheckWithWindow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import crypto from 'crypto'; | ||
import totpCheckWithWindow from './totpCheckWithWindow'; | ||
import totpCheck from './totpCheck'; | ||
|
||
jest.mock('./totpCheck', () => jest.fn()); | ||
const {default: totpCheckOriginal} = require.requireActual('./totpCheck'); | ||
|
||
describe('totpCheck', function() { | ||
|
||
const secret = 'i6im0gc96j0mn00c'; | ||
|
||
const timeToken = [ | ||
{ | ||
time: 1519050965, | ||
token: 737086 | ||
}, | ||
{ | ||
time: 1519050992, | ||
token: 555283 | ||
}, | ||
{ | ||
time: 1519051024, | ||
token: 712564 | ||
} | ||
]; | ||
|
||
function token(n) { | ||
return timeToken[n].token; | ||
} | ||
|
||
function time(n) { | ||
return timeToken[n].time; | ||
} | ||
|
||
it('should call totpCheck 1 time when window is 0', function() { | ||
totpCheck.mockImplementation(() => false); | ||
|
||
totpCheckWithWindow(token(0), secret, { | ||
epoch: time(0), | ||
step: 30, | ||
window: 0 | ||
}); | ||
|
||
expect(totpCheck).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should call totpCheck 2 times when window is 1', function() { | ||
totpCheck.mockImplementation(() => false); | ||
|
||
totpCheckWithWindow('', secret, { | ||
epoch: time(1), | ||
step: 30, | ||
window: 1 | ||
}); | ||
|
||
expect(totpCheck).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('current 3, window 1, token 0, called 2, return false', function() { | ||
totpCheck.mockImplementation((...args) => { | ||
return totpCheckOriginal(...args); | ||
}) | ||
|
||
const opt = { | ||
crypto, | ||
epoch: time(2), | ||
step: 30, | ||
window: 1 | ||
}; | ||
|
||
const result = totpCheckWithWindow(token(0), secret, opt); | ||
|
||
expect(result).toBe(false); | ||
expect(totpCheck).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('current 2, window 1, token 1, called 2, return true', function() { | ||
totpCheck.mockImplementation((...args) => { | ||
return totpCheckOriginal(...args); | ||
}) | ||
|
||
const opt = { | ||
crypto, | ||
epoch: time(1), | ||
step: 30, | ||
window: 1 | ||
}; | ||
|
||
const result = totpCheckWithWindow(token(0), secret, opt); | ||
|
||
expect(result).toBe(true); | ||
expect(totpCheck).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('current 3, window 2, token 1, called 2, return true', function() { | ||
totpCheck.mockImplementation((...args) => { | ||
return totpCheckOriginal(...args); | ||
}) | ||
|
||
const opt = { | ||
crypto, | ||
epoch: time(2), | ||
step: 30, | ||
window: 2 | ||
}; | ||
|
||
const result = totpCheckWithWindow(token(1), secret, opt); | ||
|
||
expect(result).toBe(true); | ||
expect(totpCheck).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters