Skip to content

Commit

Permalink
Add integration test and refactoring code cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: mineme0110 <[email protected]>
  • Loading branch information
mineme0110 committed Nov 19, 2024
1 parent 6a4d456 commit e15e9c2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.hyperledger.identus.pollux.core.repository.{CredentialRepository, Cre
import org.hyperledger.identus.pollux.prex.{ClaimFormat, Jwt, PresentationDefinition}
import org.hyperledger.identus.pollux.sdjwt.*
import org.hyperledger.identus.pollux.vc.jwt.{Issuer as JwtIssuer, *}
import org.hyperledger.identus.pollux.vc.jwt.PresentationPayload.Implicits.*
import org.hyperledger.identus.shared.crypto.{Ed25519KeyPair, Secp256k1KeyPair}
import org.hyperledger.identus.shared.http.UriResolver
import org.hyperledger.identus.shared.messaging.{Producer, WalletIdAndRecordId}
Expand Down Expand Up @@ -1505,7 +1506,7 @@ class CredentialServiceImpl(
ZIO.fail(CredentialRequestValidationFailed(s"JWT presentation verification failed: $error"))

jwtPresentation <- ZIO
.fromTry(JwtPresentation.decodeJwt(jwt))
.fromTry(JwtPresentation.decodeJwt[JwtPresentationPayload](jwt))
.mapError(t => CredentialRequestValidationFailed(s"JWT presentation decoding failed: ${t.getMessage}"))
} yield jwtPresentation
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.hyperledger.identus.pollux.prex.PresentationSubmissionError.{
JsonPathNotFound,
SubmissionNotSatisfyInputDescriptors
}
import org.hyperledger.identus.pollux.vc.jwt.{JWT, JwtCredential, JwtPresentation}
import org.hyperledger.identus.pollux.vc.jwt.{JWT, JwtCredential, JwtPresentation, JwtPresentationPayload}
import org.hyperledger.identus.pollux.vc.jwt.CredentialPayload.Implicits.*
import org.hyperledger.identus.pollux.vc.jwt.PresentationPayload.Implicits.*
import org.hyperledger.identus.shared.json.{JsonInterop, JsonPath, JsonPathError, JsonSchemaValidatorImpl}
Expand Down Expand Up @@ -220,7 +220,7 @@ object PresentationSubmissionVerification {
.map(JWT(_))
.mapError(_ => InvalidDataTypeForClaimFormat(format, path, "string"))
payload <- ZIO
.fromTry(JwtPresentation.decodeJwt(jwt))
.fromTry(JwtPresentation.decodeJwt[JwtPresentationPayload](jwt))
.mapError(e => ClaimDecodeFailure(format, path, e.getMessage()))
_ <- formatVerification(jwt)
.mapError(errors => ClaimFormatVerificationFailure(format, path, errors.mkString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ object JwtPresentation {
.flatMap(decode[JwtPresentationPayload](_).toTry)
}

def decodeJwt[A](jwt: JWT)(using decoder: io.circe.Decoder[A]): Try[A] = {
JwtCirce
.decodeRaw(jwt.value, options = JwtOptions(signature = false, expiration = false, notBefore = false))
.flatMap(decode[A](_).toTry)
}

def decodeJwt(jwt: JWT, publicKey: PublicKey): Try[JwtPresentationPayload] = {
JwtCirce
.decodeRaw(jwt.value, publicKey, JwtOptions(expiration = false, notBefore = false))
Expand Down Expand Up @@ -370,7 +376,7 @@ object JwtPresentation {
)(didResolver: DidResolver, uriResolver: UriResolver)(implicit
clock: Clock
): IO[List[String], Validation[String, Unit]] = {
val validateJwtPresentation = Validation.fromTry(decodeJwt(jwt)).mapError(_.toString)
val validateJwtPresentation = Validation.fromTry(decodeJwt[JwtPresentationPayload](jwt)).mapError(_.toString)

val credentialValidationZIO =
ValidationUtils.foreach(
Expand Down Expand Up @@ -405,7 +411,7 @@ object JwtPresentation {
domain: String,
challenge: String
): Validation[String, Unit] = {
val validateJwtPresentation = Validation.fromTry(decodeJwt(jwt)).mapError(_.toString)
val validateJwtPresentation = Validation.fromTry(decodeJwt[JwtPresentationPayload](jwt)).mapError(_.toString)
for {
decodeJwtPresentation <- validateJwtPresentation
aud <- validateAudience(decodeJwtPresentation, Some(domain))
Expand All @@ -419,7 +425,7 @@ object JwtPresentation {
challenge: Option[String],
schemaIdAndTrustedIssuers: Seq[CredentialSchemaAndTrustedIssuersConstraint]
): Validation[String, Unit] = {
val validateJwtPresentation = Validation.fromTry(decodeJwt(jwt)).mapError(_.toString)
val validateJwtPresentation = Validation.fromTry(decodeJwt[JwtPresentationPayload](jwt)).mapError(_.toString)
for {
decodeJwtPresentation <- validateJwtPresentation
aud <- validateAudience(decodeJwtPresentation, domain)
Expand Down Expand Up @@ -451,15 +457,10 @@ object JwtPresentation {
} yield i

case (jwtVerifiableCredentialPayload: JwtVerifiableCredentialPayload) =>
val decodeJWT = (jwt: JWT) =>
Validation
.fromTry(JwtCirce.decodeRaw(jwt.value, options = JwtOptions(false, false, false)))
.mapError(_.getMessage)
for {
jwtCredentialDecoded <- decodeJWT(jwtVerifiableCredentialPayload.jwt)
jwtCredentialPayload <- Validation
.fromEither(decode[JwtCredentialPayload](jwtCredentialDecoded))
.mapError(_.getMessage)
.fromTry(decodeJwt[JwtCredentialPayload](jwtVerifiableCredentialPayload.jwt))
.mapError(_.toString)
issuer = jwtCredentialPayload.issuer
credentialSchemas = jwtCredentialPayload.maybeCredentialSchema
s <- validateSchemaIds(credentialSchemas, expectedSchemaIds)
Expand Down Expand Up @@ -520,10 +521,6 @@ object JwtPresentation {

def verifyHolderBinding(jwt: JWT): Validation[String, Unit] = {
import org.hyperledger.identus.pollux.vc.jwt.CredentialPayload.Implicits.*
val decodeJWT = (jwt: JWT) =>
Validation
.fromTry(JwtCirce.decodeRaw(jwt.value, options = JwtOptions(false, false, false)))
.mapError(_.getMessage)

def validateCredentialSubjectId(
vcList: IndexedSeq[VerifiableCredentialPayload],
Expand All @@ -546,10 +543,9 @@ object JwtPresentation {

case (jwtVerifiableCredentialPayload: JwtVerifiableCredentialPayload) =>
for {
jwtCredentialDecoded <- decodeJWT(jwtVerifiableCredentialPayload.jwt)
jwtCredentialPayload <- Validation
.fromEither(decode[JwtCredentialPayload](jwtCredentialDecoded))
.mapError(_.getMessage)
.fromTry(decodeJwt[JwtCredentialPayload](jwtVerifiableCredentialPayload.jwt))
.mapError(_.toString)
mayBeSubjectDid = jwtCredentialPayload.maybeSub
x <-
if (mayBeSubjectDid.contains(iss)) {
Expand All @@ -564,20 +560,15 @@ object JwtPresentation {
.map(_ => ())
}
for {
decodedJWT <- decodeJWT(jwt)
jwtPresentationPayload <- Validation.fromEither(decode[JwtPresentationPayload](decodedJWT)).mapError(_.getMessage)
jwtPresentationPayload <- Validation
.fromTry(decodeJwt[JwtPresentationPayload](jwt))
.mapError(_.toString)
result <- validateCredentialSubjectId(jwtPresentationPayload.vp.verifiableCredential, jwtPresentationPayload.iss)
} yield result
}

def verifyDates(jwt: JWT, leeway: TemporalAmount)(implicit clock: Clock): Validation[String, Unit] = {
val now = clock.instant()

val decodeJWT =
Validation
.fromTry(JwtCirce.decodeRaw(jwt.value, options = JwtOptions(false, false, false)))
.mapError(_.getMessage)

def validateNbfNotAfterExp(maybeNbf: Option[Instant], maybeExp: Option[Instant]): Validation[String, Unit] = {
val maybeResult =
for {
Expand Down Expand Up @@ -612,8 +603,9 @@ object JwtPresentation {
}

for {
decodedJWT <- decodeJWT
jwtCredentialPayload <- Validation.fromEither(decode[JwtPresentationPayload](decodedJWT)).mapError(_.getMessage)
jwtCredentialPayload <- Validation
.fromTry(decodeJwt[JwtPresentationPayload](jwt))
.mapError(_.toString)
maybeNbf = jwtCredentialPayload.maybeNbf
maybeExp = jwtCredentialPayload.maybeExp
result <- Validation.validateWith(
Expand Down

0 comments on commit e15e9c2

Please sign in to comment.