authentication.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const Model = require('objection').Model
  2. const autoload = require('auto-load')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. const commonHelper = require('../../helpers/common')
  6. /* global WIKI */
  7. /**
  8. * Authentication model
  9. */
  10. module.exports = class Authentication extends Model {
  11. static get tableName() { return 'authentication' }
  12. static get jsonSchema () {
  13. return {
  14. type: 'object',
  15. required: ['key', 'title', 'isEnabled', 'useForm'],
  16. properties: {
  17. id: {type: 'integer'},
  18. key: {type: 'string'},
  19. title: {type: 'string'},
  20. isEnabled: {type: 'boolean'},
  21. useForm: {type: 'boolean'},
  22. config: {type: 'object'},
  23. selfRegistration: {type: 'boolean'},
  24. domainWhitelist: {type: 'object'},
  25. autoEnrollGroups: {type: 'object'}
  26. }
  27. }
  28. }
  29. static async getStrategies() {
  30. const strategies = await WIKI.db.authentication.query()
  31. return strategies.map(str => ({
  32. ...str,
  33. domainWhitelist: _.get(str.domainWhitelist, 'v', []),
  34. autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
  35. }))
  36. }
  37. static async refreshStrategiesFromDisk() {
  38. try {
  39. const dbStrategies = await WIKI.db.authentication.query()
  40. const diskStrategies = autoload(path.join(WIKI.SERVERPATH, 'modules/authentication'))
  41. let newStrategies = []
  42. _.forOwn(diskStrategies, (strategy, strategyKey) => {
  43. if (!_.some(dbStrategies, ['key', strategy.key])) {
  44. newStrategies.push({
  45. key: strategy.key,
  46. title: strategy.title,
  47. isEnabled: false,
  48. useForm: strategy.useForm,
  49. config: _.transform(strategy.props, (result, value, key) => {
  50. if (_.isPlainObject(value)) {
  51. let cfgValue = {
  52. type: typeof value.type(),
  53. value: !_.isNil(value.default) ? value.default : commonHelper.getTypeDefaultValue(value)
  54. }
  55. if (_.isArray(value.enum)) {
  56. cfgValue.enum = value.enum
  57. }
  58. _.set(result, key, cfgValue)
  59. } else {
  60. _.set(result, key, {
  61. type: typeof value(),
  62. value: commonHelper.getTypeDefaultValue(value)
  63. })
  64. }
  65. return result
  66. }, {}),
  67. selfRegistration: false,
  68. domainWhitelist: { v: [] },
  69. autoEnrollGroups: { v: [] }
  70. })
  71. }
  72. })
  73. if (newStrategies.length > 0) {
  74. await WIKI.db.authentication.query().insert(newStrategies)
  75. WIKI.logger.info(`Loaded ${newStrategies.length} new authentication strategies: [ OK ]`)
  76. } else {
  77. WIKI.logger.info(`No new authentication strategies found: [ SKIPPED ]`)
  78. }
  79. } catch (err) {
  80. WIKI.logger.error(`Failed to scan or load new authentication providers: [ FAILED ]`)
  81. WIKI.logger.error(err)
  82. }
  83. }
  84. }