2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2017-07-30 04:04:57 +00:00
|
|
|
const _ = require('lodash')
|
2017-09-11 03:00:03 +00:00
|
|
|
const passport = require('passport')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
const path = require('path')
|
2018-03-06 01:53:24 +00:00
|
|
|
const autoload = require('auto-load')
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2017-08-03 03:47:11 +00:00
|
|
|
module.exports = {
|
2017-09-11 03:00:03 +00:00
|
|
|
strategies: {},
|
|
|
|
init() {
|
|
|
|
this.passport = passport
|
|
|
|
|
|
|
|
// Serialization user methods
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2017-08-03 03:47:11 +00:00
|
|
|
passport.serializeUser(function (user, done) {
|
2018-01-10 01:41:53 +00:00
|
|
|
done(null, user.id)
|
2017-08-03 03:47:11 +00:00
|
|
|
})
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2017-08-03 03:47:11 +00:00
|
|
|
passport.deserializeUser(function (id, done) {
|
2018-05-19 20:40:07 +00:00
|
|
|
WIKI.db.users.query().findById(id).then((user) => {
|
2017-08-03 03:47:11 +00:00
|
|
|
if (user) {
|
|
|
|
done(null, user)
|
|
|
|
} else {
|
2018-03-05 20:49:36 +00:00
|
|
|
done(new Error(WIKI.lang.t('auth:errors:usernotfound')), null)
|
2017-08-03 03:47:11 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}).catch((err) => {
|
|
|
|
done(err, null)
|
|
|
|
})
|
2017-04-02 23:56:47 +00:00
|
|
|
})
|
|
|
|
|
2017-08-03 03:47:11 +00:00
|
|
|
// Load authentication strategies
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2018-03-06 01:53:24 +00:00
|
|
|
const modules = _.values(autoload(path.join(WIKI.SERVERPATH, 'modules/authentication')))
|
|
|
|
_.forEach(modules, (strategy) => {
|
2018-03-12 04:09:54 +00:00
|
|
|
const strategyConfig = _.get(WIKI.config.auth.strategies, strategy.key, { isEnabled: false })
|
2018-03-06 01:53:24 +00:00
|
|
|
strategyConfig.callbackURL = `${WIKI.config.site.host}${WIKI.config.site.path}login/${strategy.key}/callback`
|
2018-03-12 04:09:54 +00:00
|
|
|
strategy.config = strategyConfig
|
2018-03-06 01:53:24 +00:00
|
|
|
if (strategyConfig.isEnabled) {
|
|
|
|
try {
|
|
|
|
strategy.init(passport, strategyConfig)
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error(`Authentication Provider ${strategy.title}: [ FAILED ]`)
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
}
|
2017-12-30 07:00:49 +00:00
|
|
|
}
|
2018-03-06 01:53:24 +00:00
|
|
|
fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${strategy.key}.svg`), 'utf8').then(iconData => {
|
2017-09-11 03:00:03 +00:00
|
|
|
strategy.icon = iconData
|
|
|
|
}).catch(err => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
strategy.icon = '[missing icon]'
|
|
|
|
} else {
|
2018-03-06 01:53:24 +00:00
|
|
|
WIKI.logger.warn(err)
|
2017-09-11 03:00:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
this.strategies[strategy.key] = strategy
|
2018-03-06 01:53:24 +00:00
|
|
|
WIKI.logger.info(`Authentication Provider ${strategy.title}: [ OK ]`)
|
2017-08-03 03:47:11 +00:00
|
|
|
})
|
2017-07-29 21:33:08 +00:00
|
|
|
|
2017-09-11 03:00:03 +00:00
|
|
|
return this
|
2017-08-03 03:47:11 +00:00
|
|
|
}
|
2017-04-02 23:56:47 +00:00
|
|
|
}
|