authentication.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 jsonSchema () {
  14. return {
  15. type: 'object',
  16. required: ['key', 'title', 'isEnabled', 'useForm'],
  17. properties: {
  18. id: {type: 'integer'},
  19. key: {type: 'string'},
  20. title: {type: 'string'},
  21. isEnabled: {type: 'boolean'},
  22. useForm: {type: 'boolean'},
  23. config: {type: 'object'},
  24. selfRegistration: {type: 'boolean'},
  25. domainWhitelist: {type: 'object'},
  26. autoEnrollGroups: {type: 'object'}
  27. }
  28. }
  29. }
  30. static async getStrategies() {
  31. const strategies = await WIKI.db.authentication.query()
  32. return strategies.map(str => ({
  33. ...str,
  34. domainWhitelist: _.get(str.domainWhitelist, 'v', []),
  35. autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
  36. }))
  37. }
  38. static async refreshStrategiesFromDisk() {
  39. try {
  40. const dbStrategies = await WIKI.db.authentication.query()
  41. // -> Fetch definitions from disk
  42. const authDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
  43. let diskStrategies = []
  44. for (let dir of authDirs) {
  45. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
  46. diskStrategies.push(yaml.safeLoad(def))
  47. }
  48. let newStrategies = []
  49. _.forEach(diskStrategies, strategy => {
  50. if (!_.some(dbStrategies, ['key', strategy.key])) {
  51. newStrategies.push({
  52. key: strategy.key,
  53. title: strategy.title,
  54. isEnabled: false,
  55. useForm: strategy.useForm,
  56. config: _.transform(strategy.props, (result, value, key) => {
  57. if (_.isPlainObject(value)) {
  58. let cfgValue = {
  59. type: value.type.toLowerCase(),
  60. value: !_.isNil(value.default) ? value.default : commonHelper.getTypeDefaultValue(value.type)
  61. }
  62. if (_.isArray(value.enum)) {
  63. cfgValue.enum = value.enum
  64. }
  65. _.set(result, key, cfgValue)
  66. } else {
  67. _.set(result, key, {
  68. type: value.toLowerCase(),
  69. value: commonHelper.getTypeDefaultValue(value)
  70. })
  71. }
  72. return result
  73. }, {}),
  74. selfRegistration: false,
  75. domainWhitelist: { v: [] },
  76. autoEnrollGroups: { v: [] }
  77. })
  78. }
  79. })
  80. if (newStrategies.length > 0) {
  81. await WIKI.db.authentication.query().insert(newStrategies)
  82. WIKI.logger.info(`Loaded ${newStrategies.length} new authentication strategies: [ OK ]`)
  83. } else {
  84. WIKI.logger.info(`No new authentication strategies found: [ SKIPPED ]`)
  85. }
  86. } catch (err) {
  87. WIKI.logger.error(`Failed to scan or load new authentication providers: [ FAILED ]`)
  88. WIKI.logger.error(err)
  89. }
  90. }
  91. }