Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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)
  39. .throwIfNotFound()
  40. .first()
  41. .where({ username })
  42. }
  43. /**
  44. * Signup function
  45. * @param {*} param0
  46. * @param {*} txn
  47. * @returns
  48. */
  49. async signup({ password, ...userInfo }, txn) {
  50. const { User } = this.server.models()
  51. const { id } = await User.query(txn).insert(userInfo)
  52. await this.changePassword(id, password, txn)
  53. return id
  54. }
  55. /**
  56. * Updates user's info
  57. * @param {number} id
  58. * @param {*} param1
  59. * @param {*} txn
  60. * @returns
  61. */
  62. async update(id, { password, ...userInfo }, txn) {
  63. const { User } = this.server.models()
  64. if (Object.keys(userInfo).length > 0) {
  65. await User.query(txn)
  66. .throwIfNotFound()
  67. .where({ id })
  68. .patch(userInfo)
  69. }
  70. if (password) {
  71. await this.changePassword(id, password, txn)
  72. }
  73. return id
  74. }
  75. /**
  76. * Self explanatory
  77. * @param {*} param0
  78. * @param {*} txn
  79. * @returns
  80. */
  81. async login({ email, password }, txn) {
  82. const { User } = this.server.models()
  83. const user = await User.query(txn)
  84. .throwIfNotFound()
  85. .first()
  86. .where({ user_email: email })
  87. /** Uncomment to run password check using SecurePassword */
  88. // const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
  89. // if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
  90. // await this.changePassword(user.id, password, txn)
  91. // }
  92. // else if (passwordCheck !== SecurePassword.VALID) {
  93. // throw User.createNotFoundError()
  94. // }
  95. return user
  96. }
  97. /**
  98. * Create a token to be sent in request headers
  99. * @param {User} user
  100. * @returns {Token}
  101. */
  102. createToken(user) {
  103. const key = this.server.registrations['main-app-plugin'].options.jwtKey
  104. return Jwt.token.generate(
  105. {
  106. aud: 'urn:audience:test',
  107. iss: 'urn:issuer:test',
  108. email: user.user_email,
  109. },
  110. {
  111. key: key,
  112. algorithm: 'HS256',
  113. },
  114. {
  115. ttlSec: 4 * 60 * 60, // 7 days
  116. },
  117. )
  118. }
  119. /**
  120. * Use knex to try to change password entry
  121. * @param {number} id
  122. * @param {string} password
  123. * @param {*} txn
  124. * @returns {number}
  125. */
  126. async changePassword(id, password, txn) {
  127. const { User } = this.server.models()
  128. await User.query(txn)
  129. .throwIfNotFound()
  130. .where({ id })
  131. .patch({
  132. password: await this.pwd.hash(Buffer.from(password)),
  133. })
  134. return id
  135. }
  136. }