authentication.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.db.authentication.getStrategies()
  17. strategies = strategies.map(stg => ({
  18. ...stg,
  19. config: _.sortBy(_.transform(stg.config, (res, value, key) => {
  20. res.push({ key, value: JSON.stringify(value) })
  21. }, []), 'key')
  22. }))
  23. if (args.filter) { strategies = graphHelper.filter(strategies, args.filter) }
  24. if (args.orderBy) { strategies = graphHelper.orderBy(strategies, args.orderBy) }
  25. return strategies
  26. }
  27. },
  28. AuthenticationMutation: {
  29. async login(obj, args, context) {
  30. try {
  31. let authResult = await WIKI.db.users.login(args, context)
  32. return {
  33. ...authResult,
  34. responseResult: graphHelper.generateSuccess('Login success')
  35. }
  36. } catch (err) {
  37. return graphHelper.generateError(err)
  38. }
  39. },
  40. async loginTFA(obj, args, context) {
  41. try {
  42. let authResult = await WIKI.db.users.loginTFA(args, context)
  43. return {
  44. ...authResult,
  45. responseResult: graphHelper.generateSuccess('TFA success')
  46. }
  47. } catch (err) {
  48. return graphHelper.generateError(err)
  49. }
  50. },
  51. async updateStrategies(obj, args, context) {
  52. try {
  53. for (let str of args.strategies) {
  54. await WIKI.db.authentication.query().patch({
  55. isEnabled: str.isEnabled,
  56. config: _.reduce(str.config, (result, value, key) => {
  57. _.set(result, `${value.key}.value`, value.value)
  58. return result
  59. }, {}),
  60. selfRegistration: str.selfRegistration,
  61. domainWhitelist: { v: str.domainWhitelist },
  62. autoEnrollGroups: { v: str.autoEnrollGroups }
  63. }).where('key', str.key)
  64. }
  65. return {
  66. responseResult: graphHelper.generateSuccess('Strategies updated successfully')
  67. }
  68. } catch (err) {
  69. return graphHelper.generateError(err)
  70. }
  71. }
  72. },
  73. AuthenticationStrategy: {
  74. icon (ap, args) {
  75. return fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${ap.key}.svg`), 'utf8').catch(err => {
  76. if (err.code === 'ENOENT') {
  77. return null
  78. }
  79. throw err
  80. })
  81. }
  82. }
  83. }