Skip to content

Commit

Permalink
uptake latest spec changes for json schema (#448)
Browse files Browse the repository at this point in the history
* uptake latest spec changes

* lint
  • Loading branch information
decentralgabe authored Aug 18, 2023
1 parent 0c788e2 commit 6e10433
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 63 deletions.
2 changes: 2 additions & 0 deletions credential/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
VerifiableCredentialsLinkedDataContext string = "https://www.w3.org/2018/credentials/v1"
VerifiableCredentialType string = "VerifiableCredential"
VerifiableCredentialIDProperty string = "id"
// VerifiableCredentialJSONSchemaProperty as defined by https://www.w3.org/TR/vc-json-schema/#jsonschemacredential
VerifiableCredentialJSONSchemaProperty string = "jsonSchema"
VerifiablePresentationType string = "VerifiablePresentation"

BuilderEmptyError string = "builder cannot be empty"
Expand Down
8 changes: 8 additions & 0 deletions credential/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func (cs CredentialSubject) GetID() string {
return id
}

func (cs CredentialSubject) GetJSONSchema() map[string]any {
var schema map[string]any
if gotSchema, ok := cs[VerifiableCredentialJSONSchemaProperty]; ok {
schema = gotSchema.(map[string]any)
}
return schema
}

type CredentialSchema struct {
ID string `json:"id" validate:"required"`
Type string `json:"type" validate:"required"`
Expand Down
9 changes: 5 additions & 4 deletions credential/schema/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (ra *RemoteAccess) GetVCJSONSchema(ctx context.Context, t VCJSONSchemaType,
}

switch t {
case CredentialSchema2023Type:
case JSONSchemaCredentialType:
// either a jwt or credential json
var schemaCred any
if err = json.NewDecoder(resp.Body).Decode(&schemaCred); err != nil {
Expand All @@ -64,16 +64,17 @@ func (ra *RemoteAccess) GetVCJSONSchema(ctx context.Context, t VCJSONSchemaType,
if err != nil {
return nil, errors.Wrap(err, "error decoding schema from credential")
}
credSubjectBytes, err := json.Marshal(cred.CredentialSubject)
jsonSchemaValue := cred.CredentialSubject.GetJSONSchema()
credSubjectBytes, err := json.Marshal(jsonSchemaValue)
if err != nil {
return nil, errors.Wrap(err, "error marshalling credential subject")
return nil, errors.Wrap(err, "error marshalling jsonSchema property in the credential subject")
}
var schema JSONSchema
if err = json.Unmarshal(credSubjectBytes, &schema); err != nil {
return nil, errors.Wrap(err, "error unmarshalling credential subject to schema")
}
return schema, nil
case JSONSchema2023Type:
case JSONSchemaType:
var schema JSONSchema
if err = json.NewDecoder(resp.Body).Decode(&schema); err != nil {
return nil, errors.Wrap(err, "error decoding schema")
Expand Down
12 changes: 6 additions & 6 deletions credential/schema/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

func TestRemoteAccess(t *testing.T) {
remoteAccess := NewRemoteAccess(nil)
schema, err := getTestVector(jsonSchema2023Schema1)
schema, err := getTestVector(jsonSchemaSchema1)
require.NoError(t, err)

schemaCred, err := getTestVector(credentialSchema2023Schema1)
schemaCred, err := getTestVector(jsonSchemaCredentialSchema1)
require.NoError(t, err)

t.Run("access JsonSchema2023", func(t *testing.T) {
Expand All @@ -25,7 +25,7 @@ func TestRemoteAccess(t *testing.T) {
Reply(200).BodyString(schema)
defer gock.Off()

jsonSchema, err := remoteAccess.GetVCJSONSchema(context.Background(), JSONSchema2023Type, "https://example.com/schemas/email.json")
jsonSchema, err := remoteAccess.GetVCJSONSchema(context.Background(), JSONSchemaType, "https://example.com/schemas/email.json")
assert.NoError(t, err)
assert.JSONEq(t, schema, jsonSchema.String())
})
Expand All @@ -36,7 +36,7 @@ func TestRemoteAccess(t *testing.T) {
Reply(200).BodyString(schema)
defer gock.Off()

cred, err := getTestVector(jsonSchema2023Credential1)
cred, err := getTestVector(jsonSchemaCredential1)
assert.NoError(t, err)

var vc credential.VerifiableCredential
Expand All @@ -53,7 +53,7 @@ func TestRemoteAccess(t *testing.T) {
Reply(200).BodyString(schemaCred)
defer gock.Off()

jsonSchema, err := remoteAccess.GetVCJSONSchema(context.Background(), JSONSchema2023Type, "https://example.com/credentials/3734")
jsonSchema, err := remoteAccess.GetVCJSONSchema(context.Background(), JSONSchemaType, "https://example.com/credentials/3734")
assert.NoError(t, err)
assert.JSONEq(t, schemaCred, jsonSchema.String())
})
Expand All @@ -64,7 +64,7 @@ func TestRemoteAccess(t *testing.T) {
Reply(200).BodyString(schemaCred)
defer gock.Off()

cred, err := getTestVector(credentialSchema2023Credential1)
cred, err := getTestVector(jsonSchemaCredentialCredential1)
assert.NoError(t, err)

var vc credential.VerifiableCredential
Expand Down
10 changes: 5 additions & 5 deletions credential/schema/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

const (
// CredentialSchema2023Type https://www.w3.org/TR/vc-json-schema/#credentialschema2023
CredentialSchema2023Type VCJSONSchemaType = "CredentialSchema2023"
// JSONSchema2023Type https://www.w3.org/TR/vc-json-schema/#jsonschema2023
JSONSchema2023Type VCJSONSchemaType = "JsonSchema2023"
// JSONSchemaCredentialType https://www.w3.org/TR/vc-json-schema/#jsonschemacredential
JSONSchemaCredentialType VCJSONSchemaType = "JsonSchemaCredential"
// JSONSchemaType https://www.w3.org/TR/vc-json-schema/#jsonschema
JSONSchemaType VCJSONSchemaType = "JsonSchema"

Draft202012 JSONSchemaVersion = "https://json-schema.org/draft/2020-12/schema"
Draft201909 JSONSchemaVersion = "https://json-schema.org/draft/2019-09/schema"
Expand Down Expand Up @@ -114,5 +114,5 @@ func IsSupportedVCJSONSchemaType(t string) bool {

// GetSupportedVCJSONSchemaTypes returns the supported VC JSON Schema types
func GetSupportedVCJSONSchemaTypes() []VCJSONSchemaType {
return []VCJSONSchemaType{CredentialSchema2023Type, JSONSchema2023Type}
return []VCJSONSchemaType{JSONSchemaCredentialType, JSONSchemaType}
}
29 changes: 0 additions & 29 deletions credential/schema/testdata/credentialschema2023-schema-1.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
},
"credentialSchema": {
"id": "https://example.com/schemas/email.json",
"type": "JsonSchema2023"
"type": "JsonSchema"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$id": "https://example.com/schemas/email.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"name": "EmailCredential",
"description": "EmailCredential using JsonSchema2023",
"description": "EmailCredential using JsonSchema",
"type": "object",
"properties": {
"credentialSubject": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"https://www.w3.org/ns/credentials/examples/v2"
],
"id": "https://example.com/credentials/3733",
"type": ["VerifiableCredential", "EmailCredential"],
"type": ["VerifiableCredential", "ExampleEmailCredential"],
"issuer": "https://example.com/issuers/14",
"issuanceDate": "2010-01-01T19:23:24Z",
"credentialSubject": {
Expand All @@ -13,6 +13,6 @@
},
"credentialSchema": {
"id": "https://example.com/credentials/3734",
"type": "CredentialSchema2023"
"type": "JsonSchemaCredential"
}
}
33 changes: 33 additions & 0 deletions credential/schema/testdata/jsonschemacredential-schema-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"@context": [
"https://www.w3.org/ns/credentials/v2",
"https://www.w3.org/ns/credentials/examples/v2"
],
"id": "https://example.com/credentials/3734",
"type": ["VerifiableCredential", "JsonSchemaCredential"],
"issuer": "https://example.com/issuers/14",
"issuanceDate": "2010-01-01T19:23:24Z",
"credentialSubject": {
"id": "https://example.com/schemas/email-credential-schema.json",
"type": "JsonSchema",
"jsonSchema": {
"$id": "https://example.com/schemas/email-credential-schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"name": "EmailCredential",
"description": "EmailCredential using JsonSchemaCredential",
"type": "object",
"properties": {
"credentialSubject": {
"type": "object",
"properties": {
"emailAddress": {
"type": "string",
"format": "email"
}
},
"required": ["emailAddress"]
}
}
}
}
}
24 changes: 12 additions & 12 deletions credential/schema/vcjsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
)

const (
jsonSchema2023Credential1 string = "jsonschema2023-credential-1.json"
jsonSchema2023Schema1 string = "jsonschema2023-schema-1.json"
credentialSchema2023Credential1 string = "credentialschema2023-credential-1.json"
credentialSchema2023Schema1 string = "credentialschema2023-schema-1.json"
jsonSchemaCredential1 string = "jsonschema-credential-1.json"
jsonSchemaSchema1 string = "jsonschema-schema-1.json"
jsonSchemaCredentialCredential1 string = "jsonschemacredential-credential-1.json"
jsonSchemaCredentialSchema1 string = "jsonschemacredential-schema-1.json"
)

var (
Expand All @@ -24,8 +24,8 @@ var (
)

func TestValidateCredentialAgainstSchema(t *testing.T) {
t.Run("validate credential against JsonSchema2023", func(t *testing.T) {
cred, err := getTestVector(jsonSchema2023Credential1)
t.Run("validate credential against JsonSchema", func(t *testing.T) {
cred, err := getTestVector(jsonSchemaCredential1)
assert.NoError(t, err)

var vc credential.VerifiableCredential
Expand All @@ -36,8 +36,8 @@ func TestValidateCredentialAgainstSchema(t *testing.T) {
assert.NoError(t, err)
})

t.Run("validate credential against CredentialSchema2023", func(t *testing.T) {
cred, err := getTestVector(credentialSchema2023Credential1)
t.Run("validate credential against JsonSchemaCredential", func(t *testing.T) {
cred, err := getTestVector(jsonSchemaCredentialCredential1)
assert.NoError(t, err)

var vc credential.VerifiableCredential
Expand All @@ -57,24 +57,24 @@ func (localAccess) GetVCJSONSchema(_ context.Context, _ VCJSONSchemaType, id str
var err error
switch id {
case "https://example.com/schemas/email.json":
schema, err = getTestVector(jsonSchema2023Schema1)
schema, err = getTestVector(jsonSchemaSchema1)
if err != nil {
return nil, err
}
case "https://example.com/credentials/3734":
schemaCred, err := getTestVector(credentialSchema2023Schema1)
schemaCred, err := getTestVector(jsonSchemaCredentialSchema1)
if err != nil {
return nil, err
}
var cred credential.VerifiableCredential
if err = json.Unmarshal([]byte(schemaCred), &cred); err != nil {
return nil, err
}
credSubjectBytes, err := json.Marshal(cred.CredentialSubject)
jsonSchemaBytes, err := json.Marshal(cred.CredentialSubject.GetJSONSchema())
if err != nil {
return nil, errors.Wrap(err, "error marshalling credential subject")
}
schema = string(credSubjectBytes)
schema = string(jsonSchemaBytes)
}
if err = json.Unmarshal([]byte(schema), &s); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion credential/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestValidator(t *testing.T) {
// validate cred with schema, no schema passed in
sampleCredential.CredentialSchema = &credential.CredentialSchema{
ID: "did:example:MDP8AsFhHzhwUvGNuYkX7T;id=06e126d1-fa44-4882-a243-1e326fbe21db;version=1.0",
Type: credschema.JSONSchema2023Type.String(),
Type: credschema.JSONSchemaType.String(),
}
err = validator.ValidateCredential(sampleCredential)
assert.Error(tt, err)
Expand Down
4 changes: 2 additions & 2 deletions example/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func issueApplicationCredential(id key.DIDKey, s schema.JSONSchema) (*credential

if err := builder.SetCredentialSchema(credential.CredentialSchema{
ID: s.ID(),
Type: schema.JSONSchema2023Type.String(),
Type: schema.JSONSchemaType.String(),
}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -280,7 +280,7 @@ func issueDriversLicenseCredential(issuerDID key.DIDKey, subjectDID string, s sc

if err := builder.SetCredentialSchema(credential.CredentialSchema{
ID: s.ID(),
Type: schema.JSONSchema2023Type.String(),
Type: schema.JSONSchemaType.String(),
}); err != nil {
return nil, err
}
Expand Down

0 comments on commit 6e10433

Please sign in to comment.