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.

94 lines
2.8 KiB

  1. const _ = require('lodash')
  2. const fs = require('fs-extra')
  3. const path = require('path')
  4. const graphHelper = require('../../helpers/graph')
  5. /* global WIKI */
  6. module.exports = {
  7. Query: {
  8. async authentication() { return {} }
  9. },
  10. Mutation: {
  11. async authentication() { return {} }
  12. },
  13. AuthenticationQuery: {
  14. async strategies(obj, args, context, info) {
  15. let strategies = await WIKI.models.authentication.getStrategies(args.isEnabled)
  16. strategies = strategies.map(stg => {
  17. const strategyInfo = _.find(WIKI.data.authentication, ['key', stg.key]) || {}
  18. return {
  19. ...strategyInfo,
  20. ...stg,
  21. config: _.sortBy(_.transform(stg.config, (res, value, key) => {
  22. const configData = _.get(strategyInfo.props, key, {})
  23. res.push({
  24. key,
  25. value: JSON.stringify({
  26. ...configData,
  27. value
  28. })
  29. })
  30. }, []), 'key')
  31. }
  32. })
  33. return strategies
  34. }
  35. },
  36. AuthenticationMutation: {
  37. async login(obj, args, context) {
  38. try {
  39. let authResult = await WIKI.models.users.login(args, context)
  40. return {
  41. ...authResult,
  42. responseResult: graphHelper.generateSuccess('Login success')
  43. }
  44. } catch (err) {
  45. return graphHelper.generateError(err)
  46. }
  47. },
  48. async loginTFA(obj, args, context) {
  49. try {
  50. let authResult = await WIKI.models.users.loginTFA(args, context)
  51. return {
  52. ...authResult,
  53. responseResult: graphHelper.generateSuccess('TFA success')
  54. }
  55. } catch (err) {
  56. return graphHelper.generateError(err)
  57. }
  58. },
  59. async updateStrategies(obj, args, context) {
  60. try {
  61. for (let str of args.strategies) {
  62. await WIKI.models.authentication.query().patch({
  63. isEnabled: str.isEnabled,
  64. config: _.reduce(str.config, (result, value, key) => {
  65. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  66. return result
  67. }, {}),
  68. selfRegistration: str.selfRegistration,
  69. domainWhitelist: { v: str.domainWhitelist },
  70. autoEnrollGroups: { v: str.autoEnrollGroups }
  71. }).where('key', str.key)
  72. }
  73. await WIKI.auth.activateStrategies()
  74. return {
  75. responseResult: graphHelper.generateSuccess('Strategies updated successfully')
  76. }
  77. } catch (err) {
  78. return graphHelper.generateError(err)
  79. }
  80. }
  81. },
  82. AuthenticationStrategy: {
  83. icon (ap, args) {
  84. return fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${ap.key}.svg`), 'utf8').catch(err => {
  85. if (err.code === 'ENOENT') {
  86. return null
  87. }
  88. throw err
  89. })
  90. }
  91. }
  92. }