Skip to content

Commit 91a1e0c

Browse files
Merge pull request #165 from mdsol/tech/update_mauth_handler
Tech: Update mAuth HTTP4S middleware to return the strict request not…
2 parents 3dcdbba + 219ce6c commit 91a1e0c

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,56 +49,56 @@ object MAuthMiddleware {
4949
logger.warn(errorLogMsg) *>
5050
Response[F](status = Status.Unauthorized).pure[G]
5151

52-
def extractHeader[A](headerName: CIString)(f: String => F[A]) =
52+
def extractHeader[A](request: Request[F], headerName: CIString)(f: String => F[A]) =
5353
request.headers
5454
.get(headerName)
5555
.map(_.head)
5656
.fold(F.raiseError[A](MdsolAuthMissingHeaderRejection(headerName.toString))) { header =>
5757
f(header.value)
5858
}
5959

60-
def extractAll(headerVersion: HeaderVersion) = {
60+
def extractAll(request: Request[F], headerVersion: HeaderVersion) = {
6161
val (ahn, thn) = headerVersion match {
6262
case V1 => (V1.authHeaderName, V1.timeHeaderName)
6363
case V2 => (V2.authHeaderName, V2.timeHeaderName)
6464
}
6565
for {
66-
authHeadValue <- extractHeader(ahn)(s => s.pure[F])
67-
timeHeadValue <- extractHeader(thn)(s => Try(s.toLong).liftTo[F])
66+
authHeadValue <- extractHeader(request, ahn)(s => s.pure[F])
67+
timeHeadValue <- extractHeader(request, thn)(s => Try(s.toLong).liftTo[F])
6868
} yield MAuthContext(authHeadValue, timeHeadValue)
6969

7070
}
7171

72-
def getHeaderValOrEmpty(headerName: CIString) =
72+
def getHeaderValOrEmpty(request: Request[F], headerName: CIString) =
7373
request.headers.get(headerName).map(_.head).fold("")(h => h.value)
7474

75-
val authHeaderTimeHeader =
75+
def authHeaderTimeHeader(request: Request[F]) =
7676
if (authenticator.isV2OnlyAuthenticate)
77-
extractAll(V2)
77+
extractAll(request, V2)
7878
else
79-
extractAll(V2) orElse extractAll(V1)
79+
extractAll(request, V2) orElse extractAll(request, V1)
8080

8181
fk(for {
82-
strictBody <- request.toStrict(none)
83-
byteArray <- strictBody.as[Array[Byte]]
84-
authCtx <- authHeaderTimeHeader
82+
strictRequest <- request.toStrict(none)
83+
byteArray <- strictRequest.as[Array[Byte]]
84+
authCtx <- authHeaderTimeHeader(strictRequest)
8585
mAuthRequest = new MAuthRequest(
8686
authCtx.authHeader,
8787
byteArray,
88-
request.method.name,
88+
strictRequest.method.name,
8989
authCtx.timeHeader.toString,
90-
request.uri.path.renderString,
91-
request.uri.query.renderString
90+
strictRequest.uri.path.renderString,
91+
strictRequest.uri.query.renderString
9292
)
9393
req = if (!authenticator.isV2OnlyAuthenticate) {
94-
mAuthRequest.setXmwsSignature(getHeaderValOrEmpty(V1.authHeaderName)) // dreadful mutating type
95-
mAuthRequest.setXmwsTime(getHeaderValOrEmpty(V1.timeHeaderName))
94+
mAuthRequest.setXmwsSignature(getHeaderValOrEmpty(strictRequest, V1.authHeaderName)) // dreadful mutating type
95+
mAuthRequest.setXmwsTime(getHeaderValOrEmpty(strictRequest, V1.timeHeaderName))
9696
mAuthRequest
9797
} 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))
98+
res <- authenticator.authenticate(req)(requestValidationTimeout)
99+
} yield (res, authCtx, strictRequest))
100+
.flatMap { case (b, ctx, strictRequest) =>
101+
if (b) http(AuthedRequest(ctx, strictRequest))
102102
else logAndReturnDefaultUnauthorizedReq(s"Rejecting request as authentication failed")
103103
}
104104
.recoverWith { case MdsolAuthMissingHeaderRejection(hn) =>

0 commit comments

Comments
 (0)