You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. const Util = require('util');
  3. const Jwt = require('@hapi/jwt');
  4. const Schmervice = require('@hapipal/schmervice');
  5. const SecurePassword = require('secure-password');
  6. /** Class for methods used in the User plugin */
  7. module.exports = class UserService extends Schmervice.Service {
  8. /**
  9. * Unsure of what our constructor does
  10. * @param {...any} args
  11. */
  12. constructor(...args) {
  13. super(...args)
  14. const pwd = new SecurePassword()
  15. this.pwd = {
  16. hash: Util.promisify(pwd.hash.bind(pwd)),
  17. verify: Util.promisify(pwd.verify.bind(pwd))
  18. }
  19. }
  20. /**
  21. * Use knex to find users with id column
  22. * @param {number} id
  23. * @param {*} txn
  24. * @returns
  25. */
  26. async findById(id, txn) {
  27. const { User } = this.server.models()
  28. return await User.query(txn).throwIfNotFound().findById(id)
  29. }
  30. /**
  31. * Use knew to find first user with username
  32. * @param {*} username
  33. * @param {*} txn
  34. * @returns
  35. */
  36. async findByUsername(username, txn) {
  37. const { User } = this.server.models()
  38. return await User.query(txn).throwIfNotFound().first().where({ username })
  39. }
  40. /**
  41. * Signup function
  42. * @param {*} param0
  43. * @param {*} txn
  44. * @returns
  45. */
  46. async signup({ password, ...userInfo }, txn) {
  47. const { User } = this.server.models()
  48. const { id } = await User.query(txn).insert(userInfo)
  49. await this.changePassword(id, password, txn)
  50. return id
  51. }
  52. /**
  53. * Updates user's info
  54. * @param {number} id
  55. * @param {*} param1
  56. * @param {*} txn
  57. * @returns
  58. */
  59. async update(id, { password, ...userInfo }, txn) {
  60. const { User } = this.server.models()
  61. if (Object.keys(userInfo).length > 0) {
  62. await User.query(txn).throwIfNotFound().where({ id }).patch(userInfo)
  63. }
  64. if (password) {
  65. await this.changePassword(id, password, txn)
  66. }
  67. return id
  68. }
  69. /**
  70. * Self explanatory
  71. * @param {*} param0
  72. * @param {*} txn
  73. * @returns
  74. */
  75. async login({ email, password }, txn) {
  76. const { User } = this.server.models()
  77. const user = await User.query(txn)
  78. .throwIfNotFound()
  79. .first()
  80. .where({ user_email: email })
  81. /** Uncomment to run password check using SecurePassword */
  82. // const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
  83. // if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
  84. // await this.changePassword(user.id, password, txn)
  85. // }
  86. // else if (passwordCheck !== SecurePassword.VALID) {
  87. // throw User.createNotFoundError()
  88. // }
  89. return user
  90. }
  91. /**
  92. * Create a token to be sent in request headers
  93. * @param {User} user
  94. * @returns {Token}
  95. */
  96. createToken(user) {
  97. const key = this.server.registrations['main-app-plugin'].options.jwtKey
  98. return Jwt.token.generate({
  99. aud: 'urn:audience:test',
  100. iss: 'urn:issuer:test',
  101. email: user.user_email
  102. }, {
  103. key: key,
  104. algorithm: 'HS256'
  105. }, {
  106. ttlSec: 4 * 60 * 60 // 7 days
  107. })
  108. }
  109. /**
  110. * Use knex to try to change password entry
  111. * @param {number} id
  112. * @param {string} password
  113. * @param {*} txn
  114. * @returns {number}
  115. */
  116. async changePassword(id, password, txn) {
  117. const { User } = this.server.models()
  118. await User.query(txn).throwIfNotFound().where({ id }).patch({
  119. password: await this.pwd.hash(Buffer.from(password))
  120. })
  121. return id
  122. }
  123. }