authentication.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /**
  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: ['module'],
  16. properties: {
  17. id: { type: 'string' },
  18. module: { type: 'string' },
  19. isEnabled: { type: 'boolean' },
  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.db.authentication.query().findOne({ key })
  29. }
  30. static async getStrategies({ enabledOnly = false } = {}) {
  31. const strategies = await WIKI.db.authentication.query().where(enabledOnly ? { isEnabled: true } : {})
  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. // -> Fetch definitions from disk
  41. const authenticationDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
  42. WIKI.data.authentication = []
  43. for (const dir of authenticationDirs) {
  44. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
  45. const defParsed = yaml.load(def)
  46. if (!defParsed.isAvailable) { continue }
  47. defParsed.key = dir
  48. defParsed.props = commonHelper.parseModuleProps(defParsed.props)
  49. WIKI.data.authentication.push(defParsed)
  50. WIKI.logger.debug(`Loaded authentication module definition ${dir}: [ OK ]`)
  51. }
  52. WIKI.logger.info(`Loaded ${WIKI.data.authentication.length} authentication module definitions: [ OK ]`)
  53. } catch (err) {
  54. WIKI.logger.error(`Failed to scan or load authentication providers: [ FAILED ]`)
  55. WIKI.logger.error(err)
  56. }
  57. }
  58. }