You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.5 KiB

  1. const Model = require('objection').Model
  2. const fs = require('fs-extra')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. const yaml = require('js-yaml')
  6. const commonHelper = require('../helpers/common')
  7. /* global WIKI */
  8. /**
  9. * Authentication model
  10. */
  11. module.exports = class Authentication extends Model {
  12. static get tableName() { return 'authentication' }
  13. static get idColumn() { return 'key' }
  14. static get jsonSchema () {
  15. return {
  16. type: 'object',
  17. required: ['key', 'isEnabled'],
  18. properties: {
  19. key: {type: 'string'},
  20. isEnabled: {type: 'boolean'},
  21. selfRegistration: {type: 'boolean'}
  22. }
  23. }
  24. }
  25. static get jsonAttributes() {
  26. return ['config', 'domainWhitelist', 'autoEnrollGroups']
  27. }
  28. static async getStrategy(key) {
  29. return WIKI.models.authentication.query().findOne({ key })
  30. }
  31. static async getStrategies(isEnabled) {
  32. const strategies = await WIKI.models.authentication.query().where(_.isBoolean(isEnabled) ? { isEnabled } : {})
  33. return _.sortBy(strategies.map(str => ({
  34. ...str,
  35. domainWhitelist: _.get(str.domainWhitelist, 'v', []),
  36. autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
  37. })), ['key'])
  38. }
  39. static async refreshStrategiesFromDisk() {
  40. let trx
  41. try {
  42. const dbStrategies = await WIKI.models.authentication.query()
  43. // -> Fetch definitions from disk
  44. const authDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
  45. let diskStrategies = []
  46. for (let dir of authDirs) {
  47. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
  48. diskStrategies.push(yaml.safeLoad(def))
  49. }
  50. WIKI.data.authentication = diskStrategies.map(strategy => ({
  51. ...strategy,
  52. props: commonHelper.parseModuleProps(strategy.props)
  53. }))
  54. let newStrategies = []
  55. for (let strategy of WIKI.data.authentication) {
  56. if (!_.some(dbStrategies, ['key', strategy.key])) {
  57. newStrategies.push({
  58. key: strategy.key,
  59. isEnabled: false,
  60. config: _.transform(strategy.props, (result, value, key) => {
  61. _.set(result, key, value.default)
  62. return result
  63. }, {}),
  64. selfRegistration: false,
  65. domainWhitelist: { v: [] },
  66. autoEnrollGroups: { v: [] }
  67. })
  68. } else {
  69. const strategyConfig = _.get(_.find(dbStrategies, ['key', strategy.key]), 'config', {})
  70. await WIKI.models.authentication.query().patch({
  71. config: _.transform(strategy.props, (result, value, key) => {
  72. if (!_.has(result, key)) {
  73. _.set(result, key, value.default)
  74. }
  75. return result
  76. }, strategyConfig)
  77. }).where('key', strategy.key)
  78. }
  79. }
  80. if (newStrategies.length > 0) {
  81. trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
  82. for (let strategy of newStrategies) {
  83. await WIKI.models.authentication.query(trx).insert(strategy)
  84. }
  85. await trx.commit()
  86. WIKI.logger.info(`Loaded ${newStrategies.length} new authentication strategies: [ OK ]`)
  87. } else {
  88. WIKI.logger.info(`No new authentication strategies found: [ SKIPPED ]`)
  89. }
  90. } catch (err) {
  91. WIKI.logger.error(`Failed to scan or load new authentication providers: [ FAILED ]`)
  92. WIKI.logger.error(err)
  93. if (trx) {
  94. trx.rollback()
  95. }
  96. }
  97. }
  98. }