Skip to content

Commit

Permalink
Merge pull request #24 from hmrc/dependencies
Browse files Browse the repository at this point in the history
BAU Build for compatibility with crypto 7.x.x
  • Loading branch information
colin-lamed committed Oct 3, 2022
2 parents 38486e9 + 498397c commit 2b9ac54
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -9,3 +9,5 @@ lib_managed
*.DS_Store
.metals/
.vscode/
.bloop/
metals.sbt
10 changes: 5 additions & 5 deletions build.sbt
Expand Up @@ -3,9 +3,9 @@ import sbt._

val compileDependencies = PlayCrossCompilation.dependencies(
play28 = Seq(
"uk.gov.hmrc" %% "json-encryption" % "4.11.0-play-28",
"uk.gov.hmrc" %% "json-encryption" % "5.1.0-play-28",
"com.typesafe.play" %% "play-json" % "2.8.2",
"uk.gov.hmrc" %% "http-verbs-play-28" % "13.12.0"
"uk.gov.hmrc" %% "http-verbs-play-28" % "14.7.0"
)
)

Expand All @@ -17,13 +17,13 @@ val testDependencies = PlayCrossCompilation.dependencies(
)
)

val scala2_12 = "2.12.15"
val scala2_13 = "2.13.7"
val scala2_12 = "2.12.17"
val scala2_13 = "2.13.9"

lazy val library = Project("http-caching-client", file("."))
.disablePlugins(sbt.plugins.JUnitXmlReportPlugin)
.settings(
majorVersion := 9,
majorVersion := 10,
isPublicArtefact := true,
scalaVersion := scala2_12,
crossScalaVersions := Seq(scala2_12, scala2_13),
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
@@ -1 +1 @@
sbt.version=1.5.8
sbt.version=1.6.2
2 changes: 1 addition & 1 deletion project/plugins.sbt
@@ -1,5 +1,5 @@
resolvers += MavenRepository("HMRC-open-artefacts-maven2", "https://open.artefacts.tax.service.gov.uk/maven2")
resolvers += Resolver.url("HMRC-open-artefacts-ivy2", url("https://open.artefacts.tax.service.gov.uk/ivy2"))(Resolver.ivyStylePatterns)

addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "3.6.0")
addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "3.8.0")
addSbtPlugin("uk.gov.hmrc" % "sbt-play-cross-compilation" % "2.3.0")
50 changes: 32 additions & 18 deletions src/main/scala/uk/gov/hmrc/http/cache/client/ShortLivedCache.scala
Expand Up @@ -18,41 +18,56 @@ package uk.gov.hmrc.http.cache.client

import play.api.libs.json._
import uk.gov.hmrc.crypto.json.{JsonDecryptor, JsonEncryptor}
import uk.gov.hmrc.crypto.{CompositeSymmetricCrypto, Protected}
import uk.gov.hmrc.crypto.{Decrypter, Encrypter, Protected}
import uk.gov.hmrc.http.{HeaderCarrier, HttpResponse}

import scala.concurrent.{ExecutionContext, Future}

trait ShortLivedCache extends CacheUtil {

implicit val crypto: CompositeSymmetricCrypto
implicit val crypto: Decrypter with Encrypter

def shortLiveCache: ShortLivedHttpCaching

def cache[A](cacheId: String, formId: String, body: A)(
implicit hc: HeaderCarrier,
def cache[A](
cacheId: String,
formId : String,
body : A
)(implicit
hc : HeaderCarrier,
wts: Writes[A],
executionContext: ExecutionContext): Future[CacheMap] = {
ec : ExecutionContext
): Future[CacheMap] = {
val protectd = Protected(body)
val encryptionFormat = new JsonEncryptor()
val fm = shortLiveCache.cache(cacheId, formId, protectd)(hc, encryptionFormat, executionContext)
fm.map(cm => new CryptoCacheMap(cm))
shortLiveCache
.cache(cacheId, formId, protectd)(hc, encryptionFormat, ec)
.map(cm => new CryptoCacheMap(cm))
}

def fetch(
cacheId: String)(implicit hc: HeaderCarrier, executionContext: ExecutionContext): Future[Option[CacheMap]] = {
val fm = shortLiveCache.fetch(cacheId)
fm.map(om => om.map(cm => new CryptoCacheMap(cm)))
}
cacheId: String
)(implicit
hc: HeaderCarrier,
ec: ExecutionContext
): Future[Option[CacheMap]] =
shortLiveCache
.fetch(cacheId)
.map(_.map(cm => new CryptoCacheMap(cm)))

def fetchAndGetEntry[T](
cacheId: String,
key: String)(implicit hc: HeaderCarrier, rds: Reads[T], executionContext: ExecutionContext): Future[Option[T]] =
key : String
)(implicit
hc : HeaderCarrier,
rds: Reads[T],
ec : ExecutionContext
): Future[Option[T]] =
try {
val decryptionFormat = new JsonDecryptor()
val encrypted: Future[Option[Protected[T]]] =
shortLiveCache.fetchAndGetEntry(cacheId, key)(hc, decryptionFormat, executionContext)
encrypted.map(op => convert(op))
shortLiveCache
.fetchAndGetEntry(cacheId, key)(hc, decryptionFormat, ec)
.map(convert)
} catch {
case e: SecurityException =>
throw CachingException(s"Failed to fetch a decrypted entry by cacheId:$cacheId and key:$key", e)
Expand All @@ -64,10 +79,10 @@ trait ShortLivedCache extends CacheUtil {

trait CacheUtil {
def convert[T](entry: Option[Protected[T]]): Option[T] =
entry.map(e => e.decryptedValue)
entry.map(_.decryptedValue)
}

class CryptoCacheMap(cm: CacheMap)(implicit crypto: CompositeSymmetricCrypto)
class CryptoCacheMap(cm: CacheMap)(implicit crypto: Decrypter with Encrypter)
extends CacheMap(cm.id, cm.data)
with CacheUtil {

Expand All @@ -79,5 +94,4 @@ class CryptoCacheMap(cm: CacheMap)(implicit crypto: CompositeSymmetricCrypto)
} catch {
case e: SecurityException => throw CachingException(s"Failed to fetch a decrypted entry by key:$key", e)
}

}

0 comments on commit 2b9ac54

Please sign in to comment.