From cf1c10d77fb2c56eeef977acfa48e1301dcd1c8b Mon Sep 17 00:00:00 2001 From: Josef Vlach <38655672+josef-vlach@users.noreply.github.com> Date: Fri, 1 Nov 2024 10:16:37 +0000 Subject: [PATCH] GFORMS-3014 - Add detail.FormTemplateId to gform-frontend requestReceived events (#2308) --- .../FrontendFiltersModule.scala | 29 +++++++++++++- .../RequestFormTemplateId.scala | 40 +++++++++++++++++++ .../playcomponents/RequestHeaderService.scala | 21 +++------- 3 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 app/uk/gov/hmrc/gform/playcomponents/RequestFormTemplateId.scala diff --git a/app/uk/gov/hmrc/gform/playcomponents/FrontendFiltersModule.scala b/app/uk/gov/hmrc/gform/playcomponents/FrontendFiltersModule.scala index 68a95b427..d8e6643d7 100644 --- a/app/uk/gov/hmrc/gform/playcomponents/FrontendFiltersModule.scala +++ b/app/uk/gov/hmrc/gform/playcomponents/FrontendFiltersModule.scala @@ -17,7 +17,8 @@ package uk.gov.hmrc.gform.playcomponents import org.apache.pekko.stream.Materializer -import play.api.mvc.{ CookieHeaderEncoding, DefaultCookieHeaderEncoding, EssentialFilter, SessionCookieBaker } +import play.api.libs.json.{ JsObject, Json } +import play.api.mvc.{ CookieHeaderEncoding, DefaultCookieHeaderEncoding, EssentialFilter, RequestHeader, SessionCookieBaker } import play.filters.cors.{ CORSConfig, CORSFilter } import play.filters.csrf.CSRFComponents import play.filters.headers.SecurityHeadersFilter @@ -27,6 +28,8 @@ import uk.gov.hmrc.gform.config.ConfigModule import uk.gov.hmrc.gform.controllers.{ ControllersModule, ErrorHandler } import uk.gov.hmrc.gform.gformbackend.GformBackendModule import uk.gov.hmrc.crypto.ApplicationCrypto +import uk.gov.hmrc.http.HeaderCarrier +import uk.gov.hmrc.play.audit.model.{ ExtendedDataEvent, RedactionLog, TruncationLog } import uk.gov.hmrc.play.bootstrap.frontend.filters.crypto.DefaultSessionCookieCryptoFilter import uk.gov.hmrc.play.bootstrap.frontend.filters.crypto.SessionCookieCrypto import uk.gov.hmrc.play.bootstrap.frontend.filters.crypto.SessionCookieCryptoFilter @@ -96,6 +99,30 @@ class FrontendFiltersModule( materializer ) { override val maskedFormFields = Seq("password") + + override def extendedDataEvent( + eventType: String, + transactionName: String, + request: RequestHeader, + detail: JsObject, + truncationLog: TruncationLog, + redactionLog: RedactionLog + )(implicit hc: HeaderCarrier): ExtendedDataEvent = { + + val updatedDetail = RequestFormTemplateId.formTemplateId(request) match { + case Some(formTemplateId) => detail ++ Json.obj("formTemplateId" -> formTemplateId.value) + case None => detail + } + + super.extendedDataEvent( + eventType, + transactionName, + request, + updatedDetail, + truncationLog, + redactionLog + ) + } } private val hmrcSessionCookieCryptoFilter: SessionCookieCryptoFilter = { diff --git a/app/uk/gov/hmrc/gform/playcomponents/RequestFormTemplateId.scala b/app/uk/gov/hmrc/gform/playcomponents/RequestFormTemplateId.scala new file mode 100644 index 000000000..684970692 --- /dev/null +++ b/app/uk/gov/hmrc/gform/playcomponents/RequestFormTemplateId.scala @@ -0,0 +1,40 @@ +/* + * Copyright 2024 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.gform.playcomponents + +import cats.syntax.eq._ +import play.api.mvc.RequestHeader +import play.api.routing.Router.RequestImplicits._ +import uk.gov.hmrc.gform.sharedmodel.formtemplate.FormTemplateId + +object RequestFormTemplateId { + def formTemplateId(rh: RequestHeader): Option[FormTemplateId] = { + val formTemplateIdParamIndex: Option[Int] = { + val mayContainsFormTemplateId: Option[Array[Boolean]] = + rh.handlerDef.map(_.path.split("/")).map(_.map(_.containsSlice("$formTemplateId"))) + mayContainsFormTemplateId.map(_.indexOf(true)) + } + + formTemplateIdParamIndex match { + case Some(i) if i =!= -1 => + val templateId = rh.uri.split("\\?")(0).split("/")(i) + Some(FormTemplateId(templateId)) + case _ => + None + } + } +} diff --git a/app/uk/gov/hmrc/gform/playcomponents/RequestHeaderService.scala b/app/uk/gov/hmrc/gform/playcomponents/RequestHeaderService.scala index bfe2b61aa..83a6fcfa9 100644 --- a/app/uk/gov/hmrc/gform/playcomponents/RequestHeaderService.scala +++ b/app/uk/gov/hmrc/gform/playcomponents/RequestHeaderService.scala @@ -16,9 +16,7 @@ package uk.gov.hmrc.gform.playcomponents -import cats.syntax.eq._ import play.api.mvc.RequestHeader -import play.api.routing.Router.RequestImplicits._ import scala.concurrent.{ ExecutionContext, Future } import uk.gov.hmrc.gform.gformbackend.GformConnector import uk.gov.hmrc.gform.sharedmodel.formtemplate.{ FormTemplateContext, FormTemplateId } @@ -29,20 +27,11 @@ final class RequestHeaderService( gformConnector: GformConnector )(implicit ec: ExecutionContext) { - def formTemplateContext(rh: RequestHeader): Future[Option[FormTemplateContext]] = { - val formTemplateIdParamIndex: Option[Int] = { - val mayContainsFormTemplateId: Option[Array[Boolean]] = - rh.handlerDef.map(_.path.split("/")).map(_.map(_.containsSlice("$formTemplateId"))) - mayContainsFormTemplateId.map(_.indexOf(true)) - } - - formTemplateIdParamIndex match { - case Some(i) if i =!= -1 => - val templateId = rh.uri.split("\\?")(0).split("/")(i) + def formTemplateContext(rh: RequestHeader): Future[Option[FormTemplateContext]] = + RequestFormTemplateId.formTemplateId(rh) match { + case Some(FormTemplateId(formTemplateId)) => implicit val hc: HeaderCarrier = HeaderCarrierConverter.fromRequest(rh) - gformConnector.getFormTemplateContext(FormTemplateId(templateId.toLowerCase)).map(Some(_)) - case _ => - Future.successful(None) + gformConnector.getFormTemplateContext(FormTemplateId(formTemplateId.toLowerCase)).map(Some(_)) + case None => Future.successful(None) } - } }