tokens.js 1.3 KB

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