authentication.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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: ['module'],
  17. properties: {
  18. id: { type: 'string' },
  19. module: { 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({ enabledOnly = false } = {}) {
  32. const strategies = await WIKI.models.authentication.query().where(enabledOnly ? { isEnabled: true } : {})
  33. return strategies.map(str => ({
  34. ...str,
  35. domainWhitelist: _.get(str.domainWhitelist, 'v', []),
  36. autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
  37. }))
  38. }
  39. static async refreshStrategiesFromDisk() {
  40. try {
  41. // -> Fetch definitions from disk
  42. const authenticationDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
  43. WIKI.data.authentication = []
  44. for (const dir of authenticationDirs) {
  45. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
  46. const defParsed = yaml.load(def)
  47. if (!defParsed.isAvailable) { continue }
  48. defParsed.key = dir
  49. defParsed.props = commonHelper.parseModuleProps(defParsed.props)
  50. WIKI.data.authentication.push(defParsed)
  51. WIKI.logger.debug(`Loaded authentication module definition ${dir}: [ OK ]`)
  52. }
  53. WIKI.logger.info(`Loaded ${WIKI.data.authentication.length} authentication module definitions: [ OK ]`)
  54. } catch (err) {
  55. WIKI.logger.error(`Failed to scan or load authentication providers: [ FAILED ]`)
  56. WIKI.logger.error(err)
  57. }
  58. }
  59. }