User.scala 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.weEat.models
  2. import codes.reactive.scalatime._
  3. import org.bson.types.ObjectId
  4. import com.weEat.shared.models.UserAuthorization
  5. import java.security.SecureRandom
  6. import java.time.{Duration,Instant}
  7. import java.util.Base64
  8. import scala.concurrent.duration.FiniteDuration
  9. import scala.language.postfixOps
  10. import scalaoauth2.provider.AccessToken
  11. /* Basic User information */
  12. case class User (
  13. val _id: ObjectId,
  14. val fname: String,
  15. val lname: String,
  16. val email: String,
  17. val password: String,
  18. val created: Instant = Instant.now()
  19. ) {
  20. def toShared() = com.weEat.shared.models.User(
  21. fname,
  22. lname,
  23. email
  24. )
  25. }
  26. object User extends Collectable[User] {
  27. import com.weEat.shared.models.UserRegistration
  28. import com.github.t3hnar.bcrypt.BCryptStrOps
  29. val collectionName = "users"
  30. def apply(reg: UserRegistration): User = User(
  31. new ObjectId(),
  32. reg.fname,
  33. reg.lname,
  34. reg.email,
  35. reg.password.boundedBcrypt
  36. )
  37. }
  38. class Authorization (
  39. val accessToken: Array[Byte],
  40. val refreshToken: Array[Byte],
  41. val created: Instant,
  42. val email: String,
  43. val userId: ObjectId
  44. ) {
  45. def accessExpiration() = created + Authorization.accessFreshTime
  46. def refreshExpiration() = created + Authorization.refreshFreshTime
  47. implicit def asFiniteDuration(d: java.time.Duration) =
  48. scala.concurrent.duration.Duration.fromNanos(d.toNanos)
  49. implicit def asDate(d: java.time.Instant) =
  50. new java.util.Date(d.toEpochMilli())
  51. def toToken() = new AccessToken(
  52. Authorization.encodeToken(accessToken),
  53. Some(Authorization.encodeToken(refreshToken)),
  54. None,
  55. Some(Duration.between(Instant.now(), accessExpiration()).getSeconds()),
  56. created
  57. )
  58. def toUserAuth() = UserAuthorization(
  59. Authorization.encodeToken(accessToken),
  60. "Bearer",
  61. Duration.between(Instant.now(), accessExpiration()),
  62. Authorization.encodeToken(refreshToken)
  63. )
  64. }
  65. object Authorization extends Collectable[Authorization] {
  66. val accessFreshTime = 1 hour
  67. val refreshFreshTime = 10 hour
  68. val collectionName = "authorizations"
  69. private val rand = new SecureRandom()
  70. private def generateSecureBytes(n: Int = 32): Array[Byte] = {
  71. val token = new Array[Byte](n);
  72. rand.nextBytes(token)
  73. token
  74. }
  75. def encodeToken(token: Array[Byte]) = Base64.getEncoder.encodeToString(token)
  76. def decodeToken(token: String) = Base64.getDecoder.decode(token)
  77. def apply(id: ObjectId, email: String) = new Authorization(
  78. generateSecureBytes(),
  79. generateSecureBytes(),
  80. Instant.now(),
  81. email,
  82. id
  83. )
  84. }