From 0a721b135f706164a44d41de786e8fc2e78cd4e9 Mon Sep 17 00:00:00 2001 From: efim Date: Tue, 25 Apr 2023 10:16:11 +0400 Subject: [PATCH] stubbed auth with tagless final --- .../sunshine/planningpoker/Auth.scala | 85 ++++++++++--------- .../planningpoker/MyHttpService.scala | 4 +- 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/backend/src/main/scala/industries/sunshine/planningpoker/Auth.scala b/backend/src/main/scala/industries/sunshine/planningpoker/Auth.scala index e3aa2a9..59d5f9f 100644 --- a/backend/src/main/scala/industries/sunshine/planningpoker/Auth.scala +++ b/backend/src/main/scala/industries/sunshine/planningpoker/Auth.scala @@ -1,6 +1,7 @@ package industries.sunshine.planningpoker import cats.effect._ +import cats.syntax._ import cats.data.Kleisli import cats.data.OptionT import org.http4s.Request @@ -9,32 +10,34 @@ import java.util.UUID import industries.sunshine.planningpoker.common.Models.{RoomID, Room} import org.http4s.ResponseCookie -trait Auth { +trait Auth[F[_]] { /** for middleware that converts sessionId into PlayerId */ - def authUser: Kleisli[[A] =>> cats.data.OptionT[cats.effect.IO, A], Request[ - cats.effect.IO - ], (PlayerID, RoomID)] + def authUser + : Kleisli[[A] =>> cats.data.OptionT[F, A], Request[F], (PlayerID, RoomID)] /** Get sessionId for accessing a room * @param roomPassword * \- requirement to get access to the room + * + * check that room exists, password is valid call to add user to the players + * create session mapping and return cookie */ - def accessRoom( + def joinRoom( roomName: String, roomPassword: String, nickName: String - ): IO[Either[Unit, ResponseCookie]] + ): F[Either[Unit, ResponseCookie]] - def leaveRoom( + def deleteSession( sessionId: Long - ): IO[Unit] + ): F[Unit] } object Auth { - def pureBadStub = new Auth { + def pureBadStub = new Auth[IO] { override def authUser = Kleisli((req: Request[IO]) => OptionT.liftF( @@ -43,7 +46,7 @@ object Auth { ) ) ) - override def accessRoom( + override def joinRoom( roomName: String, roomPassword: String, nickName: String @@ -54,40 +57,42 @@ object Auth { ) ) >> IO.pure(Right(ResponseCookie("authcookie", "1"))) - override def leaveRoom(sessionId: Long): IO[Unit] = + override def deleteSession(sessionId: Long): IO[Unit] = IO(s"got request to leave for $sessionId") } type SessionsMap = Map[Long, (RoomID, PlayerID)] - val sessionsRef = - Ref.of[IO, SessionsMap](Map(1L -> (RoomID("testroom"), PlayerID(1L)))) - val roomsRef = - Ref.of[IO, Map[RoomID, Room]](Map(RoomID("testroom") -> Room.testRoom)) + class SimpleAuth[F[_]: Sync]( + sessions: Ref[F, SessionsMap], + roomService: RoomService[F] + ) extends Auth[F] { - def apply(): IO[Auth] = - for { - store <- sessionsRef - rooms <- roomsRef - } yield new Auth { - - val authcookie = "authcookie" - - override def authUser = Kleisli { (req: Request[IO]) => - { - ??? // oh, this one for when cookie present - } - } - import cats.syntax.either._ - import cats.syntax.option._ - override def accessRoom( - roomName: String, - roomPassword: String, - nickName: String - ): IO[Either[Unit, ResponseCookie]] = { - - ??? - } - - override def leaveRoom(sessionId: Long): IO[Unit] = ??? + override def joinRoom( + roomName: String, + roomPassword: String, + nickName: String + ): F[Either[Unit, ResponseCookie]] = { + ??? } + + override def authUser: Kleisli[[A] =>> cats.data.OptionT[F, A], Request[ + F + ], (PlayerID, RoomID)] = { + // check authcookie presence, exchange it for playerID ad roomID + ??? + } + + override def deleteSession(sessionId: Long): F[Unit] = { + // i suppose leaving the room should just be authed route & method + ??? + } + } + + import cats.Monad.ops.toAllMonadOps + def make[F[_]: Sync](roomsService: RoomService[F]): F[Auth[F]] = + for { + sessionsMap <- Ref.of[F, SessionsMap]( + Map(1L -> (RoomID("testroom"), PlayerID(1L))) + ) + } yield new SimpleAuth(sessionsMap, roomsService) } diff --git a/backend/src/main/scala/industries/sunshine/planningpoker/MyHttpService.scala b/backend/src/main/scala/industries/sunshine/planningpoker/MyHttpService.scala index 6fa0208..4ed1308 100644 --- a/backend/src/main/scala/industries/sunshine/planningpoker/MyHttpService.scala +++ b/backend/src/main/scala/industries/sunshine/planningpoker/MyHttpService.scala @@ -16,7 +16,7 @@ import org.http4s.server.AuthMiddleware.apply import org.http4s.server.AuthMiddleware object MyHttpService { - def create(auth: Auth)( + def create(auth: Auth[IO])( wsb: WebSocketBuilder[cats.effect.IO] ): HttpApp[cats.effect.IO] = { @@ -54,7 +54,7 @@ object MyHttpService { case req @ POST -> Root / "login" => { for { data <- req.as[Requests.LogIn] - authResult <- auth.accessRoom( + authResult <- auth.joinRoom( data.roomName, data.password, data.nickname