mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.3 KiB
110 lines
3.3 KiB
7 years ago
|
/* global wiki */
|
||
8 years ago
|
|
||
7 years ago
|
const _ = require('lodash')
|
||
7 years ago
|
const passport = require('passport')
|
||
|
const fs = require('fs-extra')
|
||
|
const path = require('path')
|
||
8 years ago
|
|
||
7 years ago
|
module.exports = {
|
||
7 years ago
|
strategies: {},
|
||
|
init() {
|
||
|
this.passport = passport
|
||
|
|
||
|
// Serialization user methods
|
||
8 years ago
|
|
||
7 years ago
|
passport.serializeUser(function (user, done) {
|
||
7 years ago
|
done(null, user.id)
|
||
7 years ago
|
})
|
||
8 years ago
|
|
||
7 years ago
|
passport.deserializeUser(function (id, done) {
|
||
|
wiki.db.User.findById(id).then((user) => {
|
||
|
if (user) {
|
||
|
done(null, user)
|
||
|
} else {
|
||
|
done(new Error(wiki.lang.t('auth:errors:usernotfound')), null)
|
||
|
}
|
||
|
return true
|
||
|
}).catch((err) => {
|
||
|
done(err, null)
|
||
|
})
|
||
8 years ago
|
})
|
||
|
|
||
7 years ago
|
// Load authentication strategies
|
||
8 years ago
|
|
||
7 years ago
|
_.forOwn(_.omitBy(wiki.config.auth.strategies, s => s.enabled === false), (strategyConfig, strategyKey) => {
|
||
7 years ago
|
strategyConfig.callbackURL = `${wiki.config.site.host}${wiki.config.site.path}login/${strategyKey}/callback`
|
||
7 years ago
|
let strategy = require(`../modules/authentication/${strategyKey}`)
|
||
7 years ago
|
try {
|
||
|
strategy.init(passport, strategyConfig)
|
||
|
} catch (err) {
|
||
|
wiki.logger.error(`Authentication Provider ${strategyKey}: [ FAILED ]`)
|
||
|
wiki.logger.error(err)
|
||
|
}
|
||
7 years ago
|
fs.readFile(path.join(wiki.ROOTPATH, `assets/svg/auth-icon-${strategyKey}.svg`), 'utf8').then(iconData => {
|
||
|
strategy.icon = iconData
|
||
|
}).catch(err => {
|
||
|
if (err.code === 'ENOENT') {
|
||
|
strategy.icon = '[missing icon]'
|
||
|
} else {
|
||
|
wiki.logger.error(err)
|
||
|
}
|
||
|
})
|
||
|
this.strategies[strategy.key] = strategy
|
||
7 years ago
|
wiki.logger.info(`Authentication Provider ${strategyKey}: [ OK ]`)
|
||
7 years ago
|
})
|
||
7 years ago
|
|
||
7 years ago
|
// Create Guest account for first-time
|
||
7 years ago
|
|
||
7 years ago
|
wiki.db.User.findOne({
|
||
7 years ago
|
where: {
|
||
7 years ago
|
provider: 'local',
|
||
7 years ago
|
email: 'guest@example.com'
|
||
|
}
|
||
|
}).then((c) => {
|
||
|
if (c < 1) {
|
||
|
return wiki.db.User.create({
|
||
|
provider: 'local',
|
||
|
email: 'guest@example.com',
|
||
|
name: 'Guest',
|
||
|
password: '',
|
||
|
role: 'guest'
|
||
|
}).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
|
||
|
})
|
||
|
}
|
||
|
})
|
||
7 years ago
|
|
||
7 years ago
|
// .then(() => {
|
||
|
// if (process.env.WIKI_JS_HEROKU) {
|
||
|
// return wiki.db.User.findOne({ provider: 'local', email: process.env.WIKI_ADMIN_EMAIL }).then((c) => {
|
||
|
// if (c < 1) {
|
||
|
// // Create root admin account (HEROKU ONLY)
|
||
7 years ago
|
|
||
7 years ago
|
// return wiki.db.User.create({
|
||
|
// provider: 'local',
|
||
|
// email: process.env.WIKI_ADMIN_EMAIL,
|
||
|
// name: 'Administrator',
|
||
|
// password: '$2a$04$MAHRw785Xe/Jd5kcKzr3D.VRZDeomFZu2lius4gGpZZ9cJw7B7Mna', // admin123 (default)
|
||
|
// role: 'admin'
|
||
|
// }).then(() => {
|
||
|
// wiki.logger.info('[AUTH] Root admin account created successfully!')
|
||
|
// return true
|
||
|
// }).catch((err) => {
|
||
|
// wiki.logger.error('[AUTH] An error occured while creating root admin account:')
|
||
|
// wiki.logger.error(err)
|
||
|
// return err
|
||
|
// })
|
||
|
// } else { return true }
|
||
|
// })
|
||
|
// } else { return true }
|
||
|
// })
|
||
7 years ago
|
|
||
|
return this
|
||
7 years ago
|
}
|
||
8 years ago
|
}
|