wikijs-fork/server/models/authentication.js

109 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-05-28 18:46:55 +00:00
const Model = require('objection').Model
const fs = require('fs-extra')
2018-05-28 18:46:55 +00:00
const path = require('path')
const _ = require('lodash')
const yaml = require('js-yaml')
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']
}
static async getStrategy(key) {
return WIKI.models.authentication.query().findOne({ key })
}
static async getStrategies(isEnabled) {
const strategies = await WIKI.models.authentication.query().where(_.isBoolean(isEnabled) ? { isEnabled } : {})
return _.sortBy(strategies.map(str => ({
...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() {
let trx
2018-05-28 18:46:55 +00:00
try {
const dbStrategies = await WIKI.models.authentication.query()
// -> 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-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,
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
}, {}),
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) {
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)
if (trx) {
trx.rollback()
}
2018-05-28 18:46:55 +00:00
}
}
}