stubbed auth with tagless final

This commit is contained in:
efim 2023-04-25 10:16:11 +04:00
parent 64267a5f67
commit 0a721b135f
2 changed files with 47 additions and 42 deletions

View File

@ -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)
}

View File

@ -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