Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to bypass is user does not have 2FA configured #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 56 additions & 45 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ function Strategy(options, verifyUsernameAndPassword, verifyTotpCode) {
verifyUsernameAndPassword = options;
options = {};
}

if (options) {
this._skipTotpVerification = options.skipTotpVerification || false;
this._skipTotpVerification = options.skipTotpVerification || false;
} else {
this._skipTotpVerification = false;
}

if (!verifyUsernameAndPassword) {
throw new TypeError('2FA TOTP Strategy required username and password verification callback');
}

if (!this._skipTotpVerification && !verifyTotpCode) {
throw new TypeError('2FA TOTP Strategy required TOTP code verification callback');
}

this._usernameField = options.usernameField || 'username';
this._passwordField = options.passwordField || 'password';
this._codeField = options.codeField || 'code';
this._window = options.window || 6;

passport.Strategy.call(this);

this.name = '2fa-totp';
this._verifyUsernameAndPassword = verifyUsernameAndPassword;
this._verifyTotpCode = verifyTotpCode;
Expand All @@ -45,38 +45,38 @@ Strategy.prototype.authenticate = function (req, options) {
var MISSING_CREDENTIALS = 'Missing credentials';
var AUTH_FAILED = 'Invalid username or password';
options = options || {};

var username = lookup(req.body, this._usernameField) || lookup(req.query, this._usernameField);
var password = lookup(req.body, this._passwordField) || lookup(req.query, this._passwordField);
var code = lookup(req.body, this._codeField) || lookup(req.query, this._codeField);

if (!username || !password) {
return this.fail(options.badRequestMessage || MISSING_CREDENTIALS);
}

var self = this;

var firstStepAuth = new Promise(function (resolve, reject) {
// 1st step: check username and password

var verify = function (error, user, info) {
if (error) {
return reject({
error: true,
message: error
});
}
}

if (!user) {
return reject({
error: false,
message: info
});
}

resolve(user);
};

try {
if (self._passReqToCallback) {
self._verifyUsernameAndPassword(req, username, password, verify);
Expand All @@ -86,44 +86,55 @@ Strategy.prototype.authenticate = function (req, options) {
} catch (err) {
reject(err);
}

});

firstStepAuth.then(function (user) {
// 2nd step: code verification using TOTP

if (self._skipTotpVerification) {
self.success(user);
}

var verify = function (error, secret, period) {
if (error) {
return self.error(error);
}

var isValid = totp.verify(code, secret, {
window: self._window,
time: period
});

if (isValid) {
self.success(user);
} else {
self.fail(options.badRequestMessage || AUTH_FAILED);
}

};

try {
if (self._passReqToCallback) {
self._verifyTotpCode(req, user, verify);
} else {
self._verifyTotpCode(user, verify);

if (!user.secret) {

self.success(user);

}

if (user.secret) {

var verify = function (error, secret, period) {

if (error) {
return self.error(error);
}

var isValid = totp.verify(code, secret, {
window: self._window,
time: period
});

if (isValid) {
self.success(user);
} else {
self.fail(options.badRequestMessage || AUTH_FAILED);
}

};

try {
if (self._passReqToCallback) {
self._verifyTotpCode(req, user, verify);
} else {
self._verifyTotpCode(user, verify);
}
} catch (err) {
self.error(err);
}
} catch (err) {
self.error(err);

}

}).catch(function (reason) {
// 1st step failed. Return an error message to the user.
if (reason.error) {
Expand Down