From 1911603c40be24ad0801848677d427d2773910f3 Mon Sep 17 00:00:00 2001 From: Francesco Serra Date: Mon, 8 Jul 2024 18:31:48 +0100 Subject: [PATCH 1/3] NOJIRA update sbt and scala versions --- project/BuildSettings.scala | 2 +- project/build.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/project/BuildSettings.scala b/project/BuildSettings.scala index 65cbf66d..28558d5a 100644 --- a/project/BuildSettings.scala +++ b/project/BuildSettings.scala @@ -6,7 +6,7 @@ import sbt._ object BuildSettings { val env: util.Map[String, String] = System.getenv() val scala212 = "2.12.17" - val scala213 = "2.13.10" + val scala213 = "2.13.14" lazy val basicSettings = Seq( homepage := Some(new URL("https://github.com/mdsol/mauth-jvm-clients")), diff --git a/project/build.properties b/project/build.properties index d240e537..e6dfc954 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1,2 +1,2 @@ # suppress inspection "UnusedProperty" -sbt.version = 1.8.3 +sbt.version =1.10.0 From 6f6b264c34b6b8e25c9ca52d031ab1e7c29f9112 Mon Sep 17 00:00:00 2001 From: Francesco Serra Date: Mon, 8 Jul 2024 18:32:22 +0100 Subject: [PATCH 2/3] NOJIRA put explicit return types to implicit loggers --- .../scala/com/mdsol/mauth/http4s/MAuthMiddlewareSuite.scala | 3 ++- .../com/mdsol/mauth/http4s/MauthPublicKeyProviderSuite.scala | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MAuthMiddlewareSuite.scala b/modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MAuthMiddlewareSuite.scala index d0d11947..c2429371 100644 --- a/modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MAuthMiddlewareSuite.scala +++ b/modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MAuthMiddlewareSuite.scala @@ -14,6 +14,7 @@ import org.http4s._ import org.http4s.{HttpRoutes, Request, Response} import org.http4s.syntax.literals._ import org.http4s.Method._ +import org.typelevel.log4cats._ import java.security.{PublicKey, Security} import java.util.UUID @@ -22,7 +23,7 @@ import org.typelevel.log4cats.noop.NoOpLogger class MAuthMiddlewareSuite extends CatsEffectSuite { - implicit val logger = NoOpLogger[IO] + implicit val logger: Logger[IO] = NoOpLogger[IO] private val route: HttpRoutes[IO] = HttpRoutes.of { case req if req.uri.path === path"/" => diff --git a/modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MauthPublicKeyProviderSuite.scala b/modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MauthPublicKeyProviderSuite.scala index 9cb1d5e4..91231fd9 100644 --- a/modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MauthPublicKeyProviderSuite.scala +++ b/modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MauthPublicKeyProviderSuite.scala @@ -16,12 +16,13 @@ import scalacache.{Cache, Entry} import java.security.PublicKey import cats.implicits._ +import org.typelevel.log4cats.Logger import java.util.UUID class MauthPublicKeyProviderSuite extends CatsEffectSuite { - implicit val logger = NoOpLogger[IO] + implicit val logger: Logger[IO] = NoOpLogger[IO] private val MAUTH_PORT = PortFinder.findFreePort() private val MAUTH_BASE_URL = s"http://localhost:$MAUTH_PORT" private val MAUTH_URL_PATH = "/mauth/v1" From bf4274913a11229e9b1bdc8351b7829c78506fe7 Mon Sep 17 00:00:00 2001 From: Francesco Serra Date: Mon, 8 Jul 2024 18:34:26 +0100 Subject: [PATCH 3/3] NOJIRA make request body strict, * request body is now consumed in one go and stored in memory * prevent occurrences of BodyAlreadyConsumedError --- .../mdsol/mauth/http4s/MAuthMiddleware.scala | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/modules/mauth-authenticator-http4s/src/main/scala/com/mdsol/mauth/http4s/MAuthMiddleware.scala b/modules/mauth-authenticator-http4s/src/main/scala/com/mdsol/mauth/http4s/MAuthMiddleware.scala index 3adf1675..451ec253 100644 --- a/modules/mauth-authenticator-http4s/src/main/scala/com/mdsol/mauth/http4s/MAuthMiddleware.scala +++ b/modules/mauth-authenticator-http4s/src/main/scala/com/mdsol/mauth/http4s/MAuthMiddleware.scala @@ -78,32 +78,32 @@ object MAuthMiddleware { else extractAll(V2) orElse extractAll(V1) - fk(request.as[Array[Byte]].flatMap { byteArray => - authHeaderTimeHeader.flatMap { authCtx: MAuthContext => - val mAuthRequest: MAuthRequest = new MAuthRequest( - authCtx.authHeader, - byteArray, - request.method.name, - authCtx.timeHeader.toString, - request.uri.path.renderString, - request.uri.query.renderString - ) - - // this mimics MAuthDirectives in the akka package - really needed? - val req = if (!authenticator.isV2OnlyAuthenticate) { - mAuthRequest.setXmwsSignature(getHeaderValOrEmpty(V1.authHeaderName)) // dreadful mutating type - mAuthRequest.setXmwsTime(getHeaderValOrEmpty(V1.timeHeaderName)) - mAuthRequest - } else mAuthRequest - - authenticator.authenticate(req)(requestValidationTimeout).map(res => (res, authCtx)) + fk(for { + strictBody <- request.toStrict(none) + byteArray <- strictBody.as[Array[Byte]] + authCtx <- authHeaderTimeHeader + mAuthRequest = new MAuthRequest( + authCtx.authHeader, + byteArray, + request.method.name, + authCtx.timeHeader.toString, + request.uri.path.renderString, + request.uri.query.renderString + ) + req = if (!authenticator.isV2OnlyAuthenticate) { + mAuthRequest.setXmwsSignature(getHeaderValOrEmpty(V1.authHeaderName)) // dreadful mutating type + mAuthRequest.setXmwsTime(getHeaderValOrEmpty(V1.timeHeaderName)) + mAuthRequest + } else mAuthRequest + res <- authenticator.authenticate(req)(requestValidationTimeout).map(res => (res, authCtx)) + } yield res) + .flatMap { case (b, ctx) => + if (b) http(AuthedRequest(ctx, request)) + else logAndReturnDefaultUnauthorizedReq(s"Rejecting request as authentication failed") + } + .recoverWith { case MdsolAuthMissingHeaderRejection(hn) => + logAndReturnDefaultUnauthorizedReq(s"Rejecting request as header $hn missing") } - }).flatMap { case (b, ctx) => - if (b) http(AuthedRequest(ctx, request)) - else logAndReturnDefaultUnauthorizedReq(s"Rejecting request as authentication failed") - }.recoverWith { case MdsolAuthMissingHeaderRejection(hn) => - logAndReturnDefaultUnauthorizedReq(s"Rejecting request as header $hn missing") - } } def httpRoutes[F[_]: Async](requestValidationTimeout: Duration, authenticator: Authenticator[F])(