Skip to content

Commit

Permalink
Generalize AuthedRequest to ContextRequest
Browse files Browse the repository at this point in the history
A ContextRequest is defined as a request with some data.

AuthedRequest can be defined as a ContextRequest with auth data
as the context.

AuthedRequest signals that it should only be used for auth stuff, but
ContextRequest opens up possibility to reuse it to other things.
  • Loading branch information
hamnis committed Oct 30, 2019
1 parent 56d97e3 commit 3fd0129
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 8 deletions.
13 changes: 5 additions & 8 deletions core/src/main/scala/org/http4s/AuthedRequest.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.http4s

import cats.{Functor, ~>}
import cats.Functor
import cats.data.Kleisli
import cats.implicits._

final case class AuthedRequest[F[_], A](authInfo: A, req: Request[F]) {
def mapK[G[_]](fk: F ~> G): AuthedRequest[G, A] =
AuthedRequest(authInfo, req.mapK(fk))
}

object AuthedRequest {
def apply[F[_]: Functor, T](
getUser: Request[F] => F[T]): Kleisli[F, Request[F], AuthedRequest[F, T]] =
Kleisli(request => getUser(request).map(user => AuthedRequest(user, request)))
ContextRequest[F, T](getUser)

def apply[F[_], T](context: T, req: Request[F]): AuthedRequest[F, T] =
ContextRequest[F, T](context, req)
}
19 changes: 19 additions & 0 deletions core/src/main/scala/org/http4s/ContextRequest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.http4s

import cats.syntax.functor._
import cats.{Functor, ~>}
import cats.data.Kleisli

final case class ContextRequest[F[_], A](context: A, req: Request[F]) {
def mapK[G[_]](fk: F ~> G): ContextRequest[G, A] =
ContextRequest(authInfo, req.mapK(fk))

def authInfo: A = context

}

object ContextRequest {
def apply[F[_]: Functor, T](
getContext: Request[F] => F[T]): Kleisli[F, Request[F], ContextRequest[F, T]] =
Kleisli(request => getContext(request).map(ctx => ContextRequest(ctx, request)))
}
44 changes: 44 additions & 0 deletions core/src/main/scala/org/http4s/ContextRoutes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.http4s

import cats.data.{Kleisli, OptionT}
import cats.Applicative
import cats.effect.Sync

object ContextRoutes {

/** Lifts a function into an [[ContextRoutes]]. The application of `run`
* is suspended in `F` to permit more efficient combination of
* routes via `SemigroupK`.
*
* @tparam F the effect of the [[ContextRoutes]]
* @tparam T the type of the auth info in the [[ContextRequest]] accepted by the [[ContextRoutes]]
* @param run the function to lift
* @return an [[ContextRoutes]] that wraps `run`
*/
def apply[T, F[_]](run: ContextRequest[F, T] => OptionT[F, Response[F]])(
implicit F: Sync[F]): ContextRoutes[T, F] =
Kleisli(req => OptionT(F.suspend(run(req).value)))

/** Lifts a partial function into an [[AuthedRoutes]]. The application of the
* partial function is suspended in `F` to permit more efficient combination
* of authed services via `SemigroupK`.
*
* @tparam F the base effect of the [[AuthedRoutes]]
* @param pf the partial function to lift
* @return An [[AuthedRoutes]] that returns some [[Response]] in an `OptionT[F, ?]`
* wherever `pf` is defined, an `OptionT.none` wherever it is not
*/
def of[T, F[_]](pf: PartialFunction[ContextRequest[F, T], F[Response[F]]])(
implicit F: Applicative[F]): ContextRoutes[T, F] =
Kleisli(req => pf.andThen(OptionT.liftF(_)).applyOrElse(req, Function.const(OptionT.none)))

/**
* The empty service (all requests fallthrough).
*
* @tparam T - ignored.
* @return
*/
def empty[T, F[_]: Applicative]: ContextRoutes[T, F] =
Kleisli.liftF(OptionT.none)

}
4 changes: 4 additions & 0 deletions core/src/main/scala/org/http4s/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ package object http4s { // scalastyle:ignore
@deprecated("Deprecated in favor of HttpRoutes", "0.19")
type HttpService[F[_]] = HttpRoutes[F]

type AuthedRequest[F[_], T] = ContextRequest[F, T]

/**
* The type parameters need to be in this order to make partial unification
* trigger. See https://github.com/http4s/http4s/issues/1506
Expand All @@ -64,6 +66,8 @@ package object http4s { // scalastyle:ignore
@deprecated("Deprecated in favor of AuthedRoutes", "0.20.1")
type AuthedService[T, F[_]] = AuthedRoutes[T, F]

type ContextRoutes[T, F[_]] = Kleisli[OptionT[F, ?], ContextRequest[F, T], Response[F]]

type Callback[A] = Either[Throwable, A] => Unit

/** A stream of server-sent events */
Expand Down
11 changes: 11 additions & 0 deletions server/src/main/scala/org/http4s/server/ContextMiddleware.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.http4s
package server

import cats.Monad
import cats.data.{Kleisli, OptionT}

object ContextMiddleware {
def apply[F[_]: Monad, T](
getContext: Kleisli[OptionT[F, ?], Request[F], T]): ContextMiddleware[F, T] =
_.compose(Kleisli((r: Request[F]) => getContext(r).map(ContextRequest(_, r))))
}
6 changes: 6 additions & 0 deletions server/src/main/scala/org/http4s/server/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ package object server {
type AuthMiddleware[F[_], T] =
Middleware[OptionT[F, ?], AuthedRequest[F, T], Response[F], Request[F], Response[F]]

/**
* An HTTP middleware that adds a context.
*/
type ContextMiddleware[F[_], T] =
Middleware[OptionT[F, ?], ContextRequest[F, T], Response[F], Request[F], Response[F]]

/**
* Old name for SSLConfig
*/
Expand Down

0 comments on commit 3fd0129

Please sign in to comment.