All Downloads are FREE. Search and download functionalities are using the official Maven repository.

izumi.idealingua.runtime.rpc.http4s.IRTContextServices.scala Maven / Gradle / Ivy

The newest version!
package izumi.idealingua.runtime.rpc.http4s

import izumi.functional.bio.{IO2, Monad2}
import izumi.idealingua.runtime.rpc.http4s.ws.WsContextSessions
import izumi.idealingua.runtime.rpc.{IRTServerMiddleware, IRTServerMultiplexor}
import izumi.reflect.Tag

trait IRTContextServices[F[+_, +_], AuthCtx, RequestCtx, WsCtx] {
  def name: String
  def authenticator: IRTAuthenticator[F, AuthCtx, RequestCtx]
  def serverMuxer: IRTServerMultiplexor[F, RequestCtx]
  def middlewares: Set[IRTServerMiddleware[F, RequestCtx]]
  def wsSessions: WsContextSessions[F, RequestCtx, WsCtx]

  def authorizedMuxer(implicit io2: IO2[F]): IRTServerMultiplexor[F, AuthCtx] = {
    val withMiddlewares: IRTServerMultiplexor[F, RequestCtx] = middlewares.toList.sortBy(_.priority).foldLeft(serverMuxer) {
      case (muxer, middleware) => muxer.wrap(middleware)
    }
    val authorized: IRTServerMultiplexor[F, AuthCtx] = withMiddlewares.contramap {
      case (authCtx, body, methodId) => authenticator.authenticate(authCtx, Some(body), Some(methodId))
    }
    authorized
  }
  def authorizedWsSessions(implicit M: Monad2[F]): WsContextSessions[F, AuthCtx, WsCtx] = {
    val authorized: WsContextSessions[F, AuthCtx, WsCtx] = wsSessions.contramap {
      authCtx =>
        authenticator.authenticate(authCtx, None, None)
    }
    authorized
  }
}

object IRTContextServices {
  type AnyContext[F[+_, +_], AuthCtx]               = IRTContextServices[F, AuthCtx, ?, ?]
  type AnyWsContext[F[+_, +_], AuthCtx, RequestCtx] = IRTContextServices[F, AuthCtx, RequestCtx, ?]

  def tagged[F[+_, +_], AuthCtx, RequestCtx: Tag, WsCtx: Tag](
    authenticator: IRTAuthenticator[F, AuthCtx, RequestCtx],
    serverMuxer: IRTServerMultiplexor[F, RequestCtx],
    middlewares: Set[IRTServerMiddleware[F, RequestCtx]],
    wsSessions: WsContextSessions[F, RequestCtx, WsCtx],
  ): Tagged[F, AuthCtx, RequestCtx, WsCtx] = Tagged(authenticator, serverMuxer, middlewares, wsSessions)

  def named[F[+_, +_], AuthCtx, RequestCtx, WsCtx](
    name: String
  )(authenticator: IRTAuthenticator[F, AuthCtx, RequestCtx],
    serverMuxer: IRTServerMultiplexor[F, RequestCtx],
    middlewares: Set[IRTServerMiddleware[F, RequestCtx]],
    wsSessions: WsContextSessions[F, RequestCtx, WsCtx],
  ): Named[F, AuthCtx, RequestCtx, WsCtx] = Named(name, authenticator, serverMuxer, middlewares, wsSessions)

  final case class Named[F[+_, +_], AuthCtx, RequestCtx, WsCtx](
    name: String,
    authenticator: IRTAuthenticator[F, AuthCtx, RequestCtx],
    serverMuxer: IRTServerMultiplexor[F, RequestCtx],
    middlewares: Set[IRTServerMiddleware[F, RequestCtx]],
    wsSessions: WsContextSessions[F, RequestCtx, WsCtx],
  ) extends IRTContextServices[F, AuthCtx, RequestCtx, WsCtx]

  final case class Tagged[F[+_, +_], AuthCtx, RequestCtx: Tag, WsCtx: Tag](
    authenticator: IRTAuthenticator[F, AuthCtx, RequestCtx],
    serverMuxer: IRTServerMultiplexor[F, RequestCtx],
    middlewares: Set[IRTServerMiddleware[F, RequestCtx]],
    wsSessions: WsContextSessions[F, RequestCtx, WsCtx],
  ) extends IRTContextServices[F, AuthCtx, RequestCtx, WsCtx] {
    override def name: String = s"${Tag[RequestCtx].tag}:${Tag[WsCtx].tag}"
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy