Skip to content

Commit 62a8187

Browse files
authored
Merge pull request #160 from mdsol/NOJIRA-fix_body_consumption_in_http4s
NOJIRA fix body consumption in http4s
2 parents a6fb9a5 + bf42749 commit 62a8187

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

modules/mauth-authenticator-http4s/src/main/scala/com/mdsol/mauth/http4s/MAuthMiddleware.scala

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,32 @@ object MAuthMiddleware {
7878
else
7979
extractAll(V2) orElse extractAll(V1)
8080

81-
fk(request.as[Array[Byte]].flatMap { byteArray =>
82-
authHeaderTimeHeader.flatMap { authCtx: MAuthContext =>
83-
val mAuthRequest: MAuthRequest = new MAuthRequest(
84-
authCtx.authHeader,
85-
byteArray,
86-
request.method.name,
87-
authCtx.timeHeader.toString,
88-
request.uri.path.renderString,
89-
request.uri.query.renderString
90-
)
91-
92-
// this mimics MAuthDirectives in the akka package - really needed?
93-
val req = if (!authenticator.isV2OnlyAuthenticate) {
94-
mAuthRequest.setXmwsSignature(getHeaderValOrEmpty(V1.authHeaderName)) // dreadful mutating type
95-
mAuthRequest.setXmwsTime(getHeaderValOrEmpty(V1.timeHeaderName))
96-
mAuthRequest
97-
} else mAuthRequest
98-
99-
authenticator.authenticate(req)(requestValidationTimeout).map(res => (res, authCtx))
81+
fk(for {
82+
strictBody <- request.toStrict(none)
83+
byteArray <- strictBody.as[Array[Byte]]
84+
authCtx <- authHeaderTimeHeader
85+
mAuthRequest = new MAuthRequest(
86+
authCtx.authHeader,
87+
byteArray,
88+
request.method.name,
89+
authCtx.timeHeader.toString,
90+
request.uri.path.renderString,
91+
request.uri.query.renderString
92+
)
93+
req = if (!authenticator.isV2OnlyAuthenticate) {
94+
mAuthRequest.setXmwsSignature(getHeaderValOrEmpty(V1.authHeaderName)) // dreadful mutating type
95+
mAuthRequest.setXmwsTime(getHeaderValOrEmpty(V1.timeHeaderName))
96+
mAuthRequest
97+
} else mAuthRequest
98+
res <- authenticator.authenticate(req)(requestValidationTimeout).map(res => (res, authCtx))
99+
} yield res)
100+
.flatMap { case (b, ctx) =>
101+
if (b) http(AuthedRequest(ctx, request))
102+
else logAndReturnDefaultUnauthorizedReq(s"Rejecting request as authentication failed")
103+
}
104+
.recoverWith { case MdsolAuthMissingHeaderRejection(hn) =>
105+
logAndReturnDefaultUnauthorizedReq(s"Rejecting request as header $hn missing")
100106
}
101-
}).flatMap { case (b, ctx) =>
102-
if (b) http(AuthedRequest(ctx, request))
103-
else logAndReturnDefaultUnauthorizedReq(s"Rejecting request as authentication failed")
104-
}.recoverWith { case MdsolAuthMissingHeaderRejection(hn) =>
105-
logAndReturnDefaultUnauthorizedReq(s"Rejecting request as header $hn missing")
106-
}
107107
}
108108

109109
def httpRoutes[F[_]: Async](requestValidationTimeout: Duration, authenticator: Authenticator[F])(

modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MAuthMiddlewareSuite.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.http4s._
1414
import org.http4s.{HttpRoutes, Request, Response}
1515
import org.http4s.syntax.literals._
1616
import org.http4s.Method._
17+
import org.typelevel.log4cats._
1718

1819
import java.security.{PublicKey, Security}
1920
import java.util.UUID
@@ -22,7 +23,7 @@ import org.typelevel.log4cats.noop.NoOpLogger
2223

2324
class MAuthMiddlewareSuite extends CatsEffectSuite {
2425

25-
implicit val logger = NoOpLogger[IO]
26+
implicit val logger: Logger[IO] = NoOpLogger[IO]
2627
private val route: HttpRoutes[IO] =
2728
HttpRoutes.of {
2829
case req if req.uri.path === path"/" =>

modules/mauth-authenticator-http4s/src/test/scala/com/mdsol/mauth/http4s/MauthPublicKeyProviderSuite.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import scalacache.{Cache, Entry}
1616

1717
import java.security.PublicKey
1818
import cats.implicits._
19+
import org.typelevel.log4cats.Logger
1920

2021
import java.util.UUID
2122

2223
class MauthPublicKeyProviderSuite extends CatsEffectSuite {
2324

24-
implicit val logger = NoOpLogger[IO]
25+
implicit val logger: Logger[IO] = NoOpLogger[IO]
2526
private val MAUTH_PORT = PortFinder.findFreePort()
2627
private val MAUTH_BASE_URL = s"http://localhost:$MAUTH_PORT"
2728
private val MAUTH_URL_PATH = "/mauth/v1"

project/BuildSettings.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import sbt._
66
object BuildSettings {
77
val env: util.Map[String, String] = System.getenv()
88
val scala212 = "2.12.17"
9-
val scala213 = "2.13.10"
9+
val scala213 = "2.13.14"
1010

1111
lazy val basicSettings = Seq(
1212
homepage := Some(new URL("https://github.com/mdsol/mauth-jvm-clients")),

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# suppress inspection "UnusedProperty"
2-
sbt.version = 1.8.3
2+
sbt.version =1.10.0

0 commit comments

Comments
 (0)