authentication.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, false)
  23. if (configData) {
  24. res.push({
  25. key,
  26. value: JSON.stringify({
  27. ...configData,
  28. value
  29. })
  30. })
  31. }
  32. }, []), 'key')
  33. }
  34. })
  35. return strategies
  36. }
  37. },
  38. AuthenticationMutation: {
  39. async login(obj, args, context) {
  40. try {
  41. const authResult = await WIKI.models.users.login(args, context)
  42. return {
  43. ...authResult,
  44. responseResult: graphHelper.generateSuccess('Login success')
  45. }
  46. } catch (err) {
  47. // LDAP Debug Flag
  48. if (args.strategy === 'ldap' && WIKI.config.flags.ldapdebug) {
  49. WIKI.logger.warn('LDAP LOGIN ERROR (c1): ', err)
  50. }
  51. return graphHelper.generateError(err)
  52. }
  53. },
  54. async loginTFA(obj, args, context) {
  55. try {
  56. const authResult = await WIKI.models.users.loginTFA(args, context)
  57. return {
  58. ...authResult,
  59. responseResult: graphHelper.generateSuccess('TFA success')
  60. }
  61. } catch (err) {
  62. return graphHelper.generateError(err)
  63. }
  64. },
  65. async register(obj, args, context) {
  66. try {
  67. await WIKI.models.users.register({ ...args, verify: true }, context)
  68. return {
  69. responseResult: graphHelper.generateSuccess('Registration success')
  70. }
  71. } catch (err) {
  72. return graphHelper.generateError(err)
  73. }
  74. },
  75. async updateStrategies(obj, args, context) {
  76. try {
  77. WIKI.config.auth = {
  78. audience: _.get(args, 'config.audience', WIKI.config.auth.audience),
  79. tokenExpiration: _.get(args, 'config.tokenExpiration', WIKI.config.auth.tokenExpiration),
  80. tokenRenewal: _.get(args, 'config.tokenRenewal', WIKI.config.auth.tokenRenewal)
  81. }
  82. await WIKI.configSvc.saveToDb(['auth'])
  83. for (let str of args.strategies) {
  84. await WIKI.models.authentication.query().patch({
  85. isEnabled: str.isEnabled,
  86. config: _.reduce(str.config, (result, value, key) => {
  87. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  88. return result
  89. }, {}),
  90. selfRegistration: str.selfRegistration,
  91. domainWhitelist: { v: str.domainWhitelist },
  92. autoEnrollGroups: { v: str.autoEnrollGroups }
  93. }).where('key', str.key)
  94. }
  95. await WIKI.auth.activateStrategies()
  96. return {
  97. responseResult: graphHelper.generateSuccess('Strategies updated successfully')
  98. }
  99. } catch (err) {
  100. return graphHelper.generateError(err)
  101. }
  102. }
  103. },
  104. AuthenticationStrategy: {
  105. icon (ap, args) {
  106. return fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${ap.key}.svg`), 'utf8').catch(err => {
  107. if (err.code === 'ENOENT') {
  108. return null
  109. }
  110. throw err
  111. })
  112. }
  113. }
  114. }