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

de.bitmarck.bms.secon.http4s.SeconDecryptMiddleware.scala Maven / Gradle / Ivy

There is a newer version: 0.2.2
Show newest version
package de.bitmarck.bms.secon.http4s

import cats.data.OptionT
import cats.effect.Async
import de.bitmarck.bms.secon.fs2.{CertLookup, DecryptVerify, IdentityLookup, SignEncrypt}
import org.http4s.HttpRoutes

class SeconDecryptMiddleware[F[_] : Async : DecryptVerify : SignEncrypt](
                                                                          identityLookup: IdentityLookup[F],
                                                                          certLookup: CertLookup[F]
                                                                        ) {
  def apply(routes: HttpRoutes[F]): HttpRoutes[F] = HttpRoutes[F] { request =>
    for {
      incomingSeconRequest <- OptionT.liftF(request.as[SeconMessage[F]])
      outgoingSeconRequest = incomingSeconRequest.decryptAndVerify(identityLookup, certLookup)
      requestMedia <- OptionT.liftF(SeconMessage.seconOctetStreamEncoder.toMedia(outgoingSeconRequest))
      response <- routes(request.withMedia(requestMedia))
      incomingSeconResponse = outgoingSeconRequest.withMedia(response)
      outgoingSeconResponse = incomingSeconResponse.signAndEncrypt(identityLookup, certLookup, request = false)
      responseMedia <- OptionT.liftF(incomingSeconRequest.encoder.toMedia(outgoingSeconResponse))
    } yield
      response.withMedia(responseMedia)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy