Skip to content
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

chore: remove dead code SchemaValidator #1296

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,6 @@ lazy val D_Pollux = new {

lazy val D_Pollux_VC_JWT = new {

private lazy val circeJsonSchema = ("net.reactivecore" %% "circe-json-schema" % "0.4.1")
.cross(CrossVersion.for3Use2_13)
.exclude("io.circe", "circe-core_2.13")
.exclude("io.circe", "circe-generic_2.13")
.exclude("io.circe", "circe-parser_2.13")

val zio = "dev.zio" %% "zio" % V.zio
val zioPrelude = "dev.zio" %% "zio-prelude" % V.zioPreludeVersion

Expand All @@ -321,7 +315,7 @@ lazy val D_Pollux_VC_JWT = new {
// Dependency Modules
val zioDependencies: Seq[ModuleID] = Seq(zio, zioPrelude, zioTest, zioTestSbt, zioTestMagnolia)
val baseDependencies: Seq[ModuleID] =
zioDependencies :+ D.jwtCirce :+ circeJsonSchema :+ networkntJsonSchemaValidator :+ D.nimbusJwt :+ D.scalaTest
zioDependencies :+ D.jwtCirce :+ networkntJsonSchemaValidator :+ D.nimbusJwt :+ D.scalaTest

// Project Dependencies
lazy val polluxVcJwtDependencies: Seq[ModuleID] = baseDependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.circe.parser.decode
import io.circe.syntax.*
import org.hyperledger.identus.castor.core.model.did.VerificationRelationship
import org.hyperledger.identus.pollux.vc.jwt.revocation.BitString
import org.hyperledger.identus.pollux.vc.jwt.schema.{SchemaResolver, SchemaValidator}
import org.hyperledger.identus.shared.crypto.{KmpSecp256k1KeyOps, PublicKey as ApolloPublicKey}
import org.hyperledger.identus.shared.http.UriResolver
import org.hyperledger.identus.shared.utils.Base64Utils
Expand Down Expand Up @@ -170,69 +169,6 @@ object CredentialPayloadValidation {
)
)
}

def validateCredentialSchema(
maybeCredentialSchema: Option[Json]
)(schemaToValidator: Json => Validation[String, SchemaValidator]): Validation[String, Option[SchemaValidator]] = {
maybeCredentialSchema.fold(Validation.succeed(Option.empty))(credentialSchema => {
schemaToValidator(credentialSchema).map(Some(_))
})
}

def validateCredentialSubjectSchema(
credentialSubject: Json,
credentialSchemaValidator: SchemaValidator
): Validation[String, Json] =
credentialSchemaValidator.validate(credentialSubject)

def validateCredentialSubject(
credentialSubject: Json,
maybeCredentialSchemaValidator: Option[SchemaValidator]
): Validation[String, Json] = {
for {
validatedCredentialSubjectNotEmpty <- validateCredentialSubjectNotEmpty(credentialSubject)
validatedCredentialSubjectHasId <- validateCredentialSubjectHasId(validatedCredentialSubjectNotEmpty)
validatedCredentialSubjectSchema <- maybeCredentialSchemaValidator
.map(validateCredentialSubjectSchema(validatedCredentialSubjectHasId, _))
.getOrElse(Validation.succeed(validatedCredentialSubjectHasId))
} yield validatedCredentialSubjectSchema
}

def validate[C <: CredentialPayload](credentialPayload: C): Validation[String, C] =
Validation.validateWith(
CredentialPayloadValidation.validateContext(credentialPayload.`@context`),
CredentialPayloadValidation.validateVcType(credentialPayload.`type`)
) { (`@context`, `type`) => credentialPayload }

def validateSchema[C <: CredentialPayload](credentialPayload: C)(schemaResolver: SchemaResolver)(
schemaToValidator: Json => Validation[String, SchemaValidator]
): IO[String, C] =
val validation =
for {
resolvedSchema <- ZIO.foreach(credentialPayload.maybeCredentialSchema)(schemaResolver.resolve)
maybeDocumentValidator <- CredentialPayloadValidation
.validateCredentialSchema(resolvedSchema)(schemaToValidator)
.toZIO
maybeValidatedCredentialSubject <- CredentialPayloadValidation
.validateCredentialSubject(
credentialPayload.credentialSubject,
maybeDocumentValidator
)
.toZIO
} yield maybeValidatedCredentialSubject
validation.map(_ => credentialPayload)

private def validateCredentialSubjectNotEmpty(credentialSubject: Json): Validation[String, Json] = {
Validation
.fromPredicateWith("credentialSubject is empty.")(credentialSubject)(_.isObject)
}

private def validateCredentialSubjectHasId(credentialSubject: Json): Validation[String, Json] = {
Validation
.fromPredicateWith("credentialSubject must contain id.")(credentialSubject)(
_.asObject.exists(jsonObject => jsonObject.toMap.contains("id"))
)
}
}

case class JwtVc(
Expand Down Expand Up @@ -796,28 +732,6 @@ object JwtCredential {
)(_.iss)
}

def validateJwtSchema(
jwt: JWT
)(schemaResolver: SchemaResolver)(
schemaToValidator: Json => Validation[String, SchemaValidator]
): IO[String, Validation[String, Unit]] = {
val decodeJWT =
Validation.fromTry(JwtCirce.decodeRawAll(jwt.value, JwtOptions(false, false, false))).mapError(_.getMessage)

val validatedDecodedClaim: Validation[String, JwtCredentialPayload] =
for {
decodedJwtTask <- decodeJWT
(_, claim, _) = decodedJwtTask
decodedClaim <- Validation.fromEither(decode[JwtCredentialPayload](claim).left.map(_.toString))
} yield decodedClaim

ValidationUtils.foreach(
validatedDecodedClaim.map(decodedClaim =>
CredentialPayloadValidation.validateSchema(decodedClaim)(schemaResolver)(schemaToValidator)
)
)(_.replicateZIODiscard(1))
}

def validateExpiration(jwt: JWT, dateTime: OffsetDateTime): Validation[String, Unit] = {
Validation
.fromTry(
Expand All @@ -838,19 +752,6 @@ object JwtCredential {
.mapError(_.getMessage)
}

def validateSchemaAndSignature(
jwt: JWT
)(didResolver: DidResolver)(schemaResolver: SchemaResolver)(
schemaToValidator: Json => Validation[String, SchemaValidator]
): IO[String, Validation[String, Unit]] = {
for {
validatedJwtSchema <- validateJwtSchema(jwt)(schemaResolver)(schemaToValidator)
validateJwtSignature <- validateEncodedJWT(jwt)(didResolver)
} yield {
Validation.validateWith(validatedJwtSchema, validateJwtSignature)((a, _) => a)
}
}

def verifyDates(jwtPayload: JwtVerifiableCredentialPayload, leeway: TemporalAmount)(implicit
clock: Clock
): Validation[String, Unit] = {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading