wikijs-fork/server/models/authentication.js

132 lines
4.0 KiB
JavaScript
Raw Permalink 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',
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']
}
static async getStrategy(key) {
return WIKI.models.authentication.query().findOne({ key })
}
static async getStrategies() {
const strategies = await WIKI.models.authentication.query().orderBy('order')
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 getStrategiesForLegacyClient() {
const strategies = await WIKI.models.authentication.query().select('key', 'selfRegistration')
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 {
const dbStrategies = await WIKI.models.authentication.query()
// -> Fetch definitions from disk
const authDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
WIKI.data.authentication = []
for (let dir of authDirs) {
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)
})
}
for (const strategy of dbStrategies) {
let newProps = false
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
}
strategy.config = _.transform(strategyDef.props, (result, value, key) => {
if (!_.has(result, key)) {
_.set(result, key, value.default)
// we have some new properties added to an existing auth strategy to write to the database
newProps = true
}
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({
displayName: strategyDef.title
2018-08-04 21:27:55 +00:00
}).where('key', strategy.key)
2018-05-28 18:46:55 +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
}
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)
}
}
}