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.

66 lines
1.7 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. providers(obj, args, context, info) {
  16. let prv = _.map(WIKI.auth.strategies, str => ({
  17. isEnabled: str.config.isEnabled,
  18. key: str.key,
  19. props: str.props,
  20. title: str.title,
  21. useForm: str.useForm,
  22. config: []
  23. }))
  24. if (args.filter) { prv = graphHelper.filter(prv, args.filter) }
  25. if (args.orderBy) { prv = graphHelper.orderBy(prv, args.orderBy) }
  26. return prv
  27. }
  28. },
  29. AuthenticationMutation: {
  30. async login(obj, args, context) {
  31. try {
  32. let authResult = await WIKI.db.User.login(args, context)
  33. return {
  34. ...authResult,
  35. operation: graphHelper.generateSuccess('Login success')
  36. }
  37. } catch (err) {
  38. return graphHelper.generateError(err)
  39. }
  40. },
  41. async loginTFA(obj, args, context) {
  42. try {
  43. let authResult = await WIKI.db.User.loginTFA(args, context)
  44. return {
  45. ...authResult,
  46. operation: graphHelper.generateSuccess('TFA success')
  47. }
  48. } catch (err) {
  49. return graphHelper.generateError(err)
  50. }
  51. }
  52. },
  53. AuthenticationProvider: {
  54. icon (ap, args) {
  55. return fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${ap.key}.svg`), 'utf8').catch(err => {
  56. if (err.code === 'ENOENT') {
  57. return null
  58. }
  59. throw err
  60. })
  61. }
  62. }
  63. }