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',
|
2020-07-03 23:36:33 +00:00
|
|
|
required: ['key'],
|
2018-05-28 18:46:55 +00:00
|
|
|
|
|
|
|
properties: {
|
|
|
|
key: {type: 'string'},
|
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 })
|
|
|
|
}
|
|
|
|
|
2020-07-03 23:36:33 +00:00
|
|
|
static async getStrategies() {
|
|
|
|
const strategies = await WIKI.models.authentication.query().orderBy('order')
|
2020-07-05 05:36:02 +00:00
|
|
|
return strategies.map(str => ({
|
2018-06-25 06:44:40 +00:00
|
|
|
...str,
|
|
|
|
domainWhitelist: _.get(str.domainWhitelist, 'v', []),
|
|
|
|
autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
|
2020-07-05 05:36:02 +00:00
|
|
|
}))
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 20:18:43 +00:00
|
|
|
static async getStrategiesForLegacyClient() {
|
2020-07-03 23:36:33 +00:00
|
|
|
const strategies = await WIKI.models.authentication.query().select('key', 'selfRegistration')
|
2019-07-13 20:18:43 +00:00
|
|
|
let formStrategies = []
|
|
|
|
let socialStrategies = []
|
|
|
|
|
|
|
|
for (let stg of strategies) {
|
|
|
|
const stgInfo = _.find(WIKI.data.authentication, ['key', stg.key]) || {}
|
|
|
|
if (stgInfo.useForm) {
|
|
|
|
formStrategies.push({
|
|
|
|
key: stg.key,
|
|
|
|
title: stgInfo.title
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
socialStrategies.push({
|
|
|
|
...stgInfo,
|
|
|
|
...stg,
|
|
|
|
icon: await fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${stg.key}.svg`), 'utf8').catch(err => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
formStrategies,
|
|
|
|
socialStrategies
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:46:55 +00:00
|
|
|
static async refreshStrategiesFromDisk() {
|
|
|
|
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'))
|
2020-07-03 23:36:33 +00:00
|
|
|
WIKI.data.authentication = []
|
2018-07-08 15:16:45 +00:00
|
|
|
for (let dir of authDirs) {
|
2020-07-03 23:36:33 +00:00
|
|
|
const defRaw = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
|
|
|
|
const def = yaml.safeLoad(defRaw)
|
|
|
|
WIKI.data.authentication.push({
|
|
|
|
...def,
|
|
|
|
props: commonHelper.parseModuleProps(def.props)
|
|
|
|
})
|
2018-07-08 15:16:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 23:36:33 +00:00
|
|
|
for (const strategy of dbStrategies) {
|
2023-03-17 21:27:31 +00:00
|
|
|
let newProps = false
|
2020-07-03 23:36:33 +00:00
|
|
|
const strategyDef = _.find(WIKI.data.authentication, ['key', strategy.strategyKey])
|
2020-08-30 20:33:52 +00:00
|
|
|
if (!strategyDef) {
|
|
|
|
await WIKI.models.authentication.query().delete().where('key', strategy.key)
|
|
|
|
WIKI.logger.info(`Authentication strategy ${strategy.strategyKey} was removed from disk: [ REMOVED ]`)
|
|
|
|
continue
|
|
|
|
}
|
2020-07-03 23:36:33 +00:00
|
|
|
strategy.config = _.transform(strategyDef.props, (result, value, key) => {
|
|
|
|
if (!_.has(result, key)) {
|
|
|
|
_.set(result, key, value.default)
|
2023-03-17 21:27:31 +00:00
|
|
|
// we have some new properties added to an existing auth strategy to write to the database
|
|
|
|
newProps = true
|
2020-07-03 23:36:33 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}, strategy.config)
|
|
|
|
|
|
|
|
// Fix pre-2.5 strategies displayName
|
|
|
|
if (!strategy.displayName) {
|
2018-08-04 21:27:55 +00:00
|
|
|
await WIKI.models.authentication.query().patch({
|
2020-07-03 23:36:33 +00:00
|
|
|
displayName: strategyDef.title
|
2018-08-04 21:27:55 +00:00
|
|
|
}).where('key', strategy.key)
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
2023-03-17 21:27:31 +00:00
|
|
|
// write existing auth model to database with new properties and defaults
|
|
|
|
if (newProps) {
|
|
|
|
await WIKI.models.authentication.query().patch({
|
|
|
|
config: strategy.config
|
|
|
|
}).where('key', strategy.key)
|
|
|
|
}
|
2018-08-04 21:27:55 +00:00
|
|
|
}
|
2020-07-03 23:36:33 +00:00
|
|
|
|
|
|
|
WIKI.logger.info(`Loaded ${WIKI.data.authentication.length} authentication strategies: [ OK ]`)
|
2018-05-28 18:46:55 +00:00
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error(`Failed to scan or load new authentication providers: [ FAILED ]`)
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|