-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from mdsol/NOJIRA-add_signer_for_http4s_022
NOJIRA add http4s-0.22 signer
- Loading branch information
Showing
9 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...s/mauth-signer-http4s-022/src/main/scala/com/mdsol/mauth/http4s022/client/Implicits.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.mdsol.mauth.http4s022.client | ||
|
||
import cats.MonadThrow | ||
import com.mdsol.mauth.models.SignedRequest | ||
import org.http4s.headers.`Content-Type` | ||
import org.http4s.{headers, Header, Headers, Method, Request, Uri} | ||
import org.typelevel.ci.CIString | ||
import cats.syntax.all._ | ||
|
||
import scala.annotation.nowarn | ||
import scala.collection.immutable | ||
|
||
object Implicits { | ||
|
||
implicit class NewSignedRequestOps(val signedRequest: SignedRequest) extends AnyVal { | ||
|
||
/** Create a http4s request from a [[models.SignedRequest]] | ||
*/ | ||
def toHttp4sRequest[F[_]: MonadThrow]: F[Request[F]] = { | ||
val contentType: Option[`Content-Type`] = extractContentTypeFromHeaders(signedRequest.req.headers) | ||
val headersWithoutContentType: Map[String, String] = removeContentTypeFromHeaders(signedRequest.req.headers) | ||
|
||
val allHeaders: immutable.Seq[Header.Raw] = (headersWithoutContentType ++ signedRequest.mauthHeaders).toList | ||
.map { case (name, value) => | ||
Header.Raw(CIString(name), value) | ||
} | ||
|
||
for { | ||
uri <- Uri.fromString(signedRequest.req.uri.toString).liftTo[F] | ||
method <- Method.fromString(signedRequest.req.httpMethod).liftTo[F] | ||
} yield Request[F]( | ||
method = method, | ||
uri = uri, | ||
body = fs2.Stream.emits(signedRequest.req.body), | ||
headers = Headers(allHeaders) | ||
).withContentTypeOption(contentType) | ||
} | ||
|
||
private def extractContentTypeFromHeaders(requestHeaders: Map[String, String]): Option[`Content-Type`] = | ||
requestHeaders | ||
.get(headers.`Content-Type`.toString) | ||
.flatMap(str => `Content-Type`.parse(str).toOption) | ||
|
||
@nowarn("msg=.*Unused import.*") // compat import only needed for 2.12 | ||
private def removeContentTypeFromHeaders(requestHeaders: Map[String, String]): Map[String, String] = { | ||
import scala.collection.compat._ | ||
requestHeaders.view.filterKeys(_ != headers.`Content-Type`.toString).toMap | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...mauth-signer-http4s-022/src/main/scala/com/mdsol/mauth/http4s022/client/MAuthSigner.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.mdsol.mauth.http4s022.client | ||
|
||
import cats.effect._ | ||
import cats.syntax.all._ | ||
import com.mdsol.mauth.RequestSigner | ||
import com.mdsol.mauth.models.UnsignedRequest | ||
import org.http4s.Request | ||
import org.http4s.client.Client | ||
|
||
import java.net.URI | ||
|
||
object MAuthSigner { | ||
def apply[F[_]: Sync](signer: RequestSigner)(client: Client[F]): Client[F] = | ||
Client { req => | ||
for { | ||
req <- Resource.eval(req.as[Array[Byte]].flatMap { byteArray => | ||
val signedRequest = signer.signRequest( | ||
UnsignedRequest( | ||
req.method.name, | ||
URI.create(req.uri.renderString), | ||
byteArray, | ||
req.headers.headers.view.map(h => h.name.toString -> h.value).toMap | ||
) | ||
) | ||
Request( | ||
method = req.method, | ||
uri = req.uri, | ||
headers = req.headers.put(signedRequest.mauthHeaders.toList), | ||
body = req.body | ||
).pure[F] | ||
}) | ||
res <- client.run(req) | ||
} yield res | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...tp4s-022/src/test/scala/com/mdsol/mauth/http4s022/client/MAuthSignerMiddlewareSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.mdsol.mauth.http4s022.client | ||
|
||
import cats.effect.IO | ||
import cats.syntax.all._ | ||
import com.mdsol.mauth.models.UnsignedRequest | ||
import com.mdsol.mauth.{MAuthRequestSigner, MAuthVersion} | ||
import munit.CatsEffectSuite | ||
import org.http4s.client.Client | ||
import org.http4s.{Headers, HttpRoutes, Request, Response, Status, Uri} | ||
import org.http4s.dsl.io._ | ||
import com.mdsol.mauth.test.utils.TestFixtures._ | ||
import com.mdsol.mauth.util.EpochTimeProvider | ||
|
||
import java.net.URI | ||
import java.util.UUID | ||
|
||
class MAuthSignerMiddlewareSuite extends CatsEffectSuite { | ||
|
||
private val CONST_EPOCH_TIME_PROVIDER: EpochTimeProvider = new EpochTimeProvider() { override def inSeconds(): Long = EXPECTED_TIME_HEADER_1.toLong } | ||
|
||
private val signerV2: MAuthRequestSigner = new MAuthRequestSigner( | ||
UUID.fromString(APP_UUID_1), | ||
PRIVATE_KEY_1, | ||
CONST_EPOCH_TIME_PROVIDER, | ||
java.util.Arrays.asList[MAuthVersion](MAuthVersion.MWSV2) | ||
) | ||
|
||
val signerV1: MAuthRequestSigner = new MAuthRequestSigner( | ||
UUID.fromString(APP_UUID_1), | ||
PRIVATE_KEY_1, | ||
CONST_EPOCH_TIME_PROVIDER, | ||
java.util.Arrays.asList[MAuthVersion](MAuthVersion.MWS) | ||
) | ||
|
||
private def route(headers: Map[String, String]) = HttpRoutes | ||
.of[IO] { case req @ POST -> Root / "v1" / "test" => | ||
if (headers.forall(h => req.headers.headers.map(h => h.name.toString -> h.value).contains(h))) | ||
Response[IO](Status.Ok).pure[IO] | ||
else | ||
Response[IO](Status.InternalServerError).pure[IO] | ||
} | ||
.orNotFound | ||
|
||
test("correctly send a customized content-type header for v2") { | ||
|
||
val simpleNewUnsignedRequest = | ||
UnsignedRequest | ||
.fromStringBodyUtf8( | ||
httpMethod = "POST", | ||
uri = new URI(s"/v1/test"), | ||
body = "", | ||
headers = Map("Content-Type" -> "application/json") | ||
) | ||
|
||
val signedReq = signerV2.signRequest(simpleNewUnsignedRequest) | ||
|
||
val client = Client.fromHttpApp(route(signedReq.mauthHeaders ++ simpleNewUnsignedRequest.headers)) | ||
|
||
val mAuthedClient = MAuthSigner(signerV2)(client) | ||
|
||
mAuthedClient | ||
.status( | ||
Request[IO]( | ||
method = POST, | ||
uri = Uri.unsafeFromString(s"/v1/test"), | ||
headers = Headers(signedReq.mauthHeaders.toList ++ List("Content-Type" -> "application/json")) | ||
) | ||
) | ||
.assertEquals(Status.Ok) | ||
} | ||
|
||
test("correctly send a customized content-type header for v1") { | ||
|
||
val simpleNewUnsignedRequest = | ||
UnsignedRequest | ||
.fromStringBodyUtf8( | ||
httpMethod = "POST", | ||
uri = new URI(s"/v1/test"), | ||
body = "", | ||
headers = Map("Content-Type" -> "application/json") | ||
) | ||
|
||
val signedReq = signerV1.signRequest(simpleNewUnsignedRequest) | ||
|
||
val client = Client.fromHttpApp(route(signedReq.mauthHeaders ++ simpleNewUnsignedRequest.headers)) | ||
|
||
val mAuthedClient = MAuthSigner(signerV1)(client) | ||
|
||
mAuthedClient | ||
.status( | ||
Request[IO]( | ||
method = POST, | ||
uri = Uri.unsafeFromString(s"/v1/test"), | ||
headers = Headers(signedReq.mauthHeaders.toList ++ List("Content-Type" -> "application/json")) | ||
) | ||
) | ||
.assertEquals(Status.Ok) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters