|
|
@ -4,87 +4,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
const _ = require('lodash')
|
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = (passport) => {
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
init(passport) {
|
|
|
|
// Serialization user methods
|
|
|
|
// Serialization user methods
|
|
|
|
|
|
|
|
|
|
|
|
passport.serializeUser(function (user, done) {
|
|
|
|
passport.serializeUser(function (user, done) {
|
|
|
|
done(null, user._id)
|
|
|
|
done(null, user._id)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
passport.deserializeUser(function (id, done) {
|
|
|
|
passport.deserializeUser(function (id, done) {
|
|
|
|
wiki.db.User.findById(id).then((user) => {
|
|
|
|
wiki.db.User.findById(id).then((user) => {
|
|
|
|
if (user) {
|
|
|
|
if (user) {
|
|
|
|
done(null, user)
|
|
|
|
done(null, user)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
done(new Error(wiki.lang.t('auth:errors:usernotfound')), null)
|
|
|
|
done(new Error(wiki.lang.t('auth:errors:usernotfound')), null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}).catch((err) => {
|
|
|
|
}).catch((err) => {
|
|
|
|
done(err, null)
|
|
|
|
done(err, null)
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Load authentication strategies
|
|
|
|
// Load authentication strategies
|
|
|
|
|
|
|
|
|
|
|
|
wiki.config.authStrategies = {
|
|
|
|
wiki.config.authStrategies = {
|
|
|
|
list: _.pickBy(wiki.config.auth, strategy => strategy.enabled),
|
|
|
|
list: _.pickBy(wiki.config.auth, strategy => strategy.enabled),
|
|
|
|
socialEnabled: (_.chain(wiki.config.auth).omit('local').filter(['enabled', true]).value().length > 0)
|
|
|
|
socialEnabled: (_.chain(wiki.config.auth).omit('local').filter(['enabled', true]).value().length > 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_.forOwn(wiki.config.authStrategies.list, (strategyConfig, strategyName) => {
|
|
|
|
_.forOwn(wiki.config.authStrategies.list, (strategyConfig, strategyName) => {
|
|
|
|
strategyConfig.callbackURL = `${wiki.config.site.host}/login/${strategyName}/callback`
|
|
|
|
strategyConfig.callbackURL = `${wiki.config.site.host}/login/${strategyName}/callback`
|
|
|
|
require(`../authentication/${strategyName}`)(passport, strategyConfig)
|
|
|
|
require(`../authentication/${strategyName}`)(passport, strategyConfig)
|
|
|
|
wiki.logger.info(`Authentication Provider ${_.upperFirst(strategyName)}: OK`)
|
|
|
|
wiki.logger.info(`Authentication Provider ${_.upperFirst(strategyName)}: OK`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// Create Guest account for first-time
|
|
|
|
// Create Guest account for first-time
|
|
|
|
|
|
|
|
|
|
|
|
return wiki.db.User.findOne({
|
|
|
|
return wiki.db.User.findOne({
|
|
|
|
where: {
|
|
|
|
where: {
|
|
|
|
provider: 'local',
|
|
|
|
|
|
|
|
email: 'guest@example.com'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}).then((c) => {
|
|
|
|
|
|
|
|
if (c < 1) {
|
|
|
|
|
|
|
|
return wiki.db.User.create({
|
|
|
|
|
|
|
|
provider: 'local',
|
|
|
|
provider: 'local',
|
|
|
|
email: 'guest@example.com',
|
|
|
|
email: 'guest@example.com'
|
|
|
|
name: 'Guest',
|
|
|
|
}
|
|
|
|
password: '',
|
|
|
|
}).then((c) => {
|
|
|
|
role: 'guest'
|
|
|
|
if (c < 1) {
|
|
|
|
}).then(() => {
|
|
|
|
return wiki.db.User.create({
|
|
|
|
wiki.logger.info('[AUTH] Guest account created successfully!')
|
|
|
|
provider: 'local',
|
|
|
|
return true
|
|
|
|
email: 'guest@example.com',
|
|
|
|
}).catch((err) => {
|
|
|
|
name: 'Guest',
|
|
|
|
wiki.logger.error('[AUTH] An error occured while creating guest account:')
|
|
|
|
password: '',
|
|
|
|
wiki.logger.error(err)
|
|
|
|
role: 'guest'
|
|
|
|
return err
|
|
|
|
}).then(() => {
|
|
|
|
})
|
|
|
|
wiki.logger.info('[AUTH] Guest account created successfully!')
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}).catch((err) => {
|
|
|
|
|
|
|
|
wiki.logger.error('[AUTH] An error occured while creating guest account:')
|
|
|
|
|
|
|
|
wiki.logger.error(err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// .then(() => {
|
|
|
|
// .then(() => {
|
|
|
|
// if (process.env.WIKI_JS_HEROKU) {
|
|
|
|
// if (process.env.WIKI_JS_HEROKU) {
|
|
|
|
// return wiki.db.User.findOne({ provider: 'local', email: process.env.WIKI_ADMIN_EMAIL }).then((c) => {
|
|
|
|
// return wiki.db.User.findOne({ provider: 'local', email: process.env.WIKI_ADMIN_EMAIL }).then((c) => {
|
|
|
|
// if (c < 1) {
|
|
|
|
// if (c < 1) {
|
|
|
|
// // Create root admin account (HEROKU ONLY)
|
|
|
|
// // Create root admin account (HEROKU ONLY)
|
|
|
|
|
|
|
|
|
|
|
|
// return wiki.db.User.create({
|
|
|
|
// return wiki.db.User.create({
|
|
|
|
// provider: 'local',
|
|
|
|
// provider: 'local',
|
|
|
|
// email: process.env.WIKI_ADMIN_EMAIL,
|
|
|
|
// email: process.env.WIKI_ADMIN_EMAIL,
|
|
|
|
// name: 'Administrator',
|
|
|
|
// name: 'Administrator',
|
|
|
|
// password: '$2a$04$MAHRw785Xe/Jd5kcKzr3D.VRZDeomFZu2lius4gGpZZ9cJw7B7Mna', // admin123 (default)
|
|
|
|
// password: '$2a$04$MAHRw785Xe/Jd5kcKzr3D.VRZDeomFZu2lius4gGpZZ9cJw7B7Mna', // admin123 (default)
|
|
|
|
// role: 'admin'
|
|
|
|
// role: 'admin'
|
|
|
|
// }).then(() => {
|
|
|
|
// }).then(() => {
|
|
|
|
// wiki.logger.info('[AUTH] Root admin account created successfully!')
|
|
|
|
// wiki.logger.info('[AUTH] Root admin account created successfully!')
|
|
|
|
// return true
|
|
|
|
// return true
|
|
|
|
// }).catch((err) => {
|
|
|
|
// }).catch((err) => {
|
|
|
|
// wiki.logger.error('[AUTH] An error occured while creating root admin account:')
|
|
|
|
// wiki.logger.error('[AUTH] An error occured while creating root admin account:')
|
|
|
|
// wiki.logger.error(err)
|
|
|
|
// wiki.logger.error(err)
|
|
|
|
// return err
|
|
|
|
// return err
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
// } else { return true }
|
|
|
|
// } else { return true }
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
// } else { return true }
|
|
|
|
// } else { return true }
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|