stubbed auth with tagless final
This commit is contained in:
parent
64267a5f67
commit
0a721b135f
|
@ -1,6 +1,7 @@
|
||||||
package industries.sunshine.planningpoker
|
package industries.sunshine.planningpoker
|
||||||
|
|
||||||
import cats.effect._
|
import cats.effect._
|
||||||
|
import cats.syntax._
|
||||||
import cats.data.Kleisli
|
import cats.data.Kleisli
|
||||||
import cats.data.OptionT
|
import cats.data.OptionT
|
||||||
import org.http4s.Request
|
import org.http4s.Request
|
||||||
|
@ -9,32 +10,34 @@ import java.util.UUID
|
||||||
import industries.sunshine.planningpoker.common.Models.{RoomID, Room}
|
import industries.sunshine.planningpoker.common.Models.{RoomID, Room}
|
||||||
import org.http4s.ResponseCookie
|
import org.http4s.ResponseCookie
|
||||||
|
|
||||||
trait Auth {
|
trait Auth[F[_]] {
|
||||||
|
|
||||||
/** for middleware that converts sessionId into PlayerId
|
/** for middleware that converts sessionId into PlayerId
|
||||||
*/
|
*/
|
||||||
def authUser: Kleisli[[A] =>> cats.data.OptionT[cats.effect.IO, A], Request[
|
def authUser
|
||||||
cats.effect.IO
|
: Kleisli[[A] =>> cats.data.OptionT[F, A], Request[F], (PlayerID, RoomID)]
|
||||||
], (PlayerID, RoomID)]
|
|
||||||
|
|
||||||
/** Get sessionId for accessing a room
|
/** Get sessionId for accessing a room
|
||||||
* @param roomPassword
|
* @param roomPassword
|
||||||
* \- requirement to get access to the room
|
* \- 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,
|
roomName: String,
|
||||||
roomPassword: String,
|
roomPassword: String,
|
||||||
nickName: String
|
nickName: String
|
||||||
): IO[Either[Unit, ResponseCookie]]
|
): F[Either[Unit, ResponseCookie]]
|
||||||
|
|
||||||
def leaveRoom(
|
def deleteSession(
|
||||||
sessionId: Long
|
sessionId: Long
|
||||||
): IO[Unit]
|
): F[Unit]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
object Auth {
|
object Auth {
|
||||||
def pureBadStub = new Auth {
|
def pureBadStub = new Auth[IO] {
|
||||||
override def authUser =
|
override def authUser =
|
||||||
Kleisli((req: Request[IO]) =>
|
Kleisli((req: Request[IO]) =>
|
||||||
OptionT.liftF(
|
OptionT.liftF(
|
||||||
|
@ -43,7 +46,7 @@ object Auth {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
override def accessRoom(
|
override def joinRoom(
|
||||||
roomName: String,
|
roomName: String,
|
||||||
roomPassword: String,
|
roomPassword: String,
|
||||||
nickName: String
|
nickName: String
|
||||||
|
@ -54,40 +57,42 @@ object Auth {
|
||||||
)
|
)
|
||||||
) >> IO.pure(Right(ResponseCookie("authcookie", "1")))
|
) >> 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")
|
IO(s"got request to leave for $sessionId")
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionsMap = Map[Long, (RoomID, PlayerID)]
|
type SessionsMap = Map[Long, (RoomID, PlayerID)]
|
||||||
val sessionsRef =
|
class SimpleAuth[F[_]: Sync](
|
||||||
Ref.of[IO, SessionsMap](Map(1L -> (RoomID("testroom"), PlayerID(1L))))
|
sessions: Ref[F, SessionsMap],
|
||||||
val roomsRef =
|
roomService: RoomService[F]
|
||||||
Ref.of[IO, Map[RoomID, Room]](Map(RoomID("testroom") -> Room.testRoom))
|
) extends Auth[F] {
|
||||||
|
|
||||||
def apply(): IO[Auth] =
|
override def joinRoom(
|
||||||
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,
|
roomName: String,
|
||||||
roomPassword: String,
|
roomPassword: String,
|
||||||
nickName: String
|
nickName: String
|
||||||
): IO[Either[Unit, ResponseCookie]] = {
|
): F[Either[Unit, ResponseCookie]] = {
|
||||||
|
|
||||||
???
|
???
|
||||||
}
|
}
|
||||||
|
|
||||||
override def leaveRoom(sessionId: Long): IO[Unit] = ???
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import org.http4s.server.AuthMiddleware.apply
|
||||||
import org.http4s.server.AuthMiddleware
|
import org.http4s.server.AuthMiddleware
|
||||||
|
|
||||||
object MyHttpService {
|
object MyHttpService {
|
||||||
def create(auth: Auth)(
|
def create(auth: Auth[IO])(
|
||||||
wsb: WebSocketBuilder[cats.effect.IO]
|
wsb: WebSocketBuilder[cats.effect.IO]
|
||||||
): HttpApp[cats.effect.IO] = {
|
): HttpApp[cats.effect.IO] = {
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ object MyHttpService {
|
||||||
case req @ POST -> Root / "login" => {
|
case req @ POST -> Root / "login" => {
|
||||||
for {
|
for {
|
||||||
data <- req.as[Requests.LogIn]
|
data <- req.as[Requests.LogIn]
|
||||||
authResult <- auth.accessRoom(
|
authResult <- auth.joinRoom(
|
||||||
data.roomName,
|
data.roomName,
|
||||||
data.password,
|
data.password,
|
||||||
data.nickname
|
data.nickname
|
||||||
|
|
Loading…
Reference in New Issue