tokens.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const express = require('express');
  2. const jwtdecode = require('../../lib/express/jwt-decode');
  3. const internalToken = require('../../internal/token');
  4. const apiValidator = require('../../lib/validator/api');
  5. let router = express.Router({
  6. caseSensitive: true,
  7. strict: true,
  8. mergeParams: true
  9. });
  10. router
  11. .route('/')
  12. .options((req, res) => {
  13. res.sendStatus(204);
  14. })
  15. /**
  16. * GET /tokens
  17. *
  18. * Get a new Token, given they already have a token they want to refresh
  19. * We also piggy back on to this method, allowing admins to get tokens
  20. * for services like Job board and Worker.
  21. */
  22. .get(jwtdecode(), (req, res, next) => {
  23. internalToken.getFreshToken(res.locals.access, {
  24. expiry: (typeof req.query.expiry !== 'undefined' ? req.query.expiry : null),
  25. scope: (typeof req.query.scope !== 'undefined' ? req.query.scope : null)
  26. })
  27. .then((data) => {
  28. res.status(200)
  29. .send(data);
  30. })
  31. .catch(next);
  32. })
  33. /**
  34. * POST /tokens
  35. *
  36. * Create a new Token
  37. */
  38. .post((req, res, next) => {
  39. apiValidator({$ref: 'endpoints/tokens#/links/0/schema'}, req.body)
  40. .then((payload) => {
  41. return internalToken.getTokenFromEmail(payload);
  42. })
  43. .then((data) => {
  44. res.status(200)
  45. .send(data);
  46. })
  47. .catch(next);
  48. });
  49. module.exports = router;