2018-05-28 18:46:55 +00:00
|
|
|
const Model = require('objection').Model
|
|
|
|
const autoload = require('auto-load')
|
|
|
|
const path = require('path')
|
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication model
|
|
|
|
*/
|
|
|
|
module.exports = class Authentication extends Model {
|
|
|
|
static get tableName() { return 'authentication' }
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
|
|
|
required: ['key', 'title', 'isEnabled', 'useForm'],
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
id: {type: 'integer'},
|
|
|
|
key: {type: 'string'},
|
|
|
|
title: {type: 'string'},
|
|
|
|
isEnabled: {type: 'boolean'},
|
|
|
|
useForm: {type: 'boolean'},
|
2018-06-25 06:44:40 +00:00
|
|
|
config: {type: 'object'},
|
|
|
|
selfRegistration: {type: 'boolean'},
|
|
|
|
domainWhitelist: {type: 'object'},
|
|
|
|
autoEnrollGroups: {type: 'object'}
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 06:44:40 +00:00
|
|
|
static async getStrategies() {
|
|
|
|
const strategies = await WIKI.db.authentication.query()
|
|
|
|
return strategies.map(str => ({
|
|
|
|
...str,
|
|
|
|
domainWhitelist: _.get(str.domainWhitelist, 'v', []),
|
|
|
|
autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
|
|
|
|
}))
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static async refreshStrategiesFromDisk() {
|
|
|
|
try {
|
|
|
|
const dbStrategies = await WIKI.db.authentication.query()
|
|
|
|
const diskStrategies = autoload(path.join(WIKI.SERVERPATH, 'modules/authentication'))
|
|
|
|
let newStrategies = []
|
|
|
|
_.forOwn(diskStrategies, (strategy, strategyKey) => {
|
|
|
|
if (!_.some(dbStrategies, ['key', strategy.key])) {
|
|
|
|
newStrategies.push({
|
|
|
|
key: strategy.key,
|
|
|
|
title: strategy.title,
|
|
|
|
isEnabled: false,
|
|
|
|
useForm: strategy.useForm,
|
2018-07-01 23:50:42 +00:00
|
|
|
config: _.transform(strategy.props, (result, value, key) => {
|
|
|
|
if (_.isPlainObject(value)) {
|
|
|
|
let cfgValue = {
|
|
|
|
type: typeof value.type(),
|
2018-07-08 05:12:43 +00:00
|
|
|
value: !_.isNil(value.default) ? value.default : new value() // eslint-disable-line new-cap
|
2018-07-01 23:50:42 +00:00
|
|
|
}
|
|
|
|
if (_.isArray(value.enum)) {
|
|
|
|
cfgValue.enum = value.enum
|
|
|
|
}
|
|
|
|
_.set(result, key, cfgValue)
|
|
|
|
} else {
|
|
|
|
_.set(result, key, {
|
|
|
|
type: typeof value(),
|
2018-07-08 05:12:43 +00:00
|
|
|
value: new value() // eslint-disable-line new-cap
|
2018-07-01 23:50:42 +00:00
|
|
|
})
|
|
|
|
}
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (newStrategies.length > 0) {
|
|
|
|
await WIKI.db.authentication.query().insert(newStrategies)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|