authentication.mjs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { Model } from 'objection'
  2. import fs from 'node:fs/promises'
  3. import path from 'node:path'
  4. import { get } from 'lodash-es'
  5. import yaml from 'js-yaml'
  6. import { parseModuleProps } from '../helpers/common.mjs'
  7. /**
  8. * Authentication model
  9. */
  10. export 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(module) {
  28. return WIKI.db.authentication.query().findOne({ module })
  29. }
  30. static async getStrategies({ enabledOnly = false } = {}) {
  31. return WIKI.db.authentication.query().where(enabledOnly ? { isEnabled: true } : {})
  32. }
  33. static async refreshStrategiesFromDisk() {
  34. try {
  35. // -> Fetch definitions from disk
  36. const authenticationDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
  37. WIKI.data.authentication = []
  38. for (const dir of authenticationDirs) {
  39. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
  40. const defParsed = yaml.load(def)
  41. if (!defParsed.isAvailable) { continue }
  42. defParsed.key = dir
  43. defParsed.props = parseModuleProps(defParsed.props)
  44. WIKI.data.authentication.push(defParsed)
  45. WIKI.logger.debug(`Loaded authentication module definition ${dir}: [ OK ]`)
  46. }
  47. WIKI.logger.info(`Loaded ${WIKI.data.authentication.length} authentication module definitions: [ OK ]`)
  48. } catch (err) {
  49. WIKI.logger.error(`Failed to scan or load authentication providers: [ FAILED ]`)
  50. WIKI.logger.error(err)
  51. }
  52. }
  53. }