94 lines
2.3 KiB
Scala
94 lines
2.3 KiB
Scala
package industries.sunshine.planningpoker
|
|
|
|
import cats.effect._
|
|
import cats.data.Kleisli
|
|
import cats.data.OptionT
|
|
import org.http4s.Request
|
|
import industries.sunshine.planningpoker.common.Models.PlayerID
|
|
import java.util.UUID
|
|
import industries.sunshine.planningpoker.common.Models.{RoomID, Room}
|
|
import org.http4s.ResponseCookie
|
|
|
|
trait Auth {
|
|
|
|
/** for middleware that converts sessionId into PlayerId
|
|
*/
|
|
def authUser: Kleisli[[A] =>> cats.data.OptionT[cats.effect.IO, A], Request[
|
|
cats.effect.IO
|
|
], (PlayerID, RoomID)]
|
|
|
|
/** Get sessionId for accessing a room
|
|
* @param roomPassword
|
|
* \- requirement to get access to the room
|
|
*/
|
|
def accessRoom(
|
|
roomName: String,
|
|
roomPassword: String,
|
|
nickName: String
|
|
): IO[Either[Unit, ResponseCookie]]
|
|
|
|
def leaveRoom(
|
|
sessionId: Long
|
|
): IO[Unit]
|
|
|
|
}
|
|
|
|
object Auth {
|
|
def pureBadStub = new Auth {
|
|
override def authUser =
|
|
Kleisli((req: Request[IO]) =>
|
|
OptionT.liftF(
|
|
IO(println(s"authUser: $req")) >> IO(
|
|
PlayerID(14) -> RoomID("testroom")
|
|
)
|
|
)
|
|
)
|
|
override def accessRoom(
|
|
roomName: String,
|
|
roomPassword: String,
|
|
nickName: String
|
|
) =
|
|
IO(
|
|
println(
|
|
s"> access room for $roomName $roomPassword $nickName, to return stub 111"
|
|
)
|
|
) >> IO.pure(Right(ResponseCookie("authcookie", "1")))
|
|
|
|
override def leaveRoom(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))
|
|
|
|
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] = ???
|
|
}
|
|
}
|