2
0

authentication.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const bcrypt = require('bcryptjs-then')
  2. /* global WIKI */
  3. // ------------------------------------
  4. // Local Account
  5. // ------------------------------------
  6. const LocalStrategy = require('passport-local').Strategy
  7. module.exports = {
  8. init (passport, conf) {
  9. passport.use('local',
  10. new LocalStrategy({
  11. usernameField: 'email',
  12. passwordField: 'password'
  13. }, async (uEmail, uPassword, done) => {
  14. try {
  15. const user = await WIKI.models.users.query().findOne({
  16. email: uEmail.toLowerCase(),
  17. providerKey: 'local'
  18. })
  19. if (user) {
  20. await user.verifyPassword(uPassword)
  21. if (!user.isActive) {
  22. done(new WIKI.Error.AuthAccountBanned(), null)
  23. } else if (!user.isVerified) {
  24. done(new WIKI.Error.AuthAccountNotVerified(), null)
  25. } else {
  26. done(null, user)
  27. }
  28. } else {
  29. // Fake verify password to mask timing differences
  30. await bcrypt.compare((Math.random() + 1).toString(36), '$2a$12$irXbAcQSY59pcQQfNQpY8uyhfSw48nzDikAmr60drI501nR.PuBx2')
  31. done(new WIKI.Error.AuthLoginFailed(), null)
  32. }
  33. } catch (err) {
  34. done(err, null)
  35. }
  36. })
  37. )
  38. }
  39. }