2017-09-11 03:00:03 +00:00
|
|
|
const passport = require('passport')
|
2018-10-08 04:17:31 +00:00
|
|
|
const passportJWT = require('passport-jwt')
|
2017-09-11 03:00:03 +00:00
|
|
|
const fs = require('fs-extra')
|
2018-05-28 18:46:55 +00:00
|
|
|
const _ = require('lodash')
|
2017-09-11 03:00:03 +00:00
|
|
|
const path = require('path')
|
2018-08-04 21:27:55 +00:00
|
|
|
|
2018-10-08 04:17:31 +00:00
|
|
|
const securityHelper = require('../helpers/security')
|
2018-05-28 18:46:55 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
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-10-08 04:17:31 +00:00
|
|
|
WIKI.models.users.query().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)
|
|
|
|
})
|
2017-04-02 23:56:47 +00:00
|
|
|
})
|
|
|
|
|
2017-09-11 03:00:03 +00:00
|
|
|
return this
|
2018-05-28 18:46:55 +00:00
|
|
|
},
|
|
|
|
async activateStrategies() {
|
|
|
|
try {
|
|
|
|
// Unload any active strategies
|
2018-05-28 23:36:35 +00:00
|
|
|
WIKI.auth.strategies = {}
|
2018-05-28 18:46:55 +00:00
|
|
|
const currentStrategies = _.keys(passport._strategies)
|
|
|
|
_.pull(currentStrategies, 'session')
|
|
|
|
_.forEach(currentStrategies, stg => { passport.unuse(stg) })
|
|
|
|
|
2018-10-08 04:17:31 +00:00
|
|
|
// Load JWT
|
|
|
|
passport.use('jwt', new passportJWT.Strategy({
|
|
|
|
jwtFromRequest: securityHelper.extractJWT,
|
2018-12-03 02:42:43 +00:00
|
|
|
secretOrKey: WIKI.config.certs.public,
|
2018-10-08 04:17:31 +00:00
|
|
|
audience: 'urn:wiki.js', // TODO: use value from admin
|
|
|
|
issuer: 'urn:wiki.js'
|
|
|
|
}, (jwtPayload, cb) => {
|
|
|
|
cb(null, jwtPayload)
|
|
|
|
}))
|
|
|
|
|
2018-06-25 06:44:40 +00:00
|
|
|
// Load enabled strategies
|
2018-07-30 02:23:33 +00:00
|
|
|
const enabledStrategies = await WIKI.models.authentication.getStrategies()
|
2018-05-28 18:46:55 +00:00
|
|
|
for (let idx in enabledStrategies) {
|
|
|
|
const stg = enabledStrategies[idx]
|
2018-06-25 06:44:40 +00:00
|
|
|
if (!stg.isEnabled) { continue }
|
|
|
|
|
2018-07-08 15:16:45 +00:00
|
|
|
const strategy = require(`../modules/authentication/${stg.key}/authentication.js`)
|
2018-06-25 06:44:40 +00:00
|
|
|
|
2018-06-11 03:23:09 +00:00
|
|
|
stg.config.callbackURL = `${WIKI.config.host}/login/${stg.key}/callback` // TODO: config.host
|
2018-05-28 18:46:55 +00:00
|
|
|
strategy.init(passport, stg.config)
|
|
|
|
|
|
|
|
fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${strategy.key}.svg`), 'utf8').then(iconData => {
|
|
|
|
strategy.icon = iconData
|
|
|
|
}).catch(err => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
strategy.icon = '[missing icon]'
|
|
|
|
} else {
|
|
|
|
WIKI.logger.warn(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
WIKI.auth.strategies[stg.key] = strategy
|
2018-10-13 03:14:11 +00:00
|
|
|
WIKI.logger.info(`Authentication Strategy ${stg.key}: [ OK ]`)
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error(`Authentication Strategy: [ FAILED ]`)
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
}
|
2017-08-03 03:47:11 +00:00
|
|
|
}
|
2017-04-02 23:56:47 +00:00
|
|
|
}
|