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.

97 lines
2.9 KiB

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