Skip to content

Commit

Permalink
Support JSON-LD graph in document @context (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul authored Oct 31, 2023
1 parent 4c1e711 commit 79414dd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion did/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ParseDocument(raw string) (*Document, error) {

// Document represents a DID Document as specified by the DID Core specification (https://www.w3.org/TR/did-core/).
type Document struct {
Context []ssi.URI `json:"@context"`
Context []interface{} `json:"@context"`
ID DID `json:"id"`
Controller []DID `json:"controller,omitempty"`
AlsoKnownAs []ssi.URI `json:"alsoKnownAs,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions did/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func Test_Document(t *testing.T) {
return
}
expected := "https://www.w3.org/ns/did/v1"
if expected != actual.Context[0].String() {
t.Errorf("expected:\n%s\n, got:\n%s", expected, actual.Context[0].String())
if expected != actual.Context[0].(string) {
t.Errorf("expected:\n%s\n, got:\n%s", expected, actual.Context[0].(string))
}
})
t.Run("validate controllers", func(t *testing.T) {
Expand Down
18 changes: 15 additions & 3 deletions did/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package did
import (
"errors"
"fmt"
ssi "github.com/nuts-foundation/go-did"
"strings"
)

Expand Down Expand Up @@ -106,7 +107,7 @@ type baseValidator struct{}

func (w baseValidator) Validate(document Document) error {
// Verify `@context`
if !containsContext(document, DIDContextV1) {
if !containsContextURI(document, DIDContextV1) {
return makeValidationError(ErrInvalidContext)
}
// Verify `id`
Expand Down Expand Up @@ -186,9 +187,20 @@ func (s serviceValidator) Validate(document Document) error {
return nil
}

func containsContext(document Document, ctx string) bool {
func containsContextURI(document Document, ctx string) bool {
for _, curr := range document.Context {
if curr.String() == ctx {
var currStr string
var ok bool
if currStr, ok = curr.(string); !ok {
// or it might be ssi.URI
if currURI, ok := curr.(ssi.URI); ok {
currStr = currURI.String()
} else {
// can't compare this context entry (might be a JSON-LD graph)
continue
}
}
if currStr == ctx {
return true
}
}
Expand Down
14 changes: 12 additions & 2 deletions did/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ func TestW3CSpecValidator(t *testing.T) {
}
t.Run("context is missing DIDv1", func(t *testing.T) {
input := document()
input.Context = []ssi.URI{}
input.Context = []interface{}{
"someting-else",
map[string]interface{}{
"@base": "did:example:123",
},
}
assertIsError(t, ErrInvalidContext, W3CSpecValidator{}.Validate(input))
})
t.Run("invalid ID - is empty", func(t *testing.T) {
Expand Down Expand Up @@ -169,7 +174,12 @@ func document() Document {
serviceID := *did
serviceID.Fragment = "service-1"
doc := Document{
Context: []ssi.URI{DIDContextV1URI()},
Context: []interface{}{
DIDContextV1URI(),
map[string]interface{}{
"@base": "did:example:12345",
},
},
ID: *did,
Controller: []DID{*did},
VerificationMethod: []*VerificationMethod{vm},
Expand Down

0 comments on commit 79414dd

Please sign in to comment.