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

de.bitmarck.bms.secon.http4s.SeconEncryptMiddleware.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 SeconEncryptMiddleware[F[_] : Async : DecryptVerify : SignEncrypt](
                                                                          identityLookup: IdentityLookup[F],
                                                                          certLookup: CertLookup[F],
                                                                          multipart: Boolean = true
                                                                        ) {
  def apply(routes: HttpRoutes[F]): HttpRoutes[F] = HttpRoutes[F] { request =>
    for {
      incomingSeconRequest <- OptionT.liftF(request.as[SeconMessage[F]])
      outgoingSeconRequest = incomingSeconRequest.signAndEncrypt(identityLookup, certLookup, request = true)
      requestEncoder = if (multipart) SeconMessage.seconMultipartEncoder else SeconMessage.seconOctetStreamEncoder
      requestMedia <- OptionT.liftF(requestEncoder.toMedia(outgoingSeconRequest))
      response <- routes(request.withMedia(requestMedia))
      incomingSeconResponse <- OptionT.liftF(response.as[SeconMessage[F]])
      outgoingSeconResponse = incomingSeconResponse.decryptAndVerify(identityLookup, certLookup)
      responseMedia <- OptionT.liftF(incomingSeconRequest.encoder.toMedia(outgoingSeconResponse))
    } yield
      response.withMedia(responseMedia)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy