Compare commits
12
Commits
88b187e6a3
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d90ae86e1f | ||
|
|
e9d1f93723 | ||
|
|
d1360b2917 | ||
|
|
e144a7f3c0 | ||
|
|
621fcf90fa | ||
|
|
c45889838b | ||
|
|
98fa797a05 | ||
|
|
8c0deee2df | ||
|
|
2754c86c63 | ||
|
|
eadf793882 | ||
|
|
f2ec6889cb | ||
|
|
a6bbef1bf3 |
@@ -22,7 +22,7 @@ object AuctionPublisher extends ActorModule with Logging {
|
|||||||
sealed trait Command
|
sealed trait Command
|
||||||
private case object Ready extends Command
|
private case object Ready extends Command
|
||||||
case class Publish(auction: Auction, author: User) extends Command
|
case class Publish(auction: Auction, author: User) extends Command
|
||||||
private case class AuctionConfirmed(id: Int, responseTopic: String) extends Command
|
private case class AuctionConfirmed(id: Long, responseTopic: String) extends Command
|
||||||
|
|
||||||
private final val topic = "auctions/created"
|
private final val topic = "auctions/created"
|
||||||
|
|
||||||
|
|||||||
@@ -3,23 +3,43 @@ package controllers
|
|||||||
import actors.auctions.AuctionPublisher
|
import actors.auctions.AuctionPublisher
|
||||||
import controllers.authentication.AuthenticatedAction
|
import controllers.authentication.AuthenticatedAction
|
||||||
import dao.{AuctionsDAO, AuthTokensDAO}
|
import dao.{AuctionsDAO, AuthTokensDAO}
|
||||||
import models.Auction
|
|
||||||
import org.apache.pekko.actor.typed.ActorRef
|
import org.apache.pekko.actor.typed.ActorRef
|
||||||
import play.api.libs.functional.syntax.toFunctionalBuilderOps
|
import play.api.libs.json.*
|
||||||
import play.api.libs.json.{JsError, JsPath, JsValue, Json, Reads, Writes}
|
|
||||||
import play.api.mvc.{BaseController, ControllerComponents}
|
import play.api.mvc.{BaseController, ControllerComponents}
|
||||||
|
|
||||||
import java.security.SecureRandom
|
|
||||||
import javax.inject.{Inject, Singleton}
|
import javax.inject.{Inject, Singleton}
|
||||||
import scala.concurrent.{ExecutionContext, Future}
|
import scala.concurrent.{ExecutionContext, Future}
|
||||||
|
import models.Auction
|
||||||
|
import caching.{CheckFresh, DigestEtagable, Etagable}
|
||||||
|
|
||||||
|
import java.io.DataOutputStream
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class AuctionsController @Inject()(val controllerComponents: ControllerComponents,
|
class AuctionsController @Inject()(val controllerComponents: ControllerComponents,
|
||||||
val auctionsDAO: AuctionsDAO,
|
val auctionsDAO: AuctionsDAO,
|
||||||
val authTokensDAO: AuthTokensDAO,
|
val authTokensDAO: AuthTokensDAO,
|
||||||
val auctionPublisher: ActorRef[AuctionPublisher.Command],
|
authenticatedAction: AuthenticatedAction,
|
||||||
implicit val ec: ExecutionContext) extends BaseController {
|
val auctionPublisher: ActorRef[AuctionPublisher.Command])
|
||||||
def create = Action.andThen(AuthenticatedAction(authTokensDAO)).async(parse.json) { implicit request =>
|
(implicit val ec: ExecutionContext)
|
||||||
|
extends BaseController with CheckFresh {
|
||||||
|
def index = authenticatedAction.async { request =>
|
||||||
|
for {
|
||||||
|
auctions <- auctionsDAO.list_available(request.user)
|
||||||
|
json = Json.toJson(auctions)
|
||||||
|
} yield Ok(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
def show(id: Long) = authenticatedAction.async { implicit request =>
|
||||||
|
auctionsDAO.showAvailable(request.user, id).map { auctionOpt =>
|
||||||
|
auctionOpt.fold(NotFound) { auction =>
|
||||||
|
checkFresh(auction) {
|
||||||
|
Ok(Json.toJson(auction)).withHeaders("etag" -> auction.etag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def create = authenticatedAction.async(parse.json) { implicit request =>
|
||||||
val auctionCreateDataResult = request.body.validate[AuctionCreateData]
|
val auctionCreateDataResult = request.body.validate[AuctionCreateData]
|
||||||
auctionCreateDataResult.fold(
|
auctionCreateDataResult.fold(
|
||||||
errors => Future.successful(BadRequest(JsError.toJson(errors))),
|
errors => Future.successful(BadRequest(JsError.toJson(errors))),
|
||||||
@@ -27,15 +47,27 @@ class AuctionsController @Inject()(val controllerComponents: ControllerComponent
|
|||||||
for {
|
for {
|
||||||
auction <- auctionsDAO.create(auctionCreateData.name, auctionCreateData.startingPrice, request.user)
|
auction <- auctionsDAO.create(auctionCreateData.name, auctionCreateData.startingPrice, request.user)
|
||||||
json = Json.toJson(auction)
|
json = Json.toJson(auction)
|
||||||
response = Created(json)
|
|
||||||
_ = auctionPublisher ! AuctionPublisher.Publish(auction, request.user)
|
_ = auctionPublisher ! AuctionPublisher.Publish(auction, request.user)
|
||||||
} yield response
|
} yield Created(json)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private case class AuctionCreateData(name: String, startingPrice: BigDecimal)
|
private case class AuctionCreateData(name: String, startingPrice: BigDecimal)
|
||||||
private implicit val auctionCreateDataReads: Reads[AuctionCreateData] = (
|
|
||||||
(JsPath \ "name").read[String] and (JsPath \ "starting_price").read[BigDecimal]
|
private given Reads[AuctionCreateData] = Json.reads[AuctionCreateData]
|
||||||
)(AuctionCreateData.apply)
|
|
||||||
|
private given DigestEtagable[Auction] with {
|
||||||
|
override protected def writeDigestData(a: Auction, s: DataOutputStream): Unit = {
|
||||||
|
s.writeLong(a.id.getOrElse(0))
|
||||||
|
s.writeLong(a.authorId)
|
||||||
|
s.writeUTF(a.gid)
|
||||||
|
s.writeUTF(a.name)
|
||||||
|
s.writeLong(a.createdAt.getEpochSecond)
|
||||||
|
|
||||||
|
s.writeLong(a.currentBidderId.getOrElse(0))
|
||||||
|
a.currentBid.foreach(b => s.writeLong((b * 1000).toLong))
|
||||||
|
a.currentBidAt.foreach(ba => s.writeLong(ba.getEpochSecond))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,24 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import javax.inject._
|
import play.api.*
|
||||||
import play.api._
|
import _root_.controllers.authentication.{AuthenticatedAction, AuthenticatedRequest}
|
||||||
import play.api.mvc._
|
import _root_.controllers.caching.{CheckFresh, DigestEtagable}
|
||||||
|
import models.User
|
||||||
|
import play.api.libs.json.Json
|
||||||
|
import play.api.mvc.*
|
||||||
|
|
||||||
|
import java.io.DataOutputStream
|
||||||
|
import java.nio.charset.StandardCharsets.UTF_8
|
||||||
|
import javax.inject.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This controller creates an `Action` to handle HTTP requests to the
|
* This controller creates an `Action` to handle HTTP requests to the
|
||||||
* application's home page.
|
* application's home page.
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
|
class HomeController @Inject()(val controllerComponents: ControllerComponents,
|
||||||
|
authenticatedAction: AuthenticatedAction)
|
||||||
|
extends BaseController with Logging with CheckFresh {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an Action to render an HTML page.
|
* Create an Action to render an HTML page.
|
||||||
@@ -21,4 +30,20 @@ class HomeController @Inject()(val controllerComponents: ControllerComponents) e
|
|||||||
def index() = Action { implicit request: Request[AnyContent] =>
|
def index() = Action { implicit request: Request[AnyContent] =>
|
||||||
Ok(views.html.index())
|
Ok(views.html.index())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def userData = authenticatedAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||||
|
val user = request.user
|
||||||
|
checkFresh(user) {
|
||||||
|
// TODO: put the max-age somewhere into config
|
||||||
|
Ok(Json.toJson(user)).withHeaders("Cache-Control" -> "private, max-age=15, must-revalidate")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private given DigestEtagable[models.User] with {
|
||||||
|
override protected def writeDigestData(a: User, s: DataOutputStream): Unit = {
|
||||||
|
s.writeLong(a.id.getOrElse(0))
|
||||||
|
s.write(a.gid.getBytes(UTF_8))
|
||||||
|
s.write(a.username.getBytes(UTF_8))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,15 @@ import jakarta.inject.Inject
|
|||||||
import models.{AuthToken, User}
|
import models.{AuthToken, User}
|
||||||
import play.api.libs.json.*
|
import play.api.libs.json.*
|
||||||
import play.api.mvc.Results.Unauthorized
|
import play.api.mvc.Results.Unauthorized
|
||||||
import play.api.mvc.{ActionRefiner, Request, Result, WrappedRequest}
|
import play.api.mvc.{ActionBuilder, ActionRefiner, AnyContent, BodyParsers, Request, Result, WrappedRequest}
|
||||||
|
|
||||||
import scala.concurrent.{ExecutionContext, Future}
|
import scala.concurrent.{ExecutionContext, Future}
|
||||||
|
|
||||||
class AuthenticatedRequest[A](val token: AuthToken, val user: User, request: Request[A]) extends WrappedRequest[A](request)
|
class AuthenticatedRequest[A](val token: AuthToken, val user: User, request: Request[A]) extends WrappedRequest[A](request)
|
||||||
|
|
||||||
class AuthenticatedAction @Inject()(authTokensDao: AuthTokensDAO)(implicit val executionContext: ExecutionContext) extends ActionRefiner[Request, AuthenticatedRequest] {
|
class AuthenticatedAction @Inject()(val parser: BodyParsers.Default, authTokensDao: AuthTokensDAO)(implicit val executionContext: ExecutionContext)
|
||||||
|
extends ActionRefiner[Request, AuthenticatedRequest]
|
||||||
|
with ActionBuilder[AuthenticatedRequest, AnyContent]{
|
||||||
override protected def refine[A](request: Request[A]): Future[Either[Result, AuthenticatedRequest[A]]] = {
|
override protected def refine[A](request: Request[A]): Future[Either[Result, AuthenticatedRequest[A]]] = {
|
||||||
val authHeader = request.headers.get("Authorization")
|
val authHeader = request.headers.get("Authorization")
|
||||||
.map(_.split(" ", 2)(1))
|
.map(_.split(" ", 2)(1))
|
||||||
@@ -35,5 +37,4 @@ class AuthenticatedAction @Inject()(authTokensDao: AuthTokensDAO)(implicit val e
|
|||||||
case None => unauthenticatedRequestError
|
case None => unauthenticatedRequestError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,39 +9,45 @@ import play.api.mvc.{AbstractController, ControllerComponents}
|
|||||||
|
|
||||||
import scala.concurrent.{ExecutionContext, Future}
|
import scala.concurrent.{ExecutionContext, Future}
|
||||||
|
|
||||||
|
object SessionsController {
|
||||||
case class LoginData(username: String, password: String)
|
case class LoginData(username: String, password: String)
|
||||||
implicit val loginDataReads: Reads[LoginData] = (
|
given Reads[LoginData] = Json.reads[LoginData]
|
||||||
(JsPath \ "username").read[String] and
|
}
|
||||||
(JsPath \ "password").read[String]
|
|
||||||
)(LoginData.apply)
|
|
||||||
implicit val tokenWrites: Writes[AuthToken] = (token: AuthToken) => Json.obj(
|
|
||||||
"token" -> token.token,
|
|
||||||
"createdAt" -> token.createdAt,
|
|
||||||
"expiresAt" -> token.expiresAt
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class SessionsController @Inject()(controllerComponents: ControllerComponents, usersDAO: UsersDAO, authTokensDAO: AuthTokensDAO, implicit val ec: ExecutionContext) extends AbstractController(controllerComponents) {
|
class SessionsController @Inject()(
|
||||||
|
controllerComponents: ControllerComponents,
|
||||||
|
usersDAO: UsersDAO,
|
||||||
|
authTokensDAO: AuthTokensDAO,
|
||||||
|
authenticatedAction: AuthenticatedAction)
|
||||||
|
(implicit val ec: ExecutionContext)
|
||||||
|
extends AbstractController(controllerComponents) {
|
||||||
|
import SessionsController.{LoginData, given}
|
||||||
|
|
||||||
def login() = Action.async(parse.json) { implicit request =>
|
def login() = Action.async(parse.json) { implicit request =>
|
||||||
val loginDataResult = request.body.validate[LoginData]
|
val loginDataResult = request.body.validate[LoginData]
|
||||||
loginDataResult.fold(
|
loginDataResult.fold(
|
||||||
errors => {
|
errors => Future.successful(BadRequest(JsError.toJson(errors))),
|
||||||
Future.successful(BadRequest(Json.obj("message" -> JsError.toJson(errors))))
|
|
||||||
},
|
|
||||||
loginData => {
|
loginData => {
|
||||||
usersDAO.authenticate(loginData.username, loginData.password).flatMap { userOption =>
|
usersDAO.authenticate(loginData.username, loginData.password).flatMap { userOption =>
|
||||||
userOption.fold(
|
userOption.fold(
|
||||||
Future.successful(UnprocessableEntity(Json.obj("message" -> "Invalid authentication")))
|
Future.successful(UnprocessableEntity(Json.obj("message" -> "Invalid authentication")))
|
||||||
){ user =>
|
){ user =>
|
||||||
authTokensDAO.createToken(user).map(token => Ok(Json.toJson(token)))
|
authTokensDAO.createToken(user).map { token =>
|
||||||
|
val json = Json.toJsObject(token) ++ Json.obj(
|
||||||
|
"userId" -> user.id.get,
|
||||||
|
"userGid" -> user.gid
|
||||||
|
)
|
||||||
|
Ok(json)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
def logout = Action.andThen(new AuthenticatedAction(authTokensDAO)).async { request =>
|
def logout = authenticatedAction.async { request =>
|
||||||
authTokensDAO.destroy(request.token).map{ _ => Ok("") }
|
authTokensDAO.destroy(request.token).map{ _ => Ok("") }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package controllers.caching
|
||||||
|
|
||||||
|
import play.api.mvc.Results.NotModified
|
||||||
|
import play.api.mvc.{Request, Result}
|
||||||
|
|
||||||
|
import java.io.{ByteArrayOutputStream, DataOutputStream, OutputStream}
|
||||||
|
import java.security.{DigestOutputStream, MessageDigest}
|
||||||
|
import java.util.Base64
|
||||||
|
import scala.concurrent.{ExecutionContext, Future}
|
||||||
|
import scala.util.Using
|
||||||
|
|
||||||
|
trait Etagable[A]:
|
||||||
|
extension (a: A) def etag: String
|
||||||
|
|
||||||
|
|
||||||
|
trait DigestEtagable[A] extends Etagable[A]:
|
||||||
|
private val base64 = Base64.getUrlEncoder.withoutPadding
|
||||||
|
|
||||||
|
protected def writeDigestData(a: A, s: DataOutputStream): Unit
|
||||||
|
|
||||||
|
extension (a: A) {
|
||||||
|
override def etag: String = {
|
||||||
|
val md = MessageDigest.getInstance("SHA-1")
|
||||||
|
Using(DataOutputStream(DigestOutputStream(OutputStream.nullOutputStream(), md))) { dos =>
|
||||||
|
writeDigestData(a, dos)
|
||||||
|
dos.flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
base64.encodeToString(md.digest())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
trait CheckFresh {
|
||||||
|
protected def checkFreshAsync[A, E: Etagable](entity: E)(onStale: => Future[Result])(using request: Request[A], ec: ExecutionContext): Future[Result] = {
|
||||||
|
compareEtag(request, entity).fold(
|
||||||
|
entityEtag => onStale.map(_.withHeaders("etag" -> entityEtag)),
|
||||||
|
requestEtag => Future.successful(notModified(requestEtag))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected def checkFresh[A, E: Etagable](entity: E)(onStale: => Result)(using request: Request[A]): Result = {
|
||||||
|
compareEtag(request, entity).fold(
|
||||||
|
entityEtag => onStale.withHeaders("etag" -> entityEtag),
|
||||||
|
requestEtag => notModified(requestEtag)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private def compareEtag[A, E: Etagable](request: Request[A], entity: E) = {
|
||||||
|
val entityEtag = entity.etag
|
||||||
|
request.headers.get("If-None-Match").filter(e => e == entityEtag) match {
|
||||||
|
case Some(etag) => Right(etag)
|
||||||
|
case None => Left(entityEtag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private def notModified(etag: String) = NotModified.withHeaders("etag" -> etag)
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package controllers.devel
|
||||||
|
|
||||||
|
import dao.UsersDAO
|
||||||
|
import play.api.libs.functional.syntax.toFunctionalBuilderOps
|
||||||
|
import play.api.libs.json.{JsError, JsPath, Json, Reads}
|
||||||
|
import play.api.mvc.*
|
||||||
|
|
||||||
|
import javax.inject.Inject
|
||||||
|
import scala.concurrent.{ExecutionContext, Future}
|
||||||
|
|
||||||
|
class DevActionsController @Inject()(val controllerComponents: ControllerComponents,
|
||||||
|
val usersDAO: UsersDAO)
|
||||||
|
(implicit val ec: ExecutionContext)
|
||||||
|
extends BaseController {
|
||||||
|
|
||||||
|
private case class UserData(username: String, password: String)
|
||||||
|
private implicit val userDataReads: Reads[UserData] = (
|
||||||
|
(JsPath \ "username").read[String] and (JsPath \ "password").read[String]
|
||||||
|
)(UserData.apply)
|
||||||
|
|
||||||
|
def createUser = Action.async(parse.json) { implicit request =>
|
||||||
|
request.body.validate[UserData].fold(
|
||||||
|
errors => Future.successful(BadRequest(JsError.toJson(errors))),
|
||||||
|
userData => usersDAO.create(userData.username, userData.password)
|
||||||
|
.map {
|
||||||
|
case Left(UsersDAO.UsernameTaken) => BadRequest(Json.obj("error" -> "Username exists"))
|
||||||
|
case Left(_ex) => BadRequest(Json.obj("error" -> "User creation error"))
|
||||||
|
case Right(user) => Ok(Json.toJson(user))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package controllers.devel
|
||||||
|
|
||||||
|
import play.api.Environment
|
||||||
|
import play.api.Mode.Dev
|
||||||
|
import play.api.mvc.*
|
||||||
|
import play.api.routing.Router.Routes
|
||||||
|
import play.api.routing.SimpleRouter
|
||||||
|
import play.api.routing.sird.{POST, UrlContext}
|
||||||
|
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class DevRouter @Inject()(environment: Environment, controller: DevActionsController) extends SimpleRouter {
|
||||||
|
override def routes: Routes = {
|
||||||
|
if (environment.mode == Dev) {
|
||||||
|
devRoutes
|
||||||
|
} else {
|
||||||
|
PartialFunction.empty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val devRoutes: PartialFunction[RequestHeader, Handler] = {
|
||||||
|
case POST(p"/users") => controller.createUser()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@ class LoggingFilter @Inject()(implicit val mat: Materializer, ec: ExecutionConte
|
|||||||
val endTime = System.currentTimeMillis()
|
val endTime = System.currentTimeMillis()
|
||||||
val requestTime = endTime - startTime
|
val requestTime = endTime - startTime
|
||||||
|
|
||||||
logger.info(s"${requestHeader.method} ${requestHeader.uri} ${result.header.status} $requestTime")
|
logger.info(s"${requestHeader.method} ${requestHeader.uri} ${result.header.status} ${requestTime}ms")
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,35 @@
|
|||||||
package dao
|
package dao
|
||||||
|
|
||||||
import models.{Auction, AuctionStatus}
|
|
||||||
import schemas.Auctions
|
|
||||||
import schemas.auctionStatusColumnType
|
|
||||||
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
|
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
|
||||||
import slick.jdbc.PostgresProfile
|
import slick.jdbc.PostgresProfile
|
||||||
import slick.jdbc.PostgresProfile.api.*
|
import slick.jdbc.PostgresProfile.api.*
|
||||||
|
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import scala.concurrent.{ExecutionContext, Future}
|
import scala.concurrent.{ExecutionContext, Future}
|
||||||
import scala.util.Random
|
import scala.util.Random
|
||||||
|
|
||||||
|
import models.{Auction, AuctionStatus, User}
|
||||||
|
import schemas.Auctions
|
||||||
|
import schemas.given
|
||||||
|
|
||||||
class AuctionsDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)(implicit val ec: ExecutionContext)
|
class AuctionsDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)(implicit val ec: ExecutionContext)
|
||||||
extends HasDatabaseConfigProvider[PostgresProfile] {
|
extends HasDatabaseConfigProvider[PostgresProfile] {
|
||||||
|
|
||||||
def create(name: String, startingPrice: BigDecimal, author: models.User): Future[Auction] = {
|
def list_available(user: User): Future[Seq[Auction]] = {
|
||||||
|
db.run(availableScope(user).result)
|
||||||
|
}
|
||||||
|
|
||||||
|
def showAvailable(user: User, id: Long): Future[Option[Auction]] = {
|
||||||
|
db.run {
|
||||||
|
availableScope(user).filter(_.id === id).take(1).result.headOption
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def list_created_by(user: User): Future[Seq[Auction]] = {
|
||||||
|
db.run(Auctions.filter(_.authorId === user.id).result)
|
||||||
|
}
|
||||||
|
|
||||||
|
def create(name: String, startingPrice: BigDecimal, author: User): Future[Auction] = {
|
||||||
val auction = Auction(
|
val auction = Auction(
|
||||||
id = None, name = name,
|
id = None, name = name,
|
||||||
gid = Random.alphanumeric.take(24).mkString,
|
gid = Random.alphanumeric.take(24).mkString,
|
||||||
@@ -33,8 +47,25 @@ class AuctionsDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvid
|
|||||||
db.run((Auctions returning Auctions) += auction)
|
db.run((Auctions returning Auctions) += auction)
|
||||||
}
|
}
|
||||||
|
|
||||||
def changeStatus(auctionId: Int, status: AuctionStatus): Future[Boolean] = {
|
def changeStatus(auctionId: Long, status: AuctionStatus): Future[Boolean] = {
|
||||||
db.run(Auctions.filter(_.id === auctionId).map(_.status).update(status))
|
db.run(Auctions.filter(_.id === auctionId).map(_.status).update(status))
|
||||||
.map(_ == 1)
|
.map(_ == 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def find_by_gid(gid: String): Future[Option[Auction]] = {
|
||||||
|
db.run(Auctions.filter(_.gid === gid).take(1).result.headOption)
|
||||||
|
}
|
||||||
|
|
||||||
|
def newBid(auction: Auction, bidder: User, bid: BigDecimal, timestamp: Instant): Future[Option[Auction]] = {
|
||||||
|
db.run {
|
||||||
|
Auctions.filter(_.id === auction.id.get).map{ it => (it.currentBid, it.currentBidAt, it.currentBidderId) }
|
||||||
|
.update((Some(bid), Some(timestamp), Some(bidder.id.get)))
|
||||||
|
.andThen { Auctions.filter(_.id === auction.id.get).result.headOption }
|
||||||
|
.transactionally
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private def availableScope(user: User) = {
|
||||||
|
Auctions.filter(row => (row.status === AuctionStatus.Ready) && (row.authorId =!= user.id.get))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+57
-5
@@ -1,5 +1,6 @@
|
|||||||
package dao
|
package dao
|
||||||
|
|
||||||
|
import dao.UsersDAO.{UnexpectedError, UsernameTaken}
|
||||||
import de.mkammerer.argon2.Argon2Factory
|
import de.mkammerer.argon2.Argon2Factory
|
||||||
import execution.CryptoExecutionContext
|
import execution.CryptoExecutionContext
|
||||||
import jakarta.inject.Inject
|
import jakarta.inject.Inject
|
||||||
@@ -9,23 +10,74 @@ import slick.jdbc.PostgresProfile.api.*
|
|||||||
|
|
||||||
import scala.concurrent.{ExecutionContext, Future}
|
import scala.concurrent.{ExecutionContext, Future}
|
||||||
import models.User
|
import models.User
|
||||||
|
import org.postgresql.util.{PSQLException, PSQLState}
|
||||||
import schemas.Users
|
import schemas.Users
|
||||||
|
|
||||||
class UsersDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvider, private val cryptoEC: CryptoExecutionContext)(implicit val ec: ExecutionContext)
|
import java.nio.charset.StandardCharsets.UTF_8
|
||||||
|
import scala.util.Random
|
||||||
|
|
||||||
|
class UsersDAO @Inject()(protected val dbConfigProvider: DatabaseConfigProvider, private val cryptoEC: CryptoExecutionContext)
|
||||||
|
(using ec: ExecutionContext)
|
||||||
extends HasDatabaseConfigProvider[PostgresProfile] {
|
extends HasDatabaseConfigProvider[PostgresProfile] {
|
||||||
private val argon2 = Argon2Factory.create()
|
private val argon2 = Argon2Factory.create()
|
||||||
|
|
||||||
|
private object ArgonParameters {
|
||||||
|
val iterations = 3
|
||||||
|
val parallelism = 1
|
||||||
|
val memory = 65536
|
||||||
|
}
|
||||||
|
|
||||||
|
private final val dummyDigest = "$argon2i$v=19$m=65536,t=3,p=1$EB9QibLwzqkO7ne2bJoU/Q$ADkdyweXKylJORvWXbBVekURYaazkW/OplTq2wdN1So"
|
||||||
|
|
||||||
def authenticate(username: String, password: String): Future[Option[User]] = {
|
def authenticate(username: String, password: String): Future[Option[User]] = {
|
||||||
val query = Users.filter(_.username === username).take(1).result.headOption
|
val query = Users.filter(_.username === username).take(1).result.headOption
|
||||||
|
|
||||||
val userFuture = db.run(query)
|
val userFuture = db.run(query)
|
||||||
|
|
||||||
userFuture.map { userOption =>
|
userFuture.map { userOption =>
|
||||||
for {
|
for {
|
||||||
user <- userOption
|
user <- userOption
|
||||||
digest <- user.passwordDigest
|
digest <- user.passwordDigest
|
||||||
if argon2.verify(digest, password.getBytes("UTF-8"))
|
} yield (user, digest)
|
||||||
} yield user
|
}.map {
|
||||||
}(using cryptoEC)
|
case Some((user, digest)) => (Some(user), digest)
|
||||||
|
case None => (None, dummyDigest)
|
||||||
|
}.flatMap { (userOpt, digest) =>
|
||||||
|
for {
|
||||||
|
validHash <- Future(argon2.verify(digest, password.getBytes(UTF_8)))(using cryptoEC)
|
||||||
|
opt = userOpt.flatMap(u => if validHash then Some(u) else None)
|
||||||
|
} yield opt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def create(username: String, password: String): Future[Either[UsersDAO.RegistrationError, User]] = {
|
||||||
|
Future(argonHash(password))(using cryptoEC)
|
||||||
|
.flatMap{ passwordDigest =>
|
||||||
|
val gid = Random.alphanumeric.take(12).mkString
|
||||||
|
val user = User(
|
||||||
|
id = None,
|
||||||
|
gid = gid,
|
||||||
|
username = username,
|
||||||
|
passwordDigest = Some(passwordDigest)
|
||||||
|
)
|
||||||
|
|
||||||
|
db.run((Users returning Users) += user).map(Right(_))
|
||||||
|
}.recover {
|
||||||
|
case ex: PSQLException if ex.getSQLState == PSQLState.UNIQUE_VIOLATION.getState =>
|
||||||
|
Left(UsernameTaken)
|
||||||
|
case ex: Throwable =>
|
||||||
|
Left(UnexpectedError(ex))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def find_by_gid(gid: String): Future[Option[User]] = {
|
||||||
|
db.run(Users.filter(_.gid === gid).take(1).result.headOption)
|
||||||
|
}
|
||||||
|
|
||||||
|
private def argonHash(data: String) = argon2.hash(ArgonParameters.iterations, ArgonParameters.memory, ArgonParameters.parallelism, data.getBytes(UTF_8))
|
||||||
|
}
|
||||||
|
|
||||||
|
object UsersDAO {
|
||||||
|
sealed trait RegistrationError
|
||||||
|
case object UsernameTaken extends RegistrationError
|
||||||
|
case class UnexpectedError(cause: Throwable) extends RegistrationError
|
||||||
|
}
|
||||||
+22
-14
@@ -1,12 +1,26 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import play.api.libs.json.{JsValue, Json, Writes}
|
import play.api.libs.json.{JsString, JsValue, Json, OWrites, Writes}
|
||||||
|
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
|
|
||||||
case class User(id: Option[Int], username: String, gid: String, passwordDigest: Option[String])
|
case class User(id: Option[Long], username: String, gid: String, passwordDigest: Option[String])
|
||||||
|
object User {
|
||||||
|
given writes: Writes[User] = (o: User) => Json.obj(
|
||||||
|
"gid" -> o.gid,
|
||||||
|
"username" -> o.username
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
case class AuthToken(id: Option[Int], userId: Int, token: String, createdAt: Instant, expiresAt: Instant)
|
case class AuthToken(id: Option[Long], userId: Long, token: String, createdAt: Instant, expiresAt: Instant)
|
||||||
|
object AuthToken {
|
||||||
|
given writes: OWrites[AuthToken] = (a: AuthToken) => Json.obj(
|
||||||
|
"token" -> a.token,
|
||||||
|
"userId" -> a.userId,
|
||||||
|
"createdAt" -> a.createdAt,
|
||||||
|
"expiresAt" -> a.expiresAt
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
enum AuctionStatus(val value: Short) {
|
enum AuctionStatus(val value: Short) {
|
||||||
case Created extends AuctionStatus(0)
|
case Created extends AuctionStatus(0)
|
||||||
@@ -16,21 +30,15 @@ enum AuctionStatus(val value: Short) {
|
|||||||
|
|
||||||
object AuctionStatus {
|
object AuctionStatus {
|
||||||
lazy val byValue: Map[Short, AuctionStatus] = Map.from(AuctionStatus.values.map{it => (it.value, it)})
|
lazy val byValue: Map[Short, AuctionStatus] = Map.from(AuctionStatus.values.map{it => (it.value, it)})
|
||||||
|
|
||||||
|
implicit val writes: Writes[AuctionStatus] = (o: AuctionStatus) => JsString(o.toString)
|
||||||
}
|
}
|
||||||
|
|
||||||
case class Auction(id: Option[Int], name: String, status: AuctionStatus, gid: String, authorId: Int,
|
case class Auction(id: Option[Long], name: String, status: AuctionStatus, gid: String, authorId: Long,
|
||||||
startingPrice: BigDecimal, currentBid: Option[BigDecimal],
|
startingPrice: BigDecimal, currentBid: Option[BigDecimal],
|
||||||
currentBidderId: Option[Int], currentBidAt: Option[Instant],
|
currentBidderId: Option[Long], currentBidAt: Option[Instant],
|
||||||
createdAt: Instant)
|
createdAt: Instant)
|
||||||
|
|
||||||
object Auction {
|
object Auction {
|
||||||
implicit val writes: Writes[Auction] = (a: Auction) => Json.obj(
|
implicit val writes: OWrites[Auction] = Json.writes[Auction]
|
||||||
"id" -> a.id,
|
|
||||||
"name" -> a.name,
|
|
||||||
"gid" -> a.gid,
|
|
||||||
"starting_price" -> a.startingPrice,
|
|
||||||
"current_bid" -> a.currentBid,
|
|
||||||
"current_bidder_id" -> a.currentBidderId,
|
|
||||||
"current_bid_at" -> a.currentBidAt
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
@@ -8,8 +8,10 @@ import org.apache.pekko.actor.typed.{ActorRef, Behavior}
|
|||||||
import org.apache.pekko.actor.typed.scaladsl.Behaviors
|
import org.apache.pekko.actor.typed.scaladsl.Behaviors
|
||||||
import play.api.Logging
|
import play.api.Logging
|
||||||
import play.api.libs.concurrent.ActorModule
|
import play.api.libs.concurrent.ActorModule
|
||||||
|
import play.api.libs.json._
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
|
import scala.util.matching.Regex
|
||||||
|
|
||||||
object MessageProcessorActor extends ActorModule with Logging {
|
object MessageProcessorActor extends ActorModule with Logging {
|
||||||
sealed trait Command
|
sealed trait Command
|
||||||
@@ -18,14 +20,16 @@ object MessageProcessorActor extends ActorModule with Logging {
|
|||||||
|
|
||||||
override type Message = Command
|
override type Message = Command
|
||||||
|
|
||||||
|
private final val newBidPattern: Regex = """notify/new_bid/(\w+)""".r
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class Reg @Inject()(private val mqttMessageProcessorActor: ActorRef[Command])
|
class Reg @Inject()(private val mqttMessageProcessorActor: ActorRef[Command])
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
def create(client: Mqtt5AsyncClient): Behavior[Command] = Behaviors.setup { context =>
|
def create(client: Mqtt5AsyncClient, newBidService: services.auctions.NewBid): Behavior[Command] = Behaviors.setup { context =>
|
||||||
client.connect().whenComplete { (_action, _throwable) =>
|
client.connect().whenComplete { (_action, _throwable) =>
|
||||||
client.subscribeWith()
|
client.subscribeWith()
|
||||||
.topicFilter("test/+")
|
.topicFilter("notify/new_bid/+")
|
||||||
.callback{ (publish) =>
|
.callback{ (publish) =>
|
||||||
context.self ! MqttMessageReceived(publish)
|
context.self ! MqttMessageReceived(publish)
|
||||||
}
|
}
|
||||||
@@ -37,6 +41,17 @@ object MessageProcessorActor extends ActorModule with Logging {
|
|||||||
val topic = publish.getTopic
|
val topic = publish.getTopic
|
||||||
val payload = String(publish.getPayloadAsBytes, StandardCharsets.UTF_8)
|
val payload = String(publish.getPayloadAsBytes, StandardCharsets.UTF_8)
|
||||||
logger.info(s"Received message $topic $payload")
|
logger.info(s"Received message $topic $payload")
|
||||||
|
|
||||||
|
topic.toString match {
|
||||||
|
case newBidPattern(auctionGid) =>
|
||||||
|
Json.parse(payload).validate[services.auctions.NewBid.Request] match {
|
||||||
|
case JsSuccess(request, _) => newBidService.perform(auctionGid, request)
|
||||||
|
case JsError(errors) =>
|
||||||
|
logger.warn(errors.toString())
|
||||||
|
}
|
||||||
|
println(s"New bid $auctionGid")
|
||||||
|
}
|
||||||
|
|
||||||
Behaviors.same
|
Behaviors.same
|
||||||
case Stop => Behaviors.stopped{ () =>
|
case Stop => Behaviors.stopped{ () =>
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import slick.model.ForeignKeyAction.{Cascade, Restrict}
|
|||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
|
|
||||||
class UsersTable(tag: Tag) extends Table[User](tag, "users") {
|
class UsersTable(tag: Tag) extends Table[User](tag, "users") {
|
||||||
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
|
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
|
||||||
def gid = column[String]("gid", O.Unique)
|
def gid = column[String]("gid", O.Unique)
|
||||||
def username = column[String]("username", O.Unique)
|
def username = column[String]("username", O.Unique)
|
||||||
def passwordDigest = column[Option[String]]("password_digest")
|
def passwordDigest = column[Option[String]]("password_digest")
|
||||||
@@ -20,8 +20,8 @@ lazy val Users = TableQuery[UsersTable]
|
|||||||
|
|
||||||
class AuthTokensTable(tag: Tag) extends Table[AuthToken](tag, "auth_tokens") {
|
class AuthTokensTable(tag: Tag) extends Table[AuthToken](tag, "auth_tokens") {
|
||||||
|
|
||||||
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
|
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
|
||||||
def userId = column[Int]("user_id")
|
def userId = column[Long]("user_id")
|
||||||
def token = column[String]("token")
|
def token = column[String]("token")
|
||||||
def createdAt = column[Instant]("created_at")
|
def createdAt = column[Instant]("created_at")
|
||||||
def expiresAt = column[Instant]("expires_at")
|
def expiresAt = column[Instant]("expires_at")
|
||||||
@@ -40,14 +40,14 @@ implicit val auctionStatusColumnType: BaseColumnType[AuctionStatus] =
|
|||||||
)
|
)
|
||||||
|
|
||||||
class AuctionsTable(tag: Tag) extends Table[Auction](tag, "auctions") {
|
class AuctionsTable(tag: Tag) extends Table[Auction](tag, "auctions") {
|
||||||
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
|
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
|
||||||
def name = column[String]("name")
|
def name = column[String]("name")
|
||||||
def status = column[AuctionStatus]("status")
|
def status = column[AuctionStatus]("status")
|
||||||
def gid = column[String]("gid", O.Unique)
|
def gid = column[String]("gid", O.Unique)
|
||||||
def authorId = column[Int]("author_id")
|
def authorId = column[Long]("author_id")
|
||||||
def startingPrice = column[BigDecimal]("starting_price")
|
def startingPrice = column[BigDecimal]("starting_price", O.SqlType("DECIMAL(20, 3)"))
|
||||||
def currentBid = column[Option[BigDecimal]]("current_bid")
|
def currentBid = column[Option[BigDecimal]]("current_bid", O.SqlType("DECIMAL(20, 3)"))
|
||||||
def currentBidderId = column[Option[Int]]("current_bidder_id")
|
def currentBidderId = column[Option[Long]]("current_bidder_id")
|
||||||
def currentBidAt = column[Option[Instant]]("current_bid_at")
|
def currentBidAt = column[Option[Instant]]("current_bid_at")
|
||||||
def createdAt = column[Instant]("created_at")
|
def createdAt = column[Instant]("created_at")
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package services.auctions
|
||||||
|
|
||||||
|
import dao.{AuctionsDAO, UsersDAO}
|
||||||
|
import models.Auction
|
||||||
|
import play.api.libs.functional.syntax.toFunctionalBuilderOps
|
||||||
|
import play.api.libs.json.{JsPath, JsResult, Reads}
|
||||||
|
|
||||||
|
import java.time.Instant
|
||||||
|
import javax.inject.Inject
|
||||||
|
import scala.concurrent.{ExecutionContext, Future}
|
||||||
|
|
||||||
|
object NewBid {
|
||||||
|
case class Request(bid: BigDecimal, bidderGid: String, timestamp: Instant)
|
||||||
|
|
||||||
|
given Reads[Request] = (
|
||||||
|
(JsPath \ "bid").read[BigDecimal] and
|
||||||
|
(JsPath \ "bidder_gid").read[String] and
|
||||||
|
(JsPath \ "timestamp").read[Instant]
|
||||||
|
)(Request.apply)
|
||||||
|
}
|
||||||
|
|
||||||
|
class NewBid @Inject()(auctionsDao: AuctionsDAO, usersDAO: UsersDAO)(using ec: ExecutionContext) {
|
||||||
|
import NewBid.Request
|
||||||
|
|
||||||
|
def perform(auctionGid: String, request: Request): Future[Option[Auction]] = {
|
||||||
|
val dataFuture = for {
|
||||||
|
auction <- auctionsDao.find_by_gid(auctionGid)
|
||||||
|
bidder <- usersDAO.find_by_gid(request.bidderGid)
|
||||||
|
} yield (auction, bidder)
|
||||||
|
|
||||||
|
dataFuture.flatMap {
|
||||||
|
case (Some(auction), Some(bidder)) =>
|
||||||
|
auctionsDao.newBid(auction, bidder, bid = request.bid, timestamp = request.timestamp)
|
||||||
|
case (_, _) =>
|
||||||
|
// One of them (or both) aren't found
|
||||||
|
Future.successful(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
name: 'neon-vertigo-api'
|
name: 'neon-vertigo-api'
|
||||||
|
include:
|
||||||
|
- ../shared/compose.shared.yml
|
||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
image: "postgres:18"
|
image: "postgres:18"
|
||||||
@@ -6,3 +8,8 @@ services:
|
|||||||
- db.env
|
- db.env
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
|
volumes:
|
||||||
|
- "db:/var/lib/postgresql"
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
db:
|
||||||
@@ -2,9 +2,14 @@
|
|||||||
|
|
||||||
play.http.filters=controllers.filters.Filters
|
play.http.filters=controllers.filters.Filters
|
||||||
|
|
||||||
|
play.filters.enabled += "play.filters.cors.CORSFilter"
|
||||||
play.filters.csrf.header.bypassHeaders {
|
play.filters.csrf.header.bypassHeaders {
|
||||||
X-Requested-With = "*"
|
X-Requested-With = "*"
|
||||||
}
|
}
|
||||||
|
play.filters.cors {
|
||||||
|
allowedOrigins = ["http://localhost:4321"]
|
||||||
|
exposedHeaders = ["ETag"]
|
||||||
|
}
|
||||||
|
|
||||||
play.modules.enabled += "mqttClient.Module"
|
play.modules.enabled += "mqttClient.Module"
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,13 @@ GET / controllers.HomeController.index()
|
|||||||
POST /login controllers.authentication.SessionsController.login()
|
POST /login controllers.authentication.SessionsController.login()
|
||||||
DELETE /logout controllers.authentication.SessionsController.logout()
|
DELETE /logout controllers.authentication.SessionsController.logout()
|
||||||
|
|
||||||
|
GET /user_data controllers.HomeController.userData()
|
||||||
|
|
||||||
# Map static resources from the /public folder to the /assets URL path
|
# Map static resources from the /public folder to the /assets URL path
|
||||||
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
|
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
|
||||||
|
|
||||||
|
GET /auctions controllers.AuctionsController.index()
|
||||||
|
GET /auctions/:id controllers.AuctionsController.show(id: Long)
|
||||||
POST /auctions controllers.AuctionsController.create()
|
POST /auctions controllers.AuctionsController.create()
|
||||||
|
|
||||||
|
-> /-devel controllers.devel.DevRouter
|
||||||
Reference in New Issue
Block a user