2022-09-17 21:36:40 +00:00
|
|
|
const bcrypt = require('bcryptjs-then')
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
// Local Account
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
const LocalStrategy = require('passport-local').Strategy
|
|
|
|
|
2017-09-11 03:00:03 +00:00
|
|
|
module.exports = {
|
|
|
|
init (passport, conf) {
|
|
|
|
passport.use('local',
|
|
|
|
new LocalStrategy({
|
|
|
|
usernameField: 'email',
|
|
|
|
passwordField: 'password'
|
2019-02-22 22:05:18 +00:00
|
|
|
}, async (uEmail, uPassword, done) => {
|
|
|
|
try {
|
|
|
|
const user = await WIKI.models.users.query().findOne({
|
2020-09-09 23:59:46 +00:00
|
|
|
email: uEmail.toLowerCase(),
|
2019-02-22 22:05:18 +00:00
|
|
|
providerKey: 'local'
|
|
|
|
})
|
2017-09-11 03:00:03 +00:00
|
|
|
if (user) {
|
2019-02-22 22:05:18 +00:00
|
|
|
await user.verifyPassword(uPassword)
|
|
|
|
if (!user.isActive) {
|
|
|
|
done(new WIKI.Error.AuthAccountBanned(), null)
|
|
|
|
} else if (!user.isVerified) {
|
|
|
|
done(new WIKI.Error.AuthAccountNotVerified(), null)
|
|
|
|
} else {
|
|
|
|
done(null, user)
|
|
|
|
}
|
2017-09-11 03:00:03 +00:00
|
|
|
} else {
|
2022-09-17 21:36:40 +00:00
|
|
|
// Fake verify password to mask timing differences
|
|
|
|
await bcrypt.compare((Math.random() + 1).toString(36), '$2a$12$irXbAcQSY59pcQQfNQpY8uyhfSw48nzDikAmr60drI501nR.PuBx2')
|
|
|
|
|
2018-05-20 22:50:51 +00:00
|
|
|
done(new WIKI.Error.AuthLoginFailed(), null)
|
2017-09-11 03:00:03 +00:00
|
|
|
}
|
2019-02-22 22:05:18 +00:00
|
|
|
} catch (err) {
|
2017-09-11 03:00:03 +00:00
|
|
|
done(err, null)
|
2019-02-22 22:05:18 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2017-09-11 03:00:03 +00:00
|
|
|
}
|
2017-07-29 21:33:08 +00:00
|
|
|
}
|