2018-05-28 18:46:55 +00:00
|
|
|
const Model = require('objection').Model
|
2018-07-08 15:16:45 +00:00
|
|
|
const fs = require('fs-extra')
|
2018-05-28 18:46:55 +00:00
|
|
|
const path = require('path')
|
|
|
|
const _ = require('lodash')
|
2018-07-08 15:16:45 +00:00
|
|
|
const yaml = require('js-yaml')
|
2018-07-30 02:23:33 +00:00
|
|
|
const commonHelper = require('../helpers/common')
|
2018-05-28 18:46:55 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication model
|
|
|
|
*/
|
|
|
|
module.exports = class Authentication extends Model {
|
|
|
|
static get tableName() { return 'authentication' }
|
2018-09-08 19:49:36 +00:00
|
|
|
static get idColumn() { return 'key' }
|
2018-05-28 18:46:55 +00:00
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
2018-08-04 21:27:55 +00:00
|
|
|
required: ['key', 'isEnabled'],
|
2018-05-28 18:46:55 +00:00
|
|
|
|
|
|
|
properties: {
|
|
|
|
key: {type: 'string'},
|
|
|
|
isEnabled: {type: 'boolean'},
|
2019-02-10 00:10:34 +00:00
|
|
|
selfRegistration: {type: 'boolean'}
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 00:10:34 +00:00
|
|
|
static get jsonAttributes() {
|
|
|
|
return ['config', 'domainWhitelist', 'autoEnrollGroups']
|
|
|
|
}
|
|
|
|
|
2018-12-21 04:02:17 +00:00
|
|
|
static async getStrategy(key) {
|
|
|
|
return WIKI.models.authentication.query().findOne({ key })
|
|
|
|
}
|
|
|
|
|
2018-10-08 04:17:31 +00:00
|
|
|
static async getStrategies(isEnabled) {
|
|
|
|
const strategies = await WIKI.models.authentication.query().where(_.isBoolean(isEnabled) ? { isEnabled } : {})
|
|
|
|
return _.sortBy(strategies.map(str => ({
|
2018-06-25 06:44:40 +00:00
|
|
|
...str,
|
|
|
|
domainWhitelist: _.get(str.domainWhitelist, 'v', []),
|
|
|
|
autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
|
2018-12-24 06:03:10 +00:00
|
|
|
})), ['key'])
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static async refreshStrategiesFromDisk() {
|
2018-08-16 01:57:28 +00:00
|
|
|
let trx
|
2018-05-28 18:46:55 +00:00
|
|
|
try {
|
2018-07-30 02:23:33 +00:00
|
|
|
const dbStrategies = await WIKI.models.authentication.query()
|
2018-07-08 15:16:45 +00:00
|
|
|
|
|
|
|
// -> Fetch definitions from disk
|
|
|
|
const authDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
|
|
|
|
let diskStrategies = []
|
|
|
|
for (let dir of authDirs) {
|
|
|
|
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
|
|
|
|
diskStrategies.push(yaml.safeLoad(def))
|
|
|
|
}
|
2018-08-04 21:27:55 +00:00
|
|
|
WIKI.data.authentication = diskStrategies.map(strategy => ({
|
|
|
|
...strategy,
|
|
|
|
props: commonHelper.parseModuleProps(strategy.props)
|
|
|
|
}))
|
2018-07-08 15:16:45 +00:00
|
|
|
|
2018-05-28 18:46:55 +00:00
|
|
|
let newStrategies = []
|
2018-08-04 21:27:55 +00:00
|
|
|
for (let strategy of WIKI.data.authentication) {
|
2018-05-28 18:46:55 +00:00
|
|
|
if (!_.some(dbStrategies, ['key', strategy.key])) {
|
|
|
|
newStrategies.push({
|
|
|
|
key: strategy.key,
|
|
|
|
isEnabled: false,
|
2018-07-01 23:50:42 +00:00
|
|
|
config: _.transform(strategy.props, (result, value, key) => {
|
2018-08-04 21:27:55 +00:00
|
|
|
_.set(result, key, value.default)
|
2018-05-28 18:46:55 +00:00
|
|
|
return result
|
2018-06-25 06:44:40 +00:00
|
|
|
}, {}),
|
|
|
|
selfRegistration: false,
|
|
|
|
domainWhitelist: { v: [] },
|
|
|
|
autoEnrollGroups: { v: [] }
|
2018-05-28 18:46:55 +00:00
|
|
|
})
|
2018-08-04 21:27:55 +00:00
|
|
|
} else {
|
|
|
|
const strategyConfig = _.get(_.find(dbStrategies, ['key', strategy.key]), 'config', {})
|
|
|
|
await WIKI.models.authentication.query().patch({
|
|
|
|
config: _.transform(strategy.props, (result, value, key) => {
|
|
|
|
if (!_.has(result, key)) {
|
|
|
|
_.set(result, key, value.default)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}, strategyConfig)
|
|
|
|
}).where('key', strategy.key)
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
2018-08-04 21:27:55 +00:00
|
|
|
}
|
2018-05-28 18:46:55 +00:00
|
|
|
if (newStrategies.length > 0) {
|
2018-08-16 01:57:28 +00:00
|
|
|
trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
|
|
|
|
for (let strategy of newStrategies) {
|
|
|
|
await WIKI.models.authentication.query(trx).insert(strategy)
|
|
|
|
}
|
|
|
|
await trx.commit()
|
2018-05-28 18:46:55 +00:00
|
|
|
WIKI.logger.info(`Loaded ${newStrategies.length} new authentication strategies: [ OK ]`)
|
|
|
|
} else {
|
|
|
|
WIKI.logger.info(`No new authentication strategies found: [ SKIPPED ]`)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error(`Failed to scan or load new authentication providers: [ FAILED ]`)
|
|
|
|
WIKI.logger.error(err)
|
2018-08-16 01:57:28 +00:00
|
|
|
if (trx) {
|
|
|
|
trx.rollback()
|
|
|
|
}
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|