Skip to content

Commit

Permalink
PIN-4112 Resolved last PR issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nttdata-rtorsoli committed Oct 16, 2023
1 parent 05abf94 commit f391442
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package it.pagopa.interop.authorizationmanagement.model.persistence

import it.pagopa.interop.authorizationmanagement.errors.KeyManagementErrors.ThumbprintCalculationError
import it.pagopa.interop.authorizationmanagement.errors.KeyManagementErrors.{InvalidKey, ThumbprintCalculationError}
import it.pagopa.interop.authorizationmanagement.model.key.{Enc, PersistentKey, PersistentKeyUse, Sig}
import it.pagopa.interop.authorizationmanagement.jwk.model.Models._
import it.pagopa.interop.authorizationmanagement.model.{KeyUse, Key, OtherPrimeInfo, JWKKey}
import it.pagopa.interop.authorizationmanagement.jwk.converter.KeyConverter
import it.pagopa.interop.authorizationmanagement.model.KeyUse.{ENC, SIG}
import java.util.UUID
import cats.syntax.all._

object KeyAdapters {

implicit class PersistentKeyWrapper(private val p: PersistentKey) extends AnyVal {
def toApi: Either[Throwable, Key] =
KeyConverter
.fromBase64encodedPEMToAPIKey(p.kid, p.encodedPem, p.use.toJwk, p.algorithm)
.map(_ =>
Key(p.userId.getOrElse(UUID.randomUUID()), p.kid, p.name, p.encodedPem, p.algorithm, p.use.toApi, p.createdAt)
)
def toApi: Either[Throwable, Key] = {
val apiKey = KeyConverter.fromBase64encodedPEMToAPIKey(p.kid, p.encodedPem, p.use.toJwk, p.algorithm)
p.userId match {
case Some(userId) => apiKey.map(_ => Key(userId, p.kid, p.name, p.encodedPem, p.algorithm, p.use.toApi, p.createdAt))
case None => p.relationshipId match {
case Some(relationshipId) => apiKey.map(_ => Key(relationshipId, p.kid, p.name, p.encodedPem, p.algorithm, p.use.toApi, p.createdAt))
case None => InvalidKey(p.kid, s"User Id and Relationship Id not found on key ${p.kid}").asLeft[Key]
}
}
}
}

implicit class PersistentKeyObjectWrapper(private val p: PersistentKey.type) extends AnyVal {
Expand Down

0 comments on commit f391442

Please sign in to comment.