authentication.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /**
  15. * Fetch active authentication strategies
  16. */
  17. async strategies(obj, args, context, info) {
  18. let strategies = await WIKI.models.authentication.getStrategies(args.isEnabled)
  19. strategies = strategies.map(stg => {
  20. const strategyInfo = _.find(WIKI.data.authentication, ['key', stg.key]) || {}
  21. return {
  22. ...strategyInfo,
  23. ...stg,
  24. config: _.sortBy(_.transform(stg.config, (res, value, key) => {
  25. const configData = _.get(strategyInfo.props, key, false)
  26. if (configData) {
  27. res.push({
  28. key,
  29. value: JSON.stringify({
  30. ...configData,
  31. value
  32. })
  33. })
  34. }
  35. }, []), 'key')
  36. }
  37. })
  38. return strategies
  39. }
  40. },
  41. AuthenticationMutation: {
  42. /**
  43. * Perform Login
  44. */
  45. async login(obj, args, context) {
  46. try {
  47. const authResult = await WIKI.models.users.login(args, context)
  48. return {
  49. ...authResult,
  50. responseResult: graphHelper.generateSuccess('Login success')
  51. }
  52. } catch (err) {
  53. // LDAP Debug Flag
  54. if (args.strategy === 'ldap' && WIKI.config.flags.ldapdebug) {
  55. WIKI.logger.warn('LDAP LOGIN ERROR (c1): ', err)
  56. }
  57. return graphHelper.generateError(err)
  58. }
  59. },
  60. /**
  61. * Perform 2FA Login
  62. */
  63. async loginTFA(obj, args, context) {
  64. try {
  65. const authResult = await WIKI.models.users.loginTFA(args, context)
  66. return {
  67. ...authResult,
  68. responseResult: graphHelper.generateSuccess('TFA success')
  69. }
  70. } catch (err) {
  71. return graphHelper.generateError(err)
  72. }
  73. },
  74. /**
  75. * Register a new account
  76. */
  77. async register(obj, args, context) {
  78. try {
  79. await WIKI.models.users.register({ ...args, verify: true }, context)
  80. return {
  81. responseResult: graphHelper.generateSuccess('Registration success')
  82. }
  83. } catch (err) {
  84. return graphHelper.generateError(err)
  85. }
  86. },
  87. /**
  88. * Update Authentication Strategies
  89. */
  90. async updateStrategies(obj, args, context) {
  91. try {
  92. WIKI.config.auth = {
  93. audience: _.get(args, 'config.audience', WIKI.config.auth.audience),
  94. tokenExpiration: _.get(args, 'config.tokenExpiration', WIKI.config.auth.tokenExpiration),
  95. tokenRenewal: _.get(args, 'config.tokenRenewal', WIKI.config.auth.tokenRenewal)
  96. }
  97. await WIKI.configSvc.saveToDb(['auth'])
  98. for (let str of args.strategies) {
  99. await WIKI.models.authentication.query().patch({
  100. isEnabled: str.isEnabled,
  101. config: _.reduce(str.config, (result, value, key) => {
  102. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  103. return result
  104. }, {}),
  105. selfRegistration: str.selfRegistration,
  106. domainWhitelist: { v: str.domainWhitelist },
  107. autoEnrollGroups: { v: str.autoEnrollGroups }
  108. }).where('key', str.key)
  109. }
  110. await WIKI.auth.activateStrategies()
  111. return {
  112. responseResult: graphHelper.generateSuccess('Strategies updated successfully')
  113. }
  114. } catch (err) {
  115. return graphHelper.generateError(err)
  116. }
  117. },
  118. /**
  119. * Generate New Authentication Public / Private Key Certificates
  120. */
  121. async regenerateCertificates(obj, args, context) {
  122. try {
  123. await WIKI.auth.regenerateCertificates()
  124. return {
  125. responseResult: graphHelper.generateSuccess('Certificates have been regenerated successfully.')
  126. }
  127. } catch (err) {
  128. return graphHelper.generateError(err)
  129. }
  130. },
  131. /**
  132. * Reset Guest User
  133. */
  134. async resetGuestUser(obj, args, context) {
  135. try {
  136. await WIKI.auth.resetGuestUser()
  137. return {
  138. responseResult: graphHelper.generateSuccess('Guest user has been reset successfully.')
  139. }
  140. } catch (err) {
  141. return graphHelper.generateError(err)
  142. }
  143. }
  144. },
  145. AuthenticationStrategy: {
  146. icon (ap, args) {
  147. return fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${ap.key}.svg`), 'utf8').catch(err => {
  148. if (err.code === 'ENOENT') {
  149. return null
  150. }
  151. throw err
  152. })
  153. }
  154. }
  155. }