Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

fix(totp): add totp code window valid config #2371

Merged
merged 2 commits into from Mar 29, 2018
Merged

Conversation

vbudhram
Copy link
Contributor

@vbudhram vbudhram commented Mar 28, 2018

This PR adds a configurable window for TOTP codes to be valid, defaulting to 1.

Fixes mozilla/fxa-content-server#6005

@vbudhram vbudhram added this to the FxA-143: MFA milestone Mar 28, 2018
@vbudhram vbudhram self-assigned this Mar 28, 2018
@ghost ghost added the waffle:active label Mar 28, 2018
@vbudhram vbudhram changed the title fix(totp): add totp code window validation config fix(totp): add totp code window valid config Mar 28, 2018

it('should verify totp one outside one code window', () => {
const code = otplib.authenticator.generate()
return P.delay(4000)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking through previous remote tests, each consistently finishing in under 2 seconds, so having a 4 second delay here seemed like a safe choice for a code window.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note, this can also be a problem for humans, since we're much slower than machines ;-)
usually for a 30s TOTP window, the verifiers allow for 90s of actual window

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot wait for a test to be done in 30 secs, even 4 secs is very long time for this test. Can we avoid the 4 secs and test some other way?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate TOTPs that are 20s ahead and 20s behind window (to check the window is ~90s) and/or 31s ahead/31s behind (to check it fails if outside 90s window), then verify them - should be faster than the 4s delay

At least, that's what I would do - I don't know if it makes sense for you or not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this to remove the delay and check against future and previous code windows.

@vbudhram
Copy link
Contributor Author

Test failures fixed with mozilla/fxa-auth-db-mysql#330

@vbudhram
Copy link
Contributor Author

@mozilla/fxa-devs r?

Copy link
Contributor

@vladikoff vladikoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with nits

config/index.js Outdated
doc: 'Tokens in the previous x-windows that should be considered valid',
default: 1,
format: 'nat',
env: 'TOTP_CODE_WINDOw'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo TOTP_CODE_WINDOw

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we name this totpWindow ?

  1. env var is called ..._CODE_WINDOW
    (2. and also window is a browser global which is a bit trippy)

@@ -18,7 +18,8 @@ module.exports = (log, db, mailer, customs, config) => {
// Default options for TOTP
otplib.authenticator.options = {
encoding: 'hex',
step: config.step
step: config.step,
window: config.window
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the nit is meh above, I think you can also do

endoing: 'hex', 
step,
window

@vbudhram vbudhram force-pushed the fix-test-failures branch 3 times, most recently from d9908ff to ff3eec8 Compare March 29, 2018 16:47
@@ -64,7 +65,9 @@ module.exports = (log, db, mailer, customs, config) => {
.then(createResponse)
.then(() => reply(response), reply)

const secret = otplib.authenticator.generateSecret()
const authenticator = new otplib.authenticator.Authenticator()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related to code window size, but changed this to be consistent with how we use otplib.authenticator in other files and repos.

const P = require('../../../lib/promise')
const sinon = require('sinon')
const proxyquire = require('proxyquire').noPreserveCache()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benefit from https://github.com/mozilla/fxa-auth-server/pull/2371/files#r178127428, is that we no longer need proxyrequire.

@vbudhram vbudhram merged commit 110190d into master Mar 29, 2018
@ghost ghost removed the waffle:review label Mar 29, 2018
@vbudhram
Copy link
Contributor Author

Thank you! @vladikoff @gdestuynder

@shane-tomlinson shane-tomlinson deleted the fix-test-failures branch April 18, 2018 12:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants