Skip to content

Instantly share code, notes, and snippets.

@morloy
Last active August 9, 2018 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morloy/c75b87e3b5414ee272337d9f6093b63f to your computer and use it in GitHub Desktop.
Save morloy/c75b87e3b5414ee272337d9f6093b63f to your computer and use it in GitHub Desktop.
// /imports/api/methods/twoFactorAuth.js
import SimpleSchema from 'simpl-schema';
export const TwoFactorToken = {
type: SimpleSchema.Integer, min: 0, max: 999999, optional: true,
};
export const tokenIsValid = (token: string, secret: string) => {
if (!otplib.authenticator.check(token, secret)) {
throw new Meteor.Error('twoFactor.invalid-token');
}
};
export const enableTwoFactor = new ValidatedMethod({
name: 'twoFactor.enableTwoFactor',
validate: new SimpleSchema({
token: TwoFactorToken,
}).validator(),
run({ token }) {
userIsLoggedIn(this.userId);
if (this.isSimulation) return;
tokenIsValid(token, Meteor.user().services.twoFactorSecret);
Meteor.users.update(this.userId, { $set: { twoFactorEnabled: true } });
},
});
export const disableTwoFactor = new ValidatedMethod({
name: 'twoFactor.disableTwoFactor',
validate: null,
run() {
userIsLoggedIn(this.userId);
if (this.isSimulation) return;
Meteor.users.update(this.userId, { $unset: { twoFactorEnabled: 1, 'services.twoFactorSecret': 1 } });
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment