Skip to content

Commit

Permalink
PIN-4127 Resolved PR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli committed Oct 17, 2023
1 parent 472c24e commit 938943d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 38 deletions.
24 changes: 0 additions & 24 deletions jwt/src/main/scala/it/pagopa/interop/commons/jwt/package.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package it.pagopa.interop.commons

import akka.http.scaladsl.marshalling.ToEntityMarshaller
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.server.Route
import cats.syntax.all._
import com.nimbusds.jose.crypto.{ECDSAVerifier, RSASSAVerifier}
Expand Down Expand Up @@ -159,25 +156,4 @@ package object jwt {

AkkaResponses.forbidden(OperationForbidden, logMessage)
}

// TODO Kept for backward compatibility.
// To be removed as soon as all services have been migrated to the new authorize function
def authorizeInterop[T](isAuthorized: => Boolean, errorMessage: => T)(
route: => Route
)(implicit contexts: Seq[(String, String)], errorMarshaller: ToEntityMarshaller[T]): Route = if (isAuthorized) route
else {
val values: Map[String, String] = contexts.toMap
val uid: String = values.get(UID).filterNot(_.isBlank).orElse(values.get(SUB)).getOrElse("")
val organizationId: String = values.getOrElse(ORGANIZATION_ID_CLAIM, "")
val correlationId: String = values.getOrElse(CORRELATION_ID_HEADER, "")
val header: String = s"[UID=$uid] [OID=$organizationId] [CID=$correlationId]"
val body: String = values
.get(USER_ROLES)
.fold(s"No user roles found to execute this request")(roles =>
s"Invalid user roles ($roles) to execute this request"
)

logger.error(s"$header $body")
complete(StatusCodes.Forbidden, errorMessage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.typesafe.config.{Config, ConfigFactory}
import com.typesafe.scalalogging.CanLog
import it.pagopa.interop.commons.utils.{
CORRELATION_ID_HEADER,
IP_ADDRESS,
ORGANIZATION_ID_CLAIM,
SUB,
UID,
Expand Down Expand Up @@ -57,19 +56,15 @@ package object logging {
changeUUID: Boolean
)(wrappingDirective: Directive1[Seq[(String, String)]]): Directive1[Seq[(String, String)]] =
for {
ip <- extractClientIP
correlationId <- optionalHeaderValueByName(CORRELATION_ID_HEADER)
language <- selectPreferredLanguage(DEFAULT_LANGUAGE, OTHER_LANGUAGES: _*)
contexts <- wrappingDirective
} yield {
val ipAddress: String = ip.toOption.map(_.getHostAddress).getOrElse("unknown")
def uuid: String = UUID.randomUUID().toString
val actualCorrelationId: String = if (changeUUID) uuid else correlationId.getOrElse(uuid)
val acceptLanguage: String = language.toString

contexts.prependedAll(
List(CORRELATION_ID_HEADER -> actualCorrelationId, IP_ADDRESS -> ipAddress, ACCEPT_LANGUAGE -> acceptLanguage)
)
contexts.prependedAll(List(CORRELATION_ID_HEADER -> actualCorrelationId, ACCEPT_LANGUAGE -> acceptLanguage))
}

def logHttp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package object utils {
val ORGANIZATION: String = "organization"
val USER_ROLES: String = "user-roles"
val CORRELATION_ID_HEADER: String = "X-Correlation-Id"
val IP_ADDRESS: String = "X-Forwarded-For"
val ACCEPT_LANGUAGE: String = "Accept-Language"
val CONTENT_LANGUAGE: String = "Content-Language"
val INTEROP_PRODUCT_NAME: String = "prod-interop"
Expand All @@ -39,21 +38,17 @@ package object utils {

type BearerToken = String
type CorrelationId = String
type IpAddress = String

def extractHeaders(
contexts: Seq[(String, String)]
): Either[ComponentError, (BearerToken, CorrelationId, Option[IpAddress])] = {
def extractHeaders(contexts: Seq[(String, String)]): Either[ComponentError, (BearerToken, CorrelationId)] = {
val contextsMap = contexts.toMap
for {
bearerToken <- contextsMap.get(BEARER).toRight(MissingBearer)
correlationId <- contextsMap.get(CORRELATION_ID_HEADER).toRight(MissingHeader(CORRELATION_ID_HEADER))
ip = contextsMap.get(IP_ADDRESS)
} yield (bearerToken, correlationId, ip)
} yield (bearerToken, correlationId)
}

def withHeaders[T](
f: (BearerToken, CorrelationId, Option[IpAddress]) => Future[T]
f: (BearerToken, CorrelationId) => Future[T]
)(implicit contexts: Seq[(String, String)]): Future[T] = extractHeaders(contexts) match {
case Left(ex) => Future.failed(ex)
case Right(x) => f.tupled(x)
Expand Down

0 comments on commit 938943d

Please sign in to comment.