2
0

authentication.js 3.6 KB

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