-
Notifications
You must be signed in to change notification settings - Fork 8
DDCNL-12161: TAI - Add in auth caching #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
0308433
5ea29c8
74e49e9
e51f07d
1ba6f22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.model | ||
|
|
||
| import play.api.libs.json.{Json, OFormat} | ||
|
|
||
| case class CachedAuthRetrievals( | ||
| nino: String | ||
| ) | ||
|
|
||
| object CachedAuthRetrievals { | ||
| implicit val format: OFormat[CachedAuthRetrievals] = Json.format[CachedAuthRetrievals] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.model | ||
|
|
||
| import play.api.libs.json.* | ||
| import uk.gov.hmrc.crypto.json.JsonEncryption.{sensitiveDecrypter, sensitiveEncrypter} | ||
| import uk.gov.hmrc.crypto.{Decrypter, Encrypter, Sensitive} | ||
|
|
||
| case class SensitiveWrapper[T](override val decryptedValue: T) extends Sensitive[T] | ||
|
|
||
| object SensitiveWrapper { | ||
|
|
||
| implicit def reads[T](implicit | ||
| reads: Reads[T], | ||
| crypto: Encrypter with Decrypter | ||
| ): Reads[SensitiveWrapper[T]] = sensitiveDecrypter(SensitiveWrapper[T]) | ||
|
|
||
| implicit def writes[T](implicit | ||
| writes: Writes[T], | ||
| crypto: Encrypter with Decrypter | ||
| ): Writes[SensitiveWrapper[T]] = sensitiveEncrypter | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.repositories.cache | ||
|
|
||
| import play.api.libs.json.{Reads, Writes} | ||
| import uk.gov.hmrc.tai.config.{CryptoProvider, MongoConfig} | ||
| import uk.gov.hmrc.http.HeaderCarrier | ||
| import uk.gov.hmrc.mongo.cache.DataKey | ||
| import uk.gov.hmrc.mongo.{CurrentTimestampSupport, MongoComponent} | ||
|
|
||
| import java.util.concurrent.TimeUnit | ||
| import javax.inject.{Inject, Singleton} | ||
| import scala.concurrent.ExecutionContext | ||
| import scala.concurrent.duration.Duration | ||
|
|
||
| @Singleton | ||
| class AuthCacheRepository @Inject() ( | ||
| mongoConfig: MongoConfig, | ||
| cryptoProvider: CryptoProvider, | ||
| mongoComponent: MongoComponent | ||
| )(implicit ec: ExecutionContext) | ||
| extends GenericCacheRepository[HeaderCarrier]( | ||
| mongoComponent = mongoComponent, | ||
| crypto = cryptoProvider.get, | ||
| collectionName = "taiAuthCache", | ||
| ttl = Duration(mongoConfig.mongoAuthTTL, TimeUnit.SECONDS), | ||
| timestampSupport = new CurrentTimestampSupport(), | ||
| cacheIdType = RequestCacheId | ||
| ) { | ||
|
|
||
| def putSession[T: Writes](dataKey: DataKey[T], data: T)(implicit | ||
| hc: HeaderCarrier | ||
| ): scala.concurrent.Future[(String, String)] = | ||
| put(hc, dataKey, data) | ||
|
|
||
| def getFromSession[T: Reads](dataKey: DataKey[T])(implicit hc: HeaderCarrier): scala.concurrent.Future[Option[T]] = | ||
| get(hc, dataKey) | ||
|
|
||
| def deleteFromSession[T](dataKey: DataKey[T])(implicit hc: HeaderCarrier): scala.concurrent.Future[Unit] = | ||
| delete(hc, dataKey) | ||
|
|
||
| def deleteAllFromSession(implicit hc: HeaderCarrier): scala.concurrent.Future[Unit] = | ||
| deleteAll(hc) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.repositories.cache | ||
|
|
||
| import org.mongodb.scala.model.IndexModel | ||
| import play.api.Logging | ||
| import play.api.libs.json.{Reads, Writes} | ||
| import uk.gov.hmrc.crypto.{Decrypter, Encrypter} | ||
| import uk.gov.hmrc.tai.model.SensitiveWrapper | ||
| import uk.gov.hmrc.mdc.Mdc | ||
| import uk.gov.hmrc.mongo.cache.{CacheIdType, DataKey, MongoCacheRepository} | ||
| import uk.gov.hmrc.mongo.{MongoComponent, MongoDatabaseCollection, TimestampSupport} | ||
|
|
||
| import javax.inject.{Inject, Singleton} | ||
| import scala.concurrent.duration.Duration | ||
| import scala.concurrent.{ExecutionContext, Future} | ||
| import scala.util.control.NonFatal | ||
|
|
||
| @Singleton | ||
| abstract class GenericCacheRepository[CacheId] @Inject() ( | ||
| mongoComponent: MongoComponent, | ||
| override val collectionName: String, | ||
| val crypto: Encrypter with Decrypter, | ||
| replaceIndexes: Boolean = true, | ||
| ttl: Duration, | ||
| timestampSupport: TimestampSupport, | ||
| cacheIdType: CacheIdType[CacheId] | ||
| )(implicit ec: ExecutionContext) | ||
| extends MongoDatabaseCollection with Logging { | ||
|
|
||
| private val cacheRepo: MongoCacheRepository[CacheId] = new MongoCacheRepository[CacheId]( | ||
| mongoComponent = mongoComponent, | ||
| collectionName = collectionName, | ||
| replaceIndexes = replaceIndexes, | ||
| ttl = ttl, | ||
| timestampSupport = timestampSupport, | ||
| cacheIdType = cacheIdType | ||
| ) | ||
|
|
||
| override val indexes: Seq[IndexModel] = cacheRepo.indexes | ||
|
|
||
| given (Encrypter with Decrypter) = crypto | ||
|
|
||
| def put[T: Writes](cacheId: CacheId, dataKey: DataKey[T], data: T): Future[(String, String)] = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think put should also have recoverWith like get wher ewe log an error and move on, now it would fail bcos of mongo blip
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept this aligned with Pertax and FANDF, where put() doesn't recover. If we want to make cache writes best-effort, I'd prefer handling it in AuthAction around the putSession call rather than changing the generic repository. |
||
| Mdc.preservingMdc { | ||
| cacheRepo | ||
| .put[SensitiveWrapper[T]](cacheId)(DataKey[SensitiveWrapper[T]](dataKey.unwrap), SensitiveWrapper(data)) | ||
| .map(res => "id" -> res.id) | ||
| } | ||
|
|
||
| def get[T: Reads](cacheId: CacheId, dataKey: DataKey[T]): Future[Option[T]] = | ||
| Mdc.preservingMdc { | ||
| cacheRepo | ||
| .get[SensitiveWrapper[T]](cacheId)(DataKey[SensitiveWrapper[T]](dataKey.unwrap)) | ||
| .map(_.map(_.decryptedValue)) recoverWith { case NonFatal(error) => | ||
| logger.error(s"Failed to read data from cache", error) | ||
| Future.successful(None) | ||
| } | ||
| } | ||
|
|
||
| def delete[T](cacheId: CacheId, dataKey: DataKey[T]): Future[Unit] = | ||
| Mdc.preservingMdc { | ||
| cacheRepo.delete(cacheId)(DataKey[SensitiveWrapper[T]](dataKey.unwrap)) | ||
| } | ||
|
|
||
| def deleteAll(cacheId: CacheId): Future[Unit] = | ||
| Mdc.preservingMdc { | ||
| cacheRepo.deleteEntity(cacheId) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright 2026 HM Revenue & Customs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package uk.gov.hmrc.tai.repositories.cache | ||
|
|
||
| import play.api.Logger | ||
| import uk.gov.hmrc.http.HeaderCarrier | ||
| import uk.gov.hmrc.mongo.cache.CacheIdType | ||
|
|
||
| import java.util.UUID.randomUUID | ||
|
|
||
| case object RequestCacheId extends CacheIdType[HeaderCarrier] { | ||
| override def run: HeaderCarrier => String = { | ||
| val logger: Logger = Logger(this.getClass) | ||
|
|
||
| _.requestId | ||
| .map(_.value) | ||
| .getOrElse { | ||
| logger.error(NoRequestIdException.getMessage, NoRequestIdException) | ||
| randomUUID.toString | ||
| } | ||
| } | ||
| } | ||
| case object NoRequestIdException extends Exception("Could not find requestId") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we log it as error, because session timeout, in pertax we dont log anything for these error, worth keeping same pattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.