-
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 #166 from mdsol/tech/MCC-1289760
[MCC-1289760]: add mauth request sender for http4s
- Loading branch information
Showing
6 changed files
with
170 additions
and
33 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
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
13 changes: 13 additions & 0 deletions
13
...sender-sttp-http4s-http/src/main/scala/com/mdsol/mauth/SttpHttp4sMAuthRequestSender.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,13 @@ | ||
package com.mdsol.mauth | ||
|
||
import cats.effect.IO | ||
import sttp.client3.{Request, Response, SttpBackend} | ||
import sttp.capabilities.fs2.Fs2Streams | ||
|
||
class SttpHttp4sMAuthRequestSender( | ||
signer: MAuthSttpSigner, | ||
sttpBackend: SttpBackend[IO, Fs2Streams[IO]] | ||
) extends SttpMAuthRequestSender[IO] { | ||
override def send[T](request: Request[T, Any]): IO[Response[T]] = | ||
sttpBackend.send(signer.signSttpRequest(request)) | ||
} |
107 changes: 107 additions & 0 deletions
107
...er-sttp-http4s-http/src/test/scala/com/mdsol/mauth/SttpHttp4sMAuthRequestSenderSpec.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,107 @@ | ||
package com.mdsol.mauth | ||
|
||
import cats.effect.IO | ||
import cats.effect.kernel.Resource | ||
import cats.effect.unsafe.implicits.global | ||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.client.WireMock._ | ||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig | ||
import com.github.tomakehurst.wiremock.verification.LoggedRequest | ||
import com.mdsol.mauth.test.utils.TestFixtures | ||
import com.mdsol.mauth.test.utils.TestFixtures._ | ||
import com.mdsol.mauth.util.EpochTimeProvider | ||
import com.mdsol.mauth.util.MAuthKeysHelper.getPrivateKeyFromString | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider | ||
import org.scalatest.Inside._ | ||
import org.scalatest.matchers.should.Matchers._ | ||
import org.scalatest.wordspec.AsyncWordSpec | ||
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll} | ||
import sttp.client3.basicRequest | ||
import sttp.client3.http4s.Http4sBackend | ||
import sttp.model.{MediaType, Uri} | ||
|
||
import java.net.URI | ||
import java.security.Security | ||
import java.util.UUID | ||
import scala.jdk.CollectionConverters._ | ||
|
||
class SttpHttp4sMAuthRequestSenderSpec extends AsyncWordSpec with BeforeAndAfter with BeforeAndAfterAll { | ||
|
||
Security.addProvider(new BouncyCastleProvider) | ||
|
||
val wiremockServer: WireMockServer = new WireMockServer(wireMockConfig.dynamicPort()) | ||
|
||
lazy val requestSender: Resource[IO, SttpHttp4sMAuthRequestSender] = for { | ||
sttpBackend <- Http4sBackend.usingDefaultEmberClientBuilder[IO]() | ||
} yield new SttpHttp4sMAuthRequestSender(v1v2Signer, sttpBackend) | ||
|
||
"correctly send auth signatures and content-type header" in { | ||
val req = basicRequest | ||
.get(Uri(new URI(s"${wiremockServer.baseUrl()}$REQUEST_NORMALIZE_PATH"))) | ||
.body("") | ||
.contentType(MediaType.ApplicationJson) | ||
|
||
requestSender | ||
.use { case sttpSender => | ||
sttpSender.send(req).map { _ => | ||
inside(getRecordedRequests()) { case List(r) => | ||
r.getHeader("content-type") shouldBe "application/json" | ||
r.getHeader(TIME_HEADER_V1) shouldBe EPOCH_TIME | ||
r.getHeader(AUTH_HEADER_V1) shouldBe s"MWS $APP_UUID_V2:$SIGNATURE_NORMALIZE_PATH_V1" | ||
|
||
r.getHeader(TIME_HEADER_V2) shouldBe EPOCH_TIME | ||
r.getHeader(AUTH_HEADER_V2) shouldBe s"MWSV2 $APP_UUID_V2:$SIGNATURE_NORMALIZE_PATH_V2;" | ||
} | ||
} | ||
} | ||
.unsafeToFuture() | ||
} | ||
|
||
"sends a default content type (text/plain UTF-8) when content type not specified" in { | ||
val req = basicRequest.get(Uri(new URI(s"${wiremockServer.baseUrl()}/"))).body("hello") | ||
|
||
requestSender | ||
.use { case sttpSender => | ||
sttpSender.send(req).map { _ => | ||
inside(getRecordedRequests()) { case List(r) => | ||
r.getHeader("content-type") shouldBe "text/plain; charset=UTF-8" | ||
} | ||
} | ||
} | ||
.unsafeToFuture() | ||
} | ||
|
||
private def getRecordedRequests(): List[LoggedRequest] = | ||
wiremockServer.getAllServeEvents.asScala.toList.map(_.getRequest) | ||
|
||
lazy val v1v2Signer: MAuthSttpSigner = { | ||
val epochTimeProvider: EpochTimeProvider = () => EPOCH_TIME.toLong | ||
new MAuthSttpSignerImpl( | ||
UUID.fromString(APP_UUID_V2), | ||
getPrivateKeyFromString(TestFixtures.PRIVATE_KEY_2), | ||
epochTimeProvider, | ||
SignerConfiguration.ALL_SIGN_VERSIONS | ||
) | ||
} | ||
|
||
before { | ||
wiremockServer.stubFor( | ||
get(anyUrl()) | ||
.willReturn(aResponse().withStatus(200)) | ||
) | ||
} | ||
|
||
after { | ||
wiremockServer.resetAll() | ||
} | ||
|
||
override protected def beforeAll(): Unit = { | ||
super.beforeAll() | ||
wiremockServer.start() | ||
} | ||
|
||
override protected def afterAll(): Unit = { | ||
super.afterAll() | ||
wiremockServer.stop() | ||
} | ||
} |
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