-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ATL-4888 Anoncred schema type (#590)
* feat: ATL-4888 Anoncred Schema * feat: ATL-4888 Added test * feat: ATL-4888 Include version in type
- Loading branch information
1 parent
cf207fc
commit a57deef
Showing
12 changed files
with
605 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...b/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/common/JsonSchemaUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/AnoncredSchemaType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...n/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSchemaV1.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
11 changes: 11 additions & 0 deletions
11
...la/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSchemaVersion.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...in/scala/io/iohk/atala/pollux/core/model/schema/validator/CredentialSchemaValidator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.