Skip to content

[feature]: Routes add posibility to define server security logic #1093

@vladpo

Description

@vladpo

Problem Statement

At the moment Routes allows passing an sttp.tapir.Endpoint and to define the server logic, but no way to define the server security logic:

def add[A: Tag, I, E: SafeClassTag, O: Flat](e: Endpoint[A, I, E, O, Any])(
        f: I => O < (Async & Env[A] & Abort[E])
    )(using Frame): Unit < Routes =
        Emit.value(
            Route(
                e.serverSecurityLogic[A, KyoSttpMonad.M](a => Right(a)).serverLogic((a: A) =>
                    (i: I) =>
                        Abort.run[E](Env.run(a)(f(i))).map {
                            case Result.Success(v) => Right(v)
                            case Result.Failure(e) => Left(e)
                            case Result.Panic(ex)  => throw ex
                        }
                )
            )
        )

A scenario is when you want to define a common JWT server security logic for all JWT protected endpoints.

Proposed Solution

Define Routes add with a server security logic

def addSecure[A: Tag, AA: Tag, I, E: SafeClassTag, O: Flat](
      e: Endpoint[A, I, E, O, Any]
  )(g: A => AA < (Async & Abort[E]))(
      f: I => O < (Async & Env[AA] & Abort[E])
  )(using Frame): Unit < Routes = {
    Emit.value(
      Route(
        e
          .serverSecurityLogic[AA, KyoSttpMonad.M]((a: A) =>
            Abort
              .run[E](g(a))
              .map:
                case Result.Success(aa) => aa.asRight[E]
                case Result.Failure(e)  => e.asLeft[AA]
                case Result.Panic(ex)   => throw ex
          )
          .serverLogic((aa: AA) =>
            (i: I) =>
              Abort.run[E](Env.run(aa)(f(i))).map {
                case Result.Success(v) => Right(v)
                case Result.Failure(e) => Left(e)
                case Result.Panic(ex)  => throw ex
              }
          )
      )
    )

Alternative Solutions

No response

Current Workaround

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions