authentication.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 providers(obj, args, context, info) {
  16. let strategies = await WIKI.db.authentication.query().orderBy('title')
  17. strategies = strategies.map(stg => ({
  18. ...stg,
  19. config: _.transform(stg.config, (res, value, key) => {
  20. res.push({ key, value })
  21. }, [])
  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. },
  52. AuthenticationProvider: {
  53. icon (ap, args) {
  54. return fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${ap.key}.svg`), 'utf8').catch(err => {
  55. if (err.code === 'ENOENT') {
  56. return null
  57. }
  58. throw err
  59. })
  60. }
  61. }
  62. }