-
-
Notifications
You must be signed in to change notification settings - Fork 132
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 #49 from yeojz/feature/two-directional-windows
Allow forward and backward windows in TOTP and Authenticator
- Loading branch information
Showing
13 changed files
with
236 additions
and
69 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
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,19 @@ | ||
import { totpCheckWithWindow } from 'otplib-core'; | ||
import decodeKey from './decodeKey'; | ||
|
||
/** | ||
* Checks the provided OTP token against system generated token | ||
* Returns the delta (window) which token passes. | ||
* Returns null otherwise. | ||
* | ||
* @module otplib-authenticator/checkDelta | ||
* @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 {integer | null} | ||
*/ | ||
function checkDelta(token, secret, options) { | ||
return totpCheckWithWindow(token, decodeKey(secret), options); | ||
} | ||
|
||
export default checkDelta; |
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 |
---|---|---|
@@ -1,34 +1,52 @@ | ||
import totpCheck from './totpCheck'; | ||
|
||
function createChecker(token, secret, opt) { | ||
const delta = opt.step * 1000; | ||
const epoch = opt.epoch; | ||
|
||
return (direction, start, bounds) => { | ||
for (let i = start; i <= bounds; i++) { | ||
opt.epoch = epoch + direction * i * delta; | ||
|
||
if (totpCheck(token, secret, opt)) { | ||
return i === 0 ? 0 : direction * i; | ||
} | ||
} | ||
return null; | ||
}; | ||
} | ||
|
||
function getWindowBounds(opt) { | ||
const bounds = Array.isArray(opt.window) | ||
? opt.window | ||
: [parseInt(opt.window, 10), parseInt(opt.window, 10)]; | ||
|
||
if (!Number.isInteger(bounds[0]) || !Number.isInteger(bounds[1])) { | ||
throw new Error( | ||
'Expecting options.window to be an integer or an array of integers' | ||
); | ||
} | ||
|
||
return bounds; | ||
} | ||
|
||
/** | ||
* Checks the provided OTP token against system generated token | ||
* with support for checking previous x time-step windows | ||
* with support for checking previous or future x time-step windows | ||
* | ||
* @module otplib-core/totpCheckWithWindow | ||
* @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 {integer} - the number of windows back it was successful. -1 otherwise | ||
* @return {integer | null} - the number of windows back (eg: -1) or forward if it was successful. null otherwise | ||
*/ | ||
function totpCheckWithWindow(token, secret, options) { | ||
let opt = Object.assign({}, options); | ||
|
||
if (typeof opt.window !== 'number') { | ||
throw new Error('Expecting options.window to be a number'); | ||
} | ||
|
||
const decrement = opt.step * 1000; | ||
const epoch = opt.epoch; | ||
|
||
for (let i = 0; i <= opt.window; i++) { | ||
opt.epoch = epoch - i * decrement; | ||
|
||
if (totpCheck(token, secret, opt)) { | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
const bounds = getWindowBounds(opt); | ||
const checker = createChecker(token, secret, opt); | ||
const backward = checker(-1, 0, bounds[0]); | ||
return backward !== null ? backward : checker(1, 1, bounds[1]); | ||
} | ||
|
||
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
Oops, something went wrong.