Last active
August 9, 2018 09:13
-
-
Save morloy/c75b87e3b5414ee272337d9f6093b63f to your computer and use it in GitHub Desktop.
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
// /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