Adapt authoring tool UI documentation

v1.0.0-rc.4

adapt-authoring-auth/lib/AuthUtils.js

  1. import { App } from 'adapt-authoring-core'
  2. /**
  3. * Auth-related utility functions
  4. * @memberof auth
  5. */
  6. class AuthUtils {
  7. /**
  8. * Returns a generic empty object for mapping values to HTTP methods
  9. * @return {RouteStore}
  10. */
  11. static createEmptyStore () {
  12. /**
  13. * A key/value store linking API route/HTTP methods to values
  14. * @memberof auth
  15. * @typedef {Object} RouteStore
  16. * @property {Array} post Data relating to the post HTTP method
  17. * @property {Array} get Data relating to the get HTTP method
  18. * @property {Array} put Data relating to the put HTTP method
  19. * @property {Array} patch Data relating to the patch HTTP method
  20. * @property {Array} delete Data relating to the delete HTTP method
  21. */
  22. return {
  23. post: [],
  24. get: [],
  25. put: [],
  26. patch: [],
  27. delete: []
  28. }
  29. }
  30. /**
  31. * Adds auth data to the incoming request
  32. * @param {external:ExpressRequest} req
  33. * @return {Promise}
  34. */
  35. static async initAuthData (req) {
  36. req.auth = {}
  37. const authHeader = req.get('Authorization') || req.headers.Authorization
  38. if (!authHeader) {
  39. return
  40. }
  41. const [type, value] = authHeader.split(' ')
  42. req.auth.header = { type, value }
  43. }
  44. /**
  45. * Shortcut to retrieve auth config values
  46. * @param {String} key
  47. * @return {String}
  48. */
  49. static getConfig (key) {
  50. return App.instance.config.get(`adapt-authoring-auth.${key}`)
  51. }
  52. /**
  53. * Logs a message using the logger
  54. * @param {String} level The log level
  55. * @param {...*} args Other aruments
  56. */
  57. static log (level, ...args) {
  58. return App.instance.logger.log(level, 'auth', ...args)
  59. }
  60. }
  61. export default AuthUtils