Skip to content

Commit

Permalink
feat: ATL-4888 Anoncred schema type (#590)
Browse files Browse the repository at this point in the history
* feat: ATL-4888 Anoncred Schema

* feat: ATL-4888 Added test

* feat: ATL-4888 Include version in type
  • Loading branch information
CryptoKnightIOG authored Jul 13, 2023
1 parent cf207fc commit a57deef
Show file tree
Hide file tree
Showing 12 changed files with 605 additions and 41 deletions.
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,25 @@
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.{AnoncredSchemaSchemaV1, AnoncredSchemaSchemaVersion}
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 {

private val anoncredSchemaSchemaVersion: AnoncredSchemaSchemaVersion = AnoncredSchemaSchemaV1
val `type`: String = AnoncredSchemaSchemaV1.version

override def validate(schema: Schema): IO[CredentialSchemaError, Unit] = {
for {
jsonSchemaSchema <- anoncredSchemaSchemaVersion.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,45 @@
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 AnoncredSchemaSchemaV1 extends AnoncredSchemaSchemaVersion {
val version: String = AnoncredSchemaSchemaV1.getClass.getSimpleName
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,11 @@
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 AnoncredSchemaSchemaVersion {
val version: String

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

0 comments on commit a57deef

Please sign in to comment.