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

Feature: resend auth code #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Simple two factor authentication for accounts-password.
- [Client](https://github.com/dburles/meteor-two-factor#api-client)
- [getAuthCode](https://github.com/dburles/meteor-two-factor#getauthcode)
- [getNewAuthCode](https://github.com/dburles/meteor-two-factor#getnewauthcode)
- [resendAuthCode](https://github.com/dburles/meteor-two-factor#resendauthcode)
- [verifyAndLogin](https://github.com/dburles/meteor-two-factor#verifyandlogin)
- [isVerifying](https://github.com/dburles/meteor-two-factor#isverifying)
- [abort](https://github.com/dburles/meteor-two-factor#abort)
Expand Down Expand Up @@ -184,6 +185,16 @@ Generates a new authentication code. Only functional while verifying.

**callback** Optional callback. Called with no arguments on success, or with a single Error argument on failure.

### resendAuthCode

```
resendAuthCode([callback])
```

Sends generated authentication code one more time. Only functional while verifying.

**callback** Optional callback. Called with no arguments on success, or with a single Error argument on failure.

### verifyAndLogin

```
Expand Down
9 changes: 9 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ const getNewAuthCode = cb => {
Meteor.call('twoFactor.getAuthenticationCode', selector, password, callback);
};

const resendAuthCode = cb => {
const selector = getSelector(state.get('user'));
const password = state.get('password');
const callback = callbackHandler(cb);

Meteor.call('twoFactor.resendAuthenticationCode', selector, password, callback);
};

const verifyAndLogin = (code, cb) => {
const selector = getSelector(state.get('user'));

Expand Down Expand Up @@ -95,6 +103,7 @@ const abort = cb => {

twoFactor.getAuthCode = getAuthCode;
twoFactor.getNewAuthCode = getNewAuthCode;
twoFactor.resendAuthCode = resendAuthCode;
twoFactor.verifyAndLogin = verifyAndLogin;
twoFactor.isVerifying = isVerifying;
twoFactor.abort = abort;
65 changes: 35 additions & 30 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,26 @@ const getFieldName = () => {
return twoFactor.options.fieldName || 'twoFactorCode';
};

const verifyUser = ({userQuery, password}) => {
const user = Accounts._findUserByQuery(userQuery);
if (!user) {
throw invalidLogin();
}

const checkPassword = Accounts._checkPassword(user, password);
if (checkPassword.error) {
throw invalidLogin();
}

return user;
};

Meteor.methods({
'twoFactor.getAuthenticationCode'(userQuery, password) {
check(userQuery, userQueryValidator);
check(password, passwordValidator);

const fieldName = getFieldName();

const user = Accounts._findUserByQuery(userQuery);
if (!user) {
throw invalidLogin();
}

const checkPassword = Accounts._checkPassword(user, password);
if (checkPassword.error) {
throw invalidLogin();
}
const user = verifyUser({userQuery, password});

const code =
typeof twoFactor.generateCode === 'function'
Expand All @@ -63,12 +67,27 @@ Meteor.methods({
twoFactor.sendCode(user, code);
}

const fieldName = getFieldName();

Meteor.users.update(user._id, {
$set: {
[fieldName]: code,
},
});
},
'twoFactor.resendAuthenticationCode'(userQuery, password) {
check(userQuery, userQueryValidator);
check(password, passwordValidator);

const user = verifyUser({userQuery, password});

const fieldName = getFieldName();
const code = user[fieldName];

if (typeof twoFactor.sendCode === 'function') {
twoFactor.sendCode(user, code);
}
},
'twoFactor.verifyCodeAndLogin'(options) {
check(options, {
user: userQueryValidator,
Expand All @@ -78,15 +97,10 @@ Meteor.methods({

const fieldName = getFieldName();

const user = Accounts._findUserByQuery(options.user);
if (!user) {
throw invalidLogin();
}

const checkPassword = Accounts._checkPassword(user, options.password);
if (checkPassword.error) {
throw invalidLogin();
}
const user = verifyUser({
userQuery: options.user,
password: options.password
});

if (options.code !== user[fieldName]) {
throw new Meteor.Error(403, 'Invalid code');
Expand All @@ -107,18 +121,9 @@ Meteor.methods({
check(userQuery, userQueryValidator);
check(password, passwordValidator);

const user = verifyUser({userQuery, password})
const fieldName = getFieldName();

const user = Accounts._findUserByQuery(userQuery);
if (!user) {
throw invalidLogin();
}

const checkPassword = Accounts._checkPassword(user, password);
if (checkPassword.error) {
throw invalidLogin();
}

Meteor.users.update(user._id, {
$unset: {
[fieldName]: '',
Expand Down