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.

122 lines
3.6 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'],
  18. properties: {
  19. key: {type: 'string'},
  20. selfRegistration: {type: 'boolean'}
  21. }
  22. }
  23. }
  24. static get jsonAttributes() {
  25. return ['config', 'domainWhitelist', 'autoEnrollGroups']
  26. }
  27. static async getStrategy(key) {
  28. return WIKI.models.authentication.query().findOne({ key })
  29. }
  30. static async getStrategies() {
  31. const strategies = await WIKI.models.authentication.query().orderBy('order')
  32. return strategies.map(str => ({
  33. ...str,
  34. domainWhitelist: _.get(str.domainWhitelist, 'v', []),
  35. autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
  36. }))
  37. }
  38. static async getStrategiesForLegacyClient() {
  39. const strategies = await WIKI.models.authentication.query().select('key', 'selfRegistration')
  40. let formStrategies = []
  41. let socialStrategies = []
  42. for (let stg of strategies) {
  43. const stgInfo = _.find(WIKI.data.authentication, ['key', stg.key]) || {}
  44. if (stgInfo.useForm) {
  45. formStrategies.push({
  46. key: stg.key,
  47. title: stgInfo.title
  48. })
  49. } else {
  50. socialStrategies.push({
  51. ...stgInfo,
  52. ...stg,
  53. icon: await fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${stg.key}.svg`), 'utf8').catch(err => {
  54. if (err.code === 'ENOENT') {
  55. return null
  56. }
  57. throw err
  58. })
  59. })
  60. }
  61. }
  62. return {
  63. formStrategies,
  64. socialStrategies
  65. }
  66. }
  67. static async refreshStrategiesFromDisk() {
  68. try {
  69. const dbStrategies = await WIKI.models.authentication.query()
  70. // -> Fetch definitions from disk
  71. const authDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
  72. WIKI.data.authentication = []
  73. for (let dir of authDirs) {
  74. const defRaw = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
  75. const def = yaml.safeLoad(defRaw)
  76. WIKI.data.authentication.push({
  77. ...def,
  78. props: commonHelper.parseModuleProps(def.props)
  79. })
  80. }
  81. for (const strategy of dbStrategies) {
  82. const strategyDef = _.find(WIKI.data.authentication, ['key', strategy.strategyKey])
  83. if (!strategyDef) {
  84. await WIKI.models.authentication.query().delete().where('key', strategy.key)
  85. WIKI.logger.info(`Authentication strategy ${strategy.strategyKey} was removed from disk: [ REMOVED ]`)
  86. continue
  87. }
  88. strategy.config = _.transform(strategyDef.props, (result, value, key) => {
  89. if (!_.has(result, key)) {
  90. _.set(result, key, value.default)
  91. }
  92. return result
  93. }, strategy.config)
  94. // Fix pre-2.5 strategies displayName
  95. if (!strategy.displayName) {
  96. await WIKI.models.authentication.query().patch({
  97. displayName: strategyDef.title
  98. }).where('key', strategy.key)
  99. }
  100. }
  101. WIKI.logger.info(`Loaded ${WIKI.data.authentication.length} authentication strategies: [ OK ]`)
  102. } catch (err) {
  103. WIKI.logger.error(`Failed to scan or load new authentication providers: [ FAILED ]`)
  104. WIKI.logger.error(err)
  105. }
  106. }
  107. }