User.scala 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.language.postfixOps
  9. import scalaoauth2.provider.AccessToken
  10. import scala.language.existentials
  11. import scala.language.implicitConversions
  12. /* Basic User information */
  13. case class User (
  14. val _id: ObjectId,
  15. val fname: String,
  16. val lname: String,
  17. val email: String,
  18. val password: String,
  19. val created: Instant = Instant.now(),
  20. val isAdmin: Boolean = false
  21. ) {
  22. def toShared() = com.weEat.shared.models.User(
  23. _id,
  24. fname,
  25. lname,
  26. email
  27. )
  28. }
  29. object User extends Collectable[User] {
  30. import com.weEat.shared.models.UserRegistration
  31. import com.github.t3hnar.bcrypt.BCryptStrOps
  32. val collectionName = "users"
  33. def apply(reg: UserRegistration): User = User(
  34. new ObjectId(),
  35. reg.fname,
  36. reg.lname,
  37. reg.email,
  38. reg.password.boundedBcrypt
  39. )
  40. }
  41. class Authorization (
  42. val accessToken: Array[Byte],
  43. val refreshToken: Array[Byte],
  44. val created: Instant,
  45. val email: String,
  46. val userId: ObjectId,
  47. val hasAdminPermissions: Boolean
  48. ) {
  49. def accessExpiration() = created + Authorization.accessFreshTime
  50. def refreshExpiration() = created + Authorization.refreshFreshTime
  51. implicit def asFiniteDuration(d: java.time.Duration) =
  52. scala.concurrent.duration.Duration.fromNanos(d.toNanos)
  53. implicit def asDate(d: java.time.Instant) =
  54. new java.util.Date(d.toEpochMilli())
  55. def toToken() = new AccessToken(
  56. Authorization.encodeToken(accessToken),
  57. Some(Authorization.encodeToken(refreshToken)),
  58. Some(Set.concat(
  59. Some("user"),
  60. Option.when(hasAdminPermissions)("admin")
  61. ).mkString(" ")),
  62. Some(Duration.between(Instant.now(), accessExpiration()).getSeconds()),
  63. created
  64. )
  65. def toUserAuth() = UserAuthorization(
  66. Authorization.encodeToken(accessToken),
  67. "Bearer",
  68. Duration.between(Instant.now(), accessExpiration()),
  69. Authorization.encodeToken(refreshToken),
  70. Set.concat(
  71. Some("user"),
  72. Option.when(hasAdminPermissions)("admin")
  73. )
  74. )
  75. }
  76. object Authorization extends Collectable[Authorization] {
  77. val accessFreshTime = 1 hour
  78. val refreshFreshTime = 10 hour
  79. val collectionName = "authorizations"
  80. private val rand = new SecureRandom()
  81. private def generateSecureBytes(n: Int = 32): Array[Byte] = {
  82. val token = new Array[Byte](n);
  83. rand.nextBytes(token)
  84. token
  85. }
  86. def encodeToken(token: Array[Byte]) = Base64.getEncoder.encodeToString(token)
  87. def decodeToken(token: String) = Base64.getDecoder.decode(token)
  88. def apply(user: User) = new Authorization(
  89. generateSecureBytes(),
  90. generateSecureBytes(),
  91. Instant.now(),
  92. user.email,
  93. user._id,
  94. user.isAdmin
  95. )
  96. }