token.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const _ = require('lodash');
  2. const error = require('../lib/error');
  3. const userModel = require('../models/user');
  4. const authModel = require('../models/auth');
  5. const helpers = require('../lib/helpers');
  6. const TokenModel = require('../models/token');
  7. module.exports = {
  8. /**
  9. * @param {Object} data
  10. * @param {String} data.identity
  11. * @param {String} data.secret
  12. * @param {String} [data.scope]
  13. * @param {String} [data.expiry]
  14. * @param {String} [issuer]
  15. * @returns {Promise}
  16. */
  17. getTokenFromEmail: (data, issuer) => {
  18. let Token = new TokenModel();
  19. data.scope = data.scope || 'user';
  20. data.expiry = data.expiry || '1d';
  21. return userModel
  22. .query()
  23. .where('email', data.identity.toLowerCase().trim())
  24. .andWhere('is_deleted', 0)
  25. .andWhere('is_disabled', 0)
  26. .first()
  27. .then((user) => {
  28. if (user) {
  29. // Get auth
  30. return authModel
  31. .query()
  32. .where('user_id', '=', user.id)
  33. .where('type', '=', 'password')
  34. .first()
  35. .then((auth) => {
  36. if (auth) {
  37. return auth.verifyPassword(data.secret)
  38. .then((valid) => {
  39. if (valid) {
  40. if (data.scope !== 'user' && _.indexOf(user.roles, data.scope) === -1) {
  41. // The scope requested doesn't exist as a role against the user,
  42. // you shall not pass.
  43. throw new error.AuthError('Invalid scope: ' + data.scope);
  44. }
  45. // Create a moment of the expiry expression
  46. let expiry = helpers.parseDatePeriod(data.expiry);
  47. if (expiry === null) {
  48. throw new error.AuthError('Invalid expiry time: ' + data.expiry);
  49. }
  50. return Token.create({
  51. iss: issuer || 'api',
  52. attrs: {
  53. id: user.id
  54. },
  55. scope: [data.scope],
  56. expiresIn: data.expiry
  57. })
  58. .then((signed) => {
  59. return {
  60. token: signed.token,
  61. expires: expiry.toISOString()
  62. };
  63. });
  64. } else {
  65. throw new error.AuthError('Invalid password');
  66. }
  67. });
  68. } else {
  69. throw new error.AuthError('No password auth for user');
  70. }
  71. });
  72. } else {
  73. throw new error.AuthError('No relevant user found');
  74. }
  75. });
  76. },
  77. /**
  78. * @param {Access} access
  79. * @param {Object} [data]
  80. * @param {String} [data.expiry]
  81. * @param {String} [data.scope] Only considered if existing token scope is admin
  82. * @returns {Promise}
  83. */
  84. getFreshToken: (access, data) => {
  85. let Token = new TokenModel();
  86. data = data || {};
  87. data.expiry = data.expiry || '1d';
  88. if (access && access.token.getUserId(0)) {
  89. // Create a moment of the expiry expression
  90. let expiry = helpers.parseDatePeriod(data.expiry);
  91. if (expiry === null) {
  92. throw new error.AuthError('Invalid expiry time: ' + data.expiry);
  93. }
  94. let token_attrs = {
  95. id: access.token.getUserId(0)
  96. };
  97. // Only admins can request otherwise scoped tokens
  98. let scope = access.token.get('scope');
  99. if (data.scope && access.token.hasScope('admin')) {
  100. scope = [data.scope];
  101. if (data.scope === 'job-board' || data.scope === 'worker') {
  102. token_attrs.id = 0;
  103. }
  104. }
  105. return Token.create({
  106. iss: 'api',
  107. scope: scope,
  108. attrs: token_attrs,
  109. expiresIn: data.expiry
  110. })
  111. .then((signed) => {
  112. return {
  113. token: signed.token,
  114. expires: expiry.toISOString()
  115. };
  116. });
  117. } else {
  118. throw new error.AssertionFailedError('Existing token contained invalid user data');
  119. }
  120. },
  121. /**
  122. * @param {Object} user
  123. * @returns {Promise}
  124. */
  125. getTokenFromUser: (user) => {
  126. const expire = '1d';
  127. const Token = new TokenModel();
  128. const expiry = helpers.parseDatePeriod(expire);
  129. return Token.create({
  130. iss: 'api',
  131. attrs: {
  132. id: user.id
  133. },
  134. scope: ['user'],
  135. expiresIn: expire
  136. })
  137. .then((signed) => {
  138. return {
  139. token: signed.token,
  140. expires: expiry.toISOString(),
  141. user: user
  142. };
  143. });
  144. }
  145. };