Skip to content

Commit

Permalink
fix(prism-agent): fix credential schema parsing as spec (#841)
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Voiturier <[email protected]>
  • Loading branch information
bvoiturier authored and CryptoKnightIOG committed Feb 1, 2024
1 parent 83b692d commit 2ec1997
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,19 @@ object CredentialSchema {
for {
uri <- ZIO.attempt(new URI(schemaId)).mapError(t => URISyntaxError(t.getMessage))
content <- uriDereferencer.dereference(uri).mapError(err => UnexpectedError(err.toString))
vcSchema <- parseCredentialSchema(content)
resolvedSchemaType <- resolveCredentialSchemaType(vcSchema.`type`)
_ <-
Validation
.fromPredicateWith(
CredentialSchemaParsingError(
s"Only ${CredentialJsonSchemaType.`type`} schema type can be used to verify claims"
)
)(resolvedSchemaType.`type`)(`type` => `type` == CredentialJsonSchemaType.`type`)
.toZIO
schemaValidator <- JsonSchemaValidatorImpl.from(vcSchema.schema).mapError(SchemaError.apply)
json <- ZIO
.fromEither(content.fromJson[Json])
.mapError(error =>
CredentialSchemaError.CredentialSchemaParsingError(s"Failed to parse resolved schema content as Json: $error")
)
schemaValidator <- JsonSchemaValidatorImpl
.from(json)
.orElse(
ZIO
.fromEither(json.as[CredentialSchema])
.mapError(error => CredentialSchemaParsingError(s"Failed to parse schema content as Json or OEA: $error"))
.flatMap(cs => JsonSchemaValidatorImpl.from(cs.schema).mapError(SchemaError.apply))
)
_ <- schemaValidator.validate(claims).mapError(SchemaError.apply)
} yield ()
}
Expand All @@ -148,20 +150,10 @@ object CredentialSchema {
for {
uri <- ZIO.attempt(new URI(schemaId)).mapError(t => URISyntaxError(t.getMessage))
content <- uriDereferencer.dereference(uri).mapError(err => UnexpectedError(err.toString))
vcSchema <- parseCredentialSchema(content)
resolvedSchemaType <- resolveCredentialSchemaType(vcSchema.`type`)
_ <-
Validation
.fromPredicateWith(
CredentialSchemaParsingError(
s"Only ${CredentialJsonSchemaType.`type`} schema type can be used to verify claims"
)
)(resolvedSchemaType.`type`)(`type` => `type` == AnoncredSchemaType.`type`)
.toZIO
validAttrNames <- ZIO
.fromEither(vcSchema.schema.as[AnoncredSchemaSerDesV1])
.fromEither(content.fromJson[AnoncredSchemaSerDesV1])
.mapError(error => CredentialSchemaParsingError(s"AnonCreds Schema parsing error: $error"))
.map(_.attrNames)
.mapError(err => UnexpectedError(err))
jsonClaims <- ZIO.fromEither(claims.fromJson[Json]).mapError(err => UnexpectedError(err))
_ <- jsonClaims match
case Json.Obj(fields) =>
Expand Down Expand Up @@ -193,8 +185,4 @@ object CredentialSchema {
} yield ()
}

def parseCredentialSchema(vcSchemaString: String): IO[CredentialSchemaError, CredentialSchema] =
ZIO
.fromEither(vcSchemaString.fromJson[CredentialSchema])
.mapError(error => CredentialSchemaParsingError(s"VC Schema parsing error: $error"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.iohk.atala.pollux.anoncreds.{AnoncredLib, SchemaDef}
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.URISyntaxError
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition.{Filter, FilteredEntries}
import io.iohk.atala.pollux.core.model.schema.CredentialSchema.parseCredentialSchema
import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1
import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError
import io.iohk.atala.pollux.core.model.schema.{CredentialDefinition, CredentialSchema}
Expand Down Expand Up @@ -36,8 +35,7 @@ class CredentialDefinitionServiceImpl(
for {
uri <- ZIO.attempt(new URI(in.schemaId))
content <- uriDereferencer.dereference(uri)
vcSchema <- parseCredentialSchema(content)
anoncredSchema <- AnoncredSchemaSerDesV1.schemaSerDes.deserialize(vcSchema.schema)
anoncredSchema <- AnoncredSchemaSerDesV1.schemaSerDes.deserialize(content)
anoncredLibSchema =
SchemaDef(
in.schemaId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import io.circe.syntax.*
import io.iohk.atala.mercury.model.*
import io.iohk.atala.mercury.protocol.presentproof.*
import io.iohk.atala.pollux.core.model.*
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.CredentialSchemaParsingError
import io.iohk.atala.pollux.core.model.error.PresentationError
import io.iohk.atala.pollux.core.model.error.PresentationError.*
import io.iohk.atala.pollux.core.model.presentation.*
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition
import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1
import io.iohk.atala.pollux.core.repository.{CredentialRepository, PresentationRepository}
import io.iohk.atala.pollux.vc.jwt.*
import io.iohk.atala.shared.models.WalletAccessContext
import io.iohk.atala.shared.utils.aspects.CustomMetricsAspect
import zio.*
import zio.{ZIO, *}
import zio.json.*

import java.rmi.UnexpectedException
import java.time.Instant
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "resource:///anoncred-presentation-schema-example.json",
"version": "1.0",
"attrNames": [
"name",
"sex",
"age"
],
"issuerId": "did:prism:issuer"
}
34 changes: 9 additions & 25 deletions pollux/lib/core/src/test/resources/anoncred-schema-example.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
{
"guid": "1631026d-5d55-3285-8ccd-bd70480cfbdc",
"id": "329da384-b2bb-497f-a605-4118dec75d31",
"longId": "did:prism:4a5b5cf0a513e83b598bbea25cd6196746747f361a73ef77068268bc9bd732ff/329da384-b2bb-497f-a605-4118dec75d31?version=5.0.0",
"name": "DrivingLicense",
"version": "5.0.0",
"tags": [
"string"
"name": "Driving licence Anoncred Schema",
"version": "1.0",
"attrNames": [
"emailAddress",
"familyName",
"dateOfIssuance",
"drivingLicenseID",
"drivingClass"
],
"description": "Simple credential schema for the driving licence verifiable credential.",
"type": "AnoncredSchemaV1",
"schema": {
"name": "Driving licence Anoncred Schema",
"version": "1.0",
"attrNames": [
"emailAddress",
"familyName",
"dateOfIssuance",
"drivingLicenseID",
"drivingClass"
],
"issuerId": "http://www.example.com/issuer"
},
"author": "did:prism:4a5b5cf0a513e83b598bbea25cd6196746747f361a73ef77068268bc9bd732ff",
"authored": "2023-04-06T08:48:01.654162Z",
"kind": "CredentialSchema",
"self": "/schema-registry/schemas/1631026d-5d55-3285-8ccd-bd70480cfbdc"
"issuerId": "http://www.example.com/issuer"
}
98 changes: 41 additions & 57 deletions pollux/lib/core/src/test/resources/vc-schema-example.json
Original file line number Diff line number Diff line change
@@ -1,60 +1,44 @@
{
"guid": "1631026d-5d55-3285-8ccd-bd70480cfbdc",
"id": "329da384-b2bb-497f-a605-4118dec75d31",
"longId": "did:prism:4a5b5cf0a513e83b598bbea25cd6196746747f361a73ef77068268bc9bd732ff/329da384-b2bb-497f-a605-4118dec75d31?version=5.0.0",
"name": "DrivingLicense",
"version": "5.0.0",
"tags": [
"string"
],
"description": "Simple credential schema for the driving licence verifiable credential.",
"type": "https://w3c-ccg.github.io/vc-json-schemas/schema/2.0/schema.json",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "Driving License",
"type": "object",
"properties": {
"credentialSubject": {
"type": "object",
"properties": {
"emailAddress": {
"type": "string",
"format": "email"
},
"givenName": {
"type": "string"
},
"familyName": {
"type": "string"
},
"dateOfIssuance": {
"type": "string",
"format": "date-time"
},
"drivingLicenseID": {
"type": "string"
},
"drivingClass": {
"type": "integer"
}
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "Driving License",
"type": "object",
"properties": {
"credentialSubject": {
"type": "object",
"properties": {
"emailAddress": {
"type": "string",
"format": "email"
},
"givenName": {
"type": "string"
},
"familyName": {
"type": "string"
},
"required": [
"emailAddress",
"familyName",
"dateOfIssuance",
"drivingLicenseID",
"drivingClass"
],
"additionalProperties": false
}
},
"required": [
"credentialSubject"
],
"additionalProperties": false
"dateOfIssuance": {
"type": "string",
"format": "date-time"
},
"drivingLicenseID": {
"type": "string"
},
"drivingClass": {
"type": "integer"
}
},
"required": [
"emailAddress",
"familyName",
"dateOfIssuance",
"drivingLicenseID",
"drivingClass"
],
"additionalProperties": false
}
},
"author": "did:prism:4a5b5cf0a513e83b598bbea25cd6196746747f361a73ef77068268bc9bd732ff",
"authored": "2023-04-06T08:48:01.654162Z",
"kind": "CredentialSchema",
"self": "/schema-registry/schemas/1631026d-5d55-3285-8ccd-bd70480cfbdc"
}
"required": [
"credentialSubject"
],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
{
"guid": "1631026d-5d55-3285-8ccd-bd70480cfbdc",
"id": "329da384-b2bb-497f-a605-4118dec75d31",
"longId": "did:prism:4a5b5cf0a513e83b598bbea25cd6196746747f361a73ef77068268bc9bd732ff/329da384-b2bb-497f-a605-4118dec75d31?version=5.0.0",
"name": "DrivingLicense",
"version": "5.0.0",
"tags": [
"string"
"name": "Driving licence Anoncred Schema",
"version": "1.0",
"attrNames": [
"emailAddress",
"familyName",
"dateOfIssuance",
"drivingLicenseID",
"drivingClass"
],
"description": "Simple credential schema for the driving licence verifiable credential.",
"type": "AnoncredSchemaV1",
"schema": {
"name": "Driving licence Anoncred Schema",
"version": "1.0",
"attrNames": [
"emailAddress",
"familyName",
"dateOfIssuance",
"drivingLicenseID",
"drivingClass"
],
"issuerId": "http://www.example.com/issuer"
},
"author": "did:prism:4a5b5cf0a513e83b598bbea25cd6196746747f361a73ef77068268bc9bd732ff",
"authored": "2023-04-06T08:48:01.654162Z",
"kind": "CredentialSchema",
"self": "/schema-registry/schemas/1631026d-5d55-3285-8ccd-bd70480cfbdc"
}
"issuerId": "http://www.example.com/issuer"
}
Original file line number Diff line number Diff line change
@@ -1,60 +1,44 @@
{
"guid": "1631026d-5d55-3285-8ccd-bd70480cfbdc",
"id": "329da384-b2bb-497f-a605-4118dec75d31",
"longId": "did:prism:4a5b5cf0a513e83b598bbea25cd6196746747f361a73ef77068268bc9bd732ff/329da384-b2bb-497f-a605-4118dec75d31?version=5.0.0",
"name": "DrivingLicense",
"version": "5.0.0",
"tags": [
"string"
],
"description": "Simple credential schema for the driving licence verifiable credential.",
"type": "https://w3c-ccg.github.io/vc-json-schemas/schema/2.0/schema.json",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "Driving License",
"type": "object",
"properties": {
"credentialSubject": {
"type": "object",
"properties": {
"emailAddress": {
"type": "string",
"format": "email"
},
"givenName": {
"type": "string"
},
"familyName": {
"type": "string"
},
"dateOfIssuance": {
"type": "string",
"format": "date-time"
},
"drivingLicenseID": {
"type": "string"
},
"drivingClass": {
"type": "integer"
}
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "Driving License",
"type": "object",
"properties": {
"credentialSubject": {
"type": "object",
"properties": {
"emailAddress": {
"type": "string",
"format": "email"
},
"givenName": {
"type": "string"
},
"familyName": {
"type": "string"
},
"required": [
"emailAddress",
"familyName",
"dateOfIssuance",
"drivingLicenseID",
"drivingClass"
],
"additionalProperties": false
}
},
"required": [
"credentialSubject"
],
"additionalProperties": false
"dateOfIssuance": {
"type": "string",
"format": "date-time"
},
"drivingLicenseID": {
"type": "string"
},
"drivingClass": {
"type": "integer"
}
},
"required": [
"emailAddress",
"familyName",
"dateOfIssuance",
"drivingLicenseID",
"drivingClass"
],
"additionalProperties": false
}
},
"author": "did:prism:4a5b5cf0a513e83b598bbea25cd6196746747f361a73ef77068268bc9bd732ff",
"authored": "2023-04-06T08:48:01.654162Z",
"kind": "CredentialSchema",
"self": "/schema-registry/schemas/1631026d-5d55-3285-8ccd-bd70480cfbdc"
}
"required": [
"credentialSubject"
],
"additionalProperties": false
}
Loading

0 comments on commit 2ec1997

Please sign in to comment.