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

feat: ATL-4888 Anoncred schema type #590

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package io.iohk.atala.pollux.core.model.schema

import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.*
import io.iohk.atala.pollux.core.model.schema.`type`.{CredentialJsonSchemaType, CredentialSchemaType}
import io.iohk.atala.pollux.core.model.schema.`type`.{
AnoncredSchemaType,
CredentialJsonSchemaType,
CredentialSchemaType
}
import io.iohk.atala.pollux.core.model.schema.validator.CredentialJsonSchemaValidator
import io.iohk.atala.pollux.core.service.URIDereferencer
import zio.*
import zio.json.*
import zio.prelude.Validation

import java.net.URI
import java.time.{OffsetDateTime, ZoneOffset}
Expand Down Expand Up @@ -119,12 +125,21 @@ object CredentialSchema {
content <- uriDereferencer.dereference(uri).mapError(err => UnexpectedError(err.toString))
vcSchema <- parseCredentialSchema(content)
resolvedSchemaType <- resolveCredentialSchemaType(vcSchema.`type`)
_ <- resolvedSchemaType.validateClaims(vcSchema.schema, claims)
_ <-
Validation
.fromPredicateWith(
CredentialSchemaParsingError(
s"Only ${CredentialJsonSchemaType.`type`} schema type can be used to verify claims"
)
)(resolvedSchemaType.`type`)(`type` => `type` == CredentialJsonSchemaType.`type`)
.toZIO
schemaValidator <- CredentialJsonSchemaValidator.from(vcSchema.schema)
_ <- schemaValidator.validate(claims)
} yield ()
}

private val supportedCredentialSchemaTypes: Map[String, CredentialSchemaType] =
IndexedSeq(CredentialJsonSchemaType)
IndexedSeq(CredentialJsonSchemaType, AnoncredSchemaType)
.map(credentialSchemaType => (credentialSchemaType.`type`, credentialSchemaType))
.toMap

Expand All @@ -137,7 +152,7 @@ object CredentialSchema {
def validateCredentialSchema(vcSchema: CredentialSchema): IO[CredentialSchemaError, Unit] = {
for {
resolvedSchemaType <- resolveCredentialSchemaType(vcSchema.`type`)
_ <- resolvedSchemaType.toSchemaValidator(vcSchema.schema)
_ <- resolvedSchemaType.validate(vcSchema.schema)
} yield ()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.iohk.atala.pollux.core.model.schema.common

import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.networknt.schema.*
import com.networknt.schema.SpecVersion.VersionFlag
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.*
import zio.*
import zio.json.ast.Json

object JsonSchemaUtils {
def jsonSchema(
schema: String,
supportedVersions: IndexedSeq[VersionFlag] = IndexedSeq.empty
): IO[CredentialSchemaError, JsonSchema] = {
for {
jsonSchemaNode <- toJsonNode(schema)
specVersion <- ZIO
.attempt(SpecVersionDetector.detect(jsonSchemaNode))
.mapError(t => UnexpectedError(t.getMessage))
_ <-
if (supportedVersions.nonEmpty && !supportedVersions.contains(specVersion))
ZIO.fail(
UnsupportedJsonSchemaSpecVersion(
s"Unsupported JsonSchemaVersion. Current:$specVersion ExpectedOneOf:${supportedVersions.map(_.getId)}"
)
)
else ZIO.unit
mapper <- ZIO.attempt(new ObjectMapper()).mapError(t => UnexpectedError(t.getMessage))
factory <- ZIO
.attempt(JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(specVersion)).objectMapper(mapper).build)
.mapError(t => UnexpectedError(t.getMessage))
jsonSchema <- ZIO.attempt(factory.getSchema(jsonSchemaNode)).mapError(t => UnexpectedError(t.getMessage))
} yield jsonSchema
}

def from(
schema: Json,
supportedVersions: IndexedSeq[VersionFlag] = IndexedSeq.empty
): IO[CredentialSchemaError, JsonSchema] = {
jsonSchema(schema.toString(), supportedVersions)
}

def toJsonNode(json: Json): IO[CredentialSchemaError, JsonNode] = {
toJsonNode(json.toString())
}

def toJsonNode(json: String): IO[CredentialSchemaError, JsonNode] = {
for {
mapper <- ZIO.attempt(new ObjectMapper()).mapError(t => UnexpectedError(t.getMessage))
jsonSchemaNode <- ZIO
.attempt(mapper.readTree(json))
.mapError(t => JsonSchemaParsingError(t.getMessage))
} yield jsonSchemaNode
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.iohk.atala.pollux.core.model.schema.`type`

import com.networknt.schema.*
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.schema.Schema
import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.{AnoncredSchemaV1, AnoncredSchemaVersion}
import io.iohk.atala.pollux.core.model.schema.common.JsonSchemaUtils
import io.iohk.atala.pollux.core.model.schema.validator.CredentialJsonSchemaValidator
import zio.*
import zio.json.*

object AnoncredSchemaType extends CredentialSchemaType {
val `type`: String = "Anoncred"
private val anoncredSchemaVersion: AnoncredSchemaVersion = AnoncredSchemaV1

override def validate(schema: Schema): IO[CredentialSchemaError, Unit] = {
for {
jsonSchemaSchema <- anoncredSchemaVersion.initialiseJsonSchema
schemaValidator = CredentialJsonSchemaValidator(jsonSchemaSchema)
jsonSchemaNode <- JsonSchemaUtils.toJsonNode(schema)
_ <- schemaValidator.validate(jsonSchemaNode)
} yield ()
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.iohk.atala.pollux.core.model.schema.`type`

import com.fasterxml.jackson.databind.ObjectMapper
import com.networknt.schema.*
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.*
import io.iohk.atala.pollux.core.model.schema.Schema
import io.iohk.atala.pollux.core.model.schema.validator.CredentialJsonSchemaValidator
import zio.*
Expand All @@ -14,23 +11,8 @@ object CredentialJsonSchemaType extends CredentialSchemaType {

override val `type`: String = VC_JSON_SCHEMA_URI

override def toSchemaValidator(schema: Schema): IO[CredentialSchemaError, CredentialJsonSchemaValidator] = {
override def validate(schema: Schema): IO[CredentialSchemaError, Unit] =
for {
mapper <- ZIO.attempt(new ObjectMapper()).mapError(t => UnexpectedError(t.getMessage))
jsonSchemaNode <- ZIO
.attempt(mapper.readTree(schema.toString()))
.mapError(t => JsonSchemaParsingError(t.getMessage))
specVersion <- ZIO
.attempt(SpecVersionDetector.detect(jsonSchemaNode))
.mapError(t => UnexpectedError(t.getMessage))
_ <-
if (specVersion != SpecVersion.VersionFlag.V202012)
ZIO.fail(UnsupportedJsonSchemaSpecVersion(s"Version should be ${JsonMetaSchema.getV202012.getUri}"))
else ZIO.unit
factory <- ZIO
.attempt(JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(specVersion)).objectMapper(mapper).build)
.mapError(t => UnexpectedError(t.getMessage))
jsonSchema <- ZIO.attempt(factory.getSchema(jsonSchemaNode)).mapError(t => UnexpectedError(t.getMessage))
} yield CredentialJsonSchemaValidator(jsonSchema)
}
_ <- CredentialJsonSchemaValidator.from(schema)
} yield ()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ package io.iohk.atala.pollux.core.model.schema.`type`

import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.schema.Schema
import io.iohk.atala.pollux.core.model.schema.validator.CredentialSchemaValidator
import zio.IO

trait CredentialSchemaType {
val `type`: String

def toSchemaValidator(schema: Schema): IO[CredentialSchemaError, CredentialSchemaValidator]

def validateClaims(schema: Schema, claims: String): IO[CredentialSchemaError, Unit] = {
for {
schemaValidator <- toSchemaValidator(schema)
validatedClaims <- schemaValidator.validate(claims)
} yield validatedClaims
}
def validate(schema: Schema): IO[CredentialSchemaError, Unit]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.iohk.atala.pollux.core.model.schema.`type`.anoncred

import com.networknt.schema.*
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.schema.common.JsonSchemaUtils
import zio.*

object AnoncredSchemaV1 extends AnoncredSchemaVersion {
private val jsonSchemaSchemaStr: String =
"""
|{
| "$schema": "http://json-schema.org/draft-07/schema#",
| "type": "object",
| "properties": {
| "name": {
| "type": "string",
| "minLength": 1
| },
| "version": {
| "type": "string",
| "minLength": 1
| },
| "attrNames": {
| "type": "array",
| "items": {
| "type": "string",
| "minLength": 1
| },
| "minItems": 1,
| "maxItems": 125,
| "uniqueItems": true
| },
| "issuerId": {
| "type": "string",
| "minLength": 1
| }
| },
| "required": ["name", "version", "attrNames", "issuerId"]
|}
|""".stripMargin

override def initialiseJsonSchema: IO[CredentialSchemaError, JsonSchema] =
JsonSchemaUtils.jsonSchema(jsonSchemaSchemaStr)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.iohk.atala.pollux.core.model.schema.`type`.anoncred

import com.networknt.schema.JsonSchema
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import zio.IO

trait AnoncredSchemaVersion {
def initialiseJsonSchema: IO[CredentialSchemaError, JsonSchema]
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package io.iohk.atala.pollux.core.model.schema.validator

import com.fasterxml.jackson.databind.ObjectMapper
import com.networknt.schema.*
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.*
import io.iohk.atala.pollux.core.model.schema.Schema
import io.iohk.atala.pollux.core.model.schema.common.JsonSchemaUtils
import zio.*

case class CredentialJsonSchemaValidator(schemaValidator: JsonSchema) extends CredentialSchemaValidator {
override def validate(claims: String): IO[CredentialSchemaError, Unit] = {
import scala.jdk.CollectionConverters.*
for {
mapper <- ZIO.attempt(new ObjectMapper()).mapError(t => UnexpectedError(t.getMessage))

// Convert claims to JsonNode
jsonClaims <- ZIO
.attempt(mapper.readTree(claims))
.mapError(t => ClaimsParsingError(t.getMessage))
jsonClaims <- JsonSchemaUtils.toJsonNode(claims)

// Validate claims JsonNode
validationMessages <- ZIO
Expand All @@ -29,3 +26,11 @@ case class CredentialJsonSchemaValidator(schemaValidator: JsonSchema) extends Cr
}

}

object CredentialJsonSchemaValidator {
def from(schema: Schema): IO[CredentialSchemaError, CredentialJsonSchemaValidator] = {
for {
jsonSchema <- JsonSchemaUtils.from(schema, IndexedSeq(SpecVersion.VersionFlag.V202012))
} yield CredentialJsonSchemaValidator(jsonSchema)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package io.iohk.atala.pollux.core.model.schema.validator

import com.fasterxml.jackson.databind.JsonNode
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import zio.*

trait CredentialSchemaValidator {
def validate(claims: String): IO[CredentialSchemaError, Unit]

def validate(claimsJsonNode: JsonNode): IO[CredentialSchemaError, Unit] = {
validate(claimsJsonNode.toString)
}
}
Loading