token.js 4.1 KB

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