User.scala 2.7 KB

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