SHARE
TWEET

Untitled

a guest Apr 27th, 2018 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // /imports/startup/server/twoFactorAuthServer.js
  2.  
  3. import { Meteor } from 'meteor/meteor';
  4. import { Accounts } from 'meteor/accounts-base';
  5. import SimpleSchema from 'simpl-schema';
  6. import { authenticator } from 'otplib';
  7.  
  8. import { TwoFactorToken } from '/imports/api/methods/twoFactorAuth';
  9.  
  10. // Disable default login handler
  11. Accounts._loginHandlers = Accounts._loginHandlers.filter(h => h.name !== 'password');
  12.  
  13.  
  14. const handleError = () => {
  15.   throw new Meteor.Error(403, 'Something went wrong. Please check your credentials.');
  16. };
  17.  
  18. Accounts.registerLoginHandler('two-factor', (options) => {
  19.   if (!options.password) { return undefined; }
  20.  
  21.   new SimpleSchema({
  22.     user: new SimpleSchema({
  23.       email: SimpleSchema.RegEx.EmailWithTLD,
  24.     }),
  25.     password: new SimpleSchema({
  26.       digest: /[A-Fa-f0-9]{64}/,
  27.       algorithm: { type: String, allowedValues: ['sha-256'] },
  28.     }),
  29.     token: TwoFactorToken,
  30.   }).validate(options);
  31.  
  32.   const user = Accounts.findUserByEmail(options.user.email);
  33.   if (!user) handleError();
  34.  
  35.   if (!user.services || !user.services.password || !user.services.password.bcrypt) {
  36.     return handleError();
  37.   }
  38.  
  39.   if (Accounts._checkPassword(user, options.password).error) {
  40.     return handleError();
  41.   }
  42.  
  43.   if (user.twoFactorEnabled) {
  44.     if (typeof options.token !== 'number') {
  45.       throw new Meteor.Error('two-factor-required');
  46.     }
  47.     if (!authenticator.check(options.token, user.services.twoFactorSecret)) {
  48.       return handleError();
  49.     }
  50.   }
  51.  
  52.   return { userId: user._id };
  53. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top