diff --git a/did/document.go b/did/document.go index a8544bc..ac5cd60 100644 --- a/did/document.go +++ b/did/document.go @@ -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"` diff --git a/did/document_test.go b/did/document_test.go index c57dca6..bb041f5 100644 --- a/did/document_test.go +++ b/did/document_test.go @@ -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) { diff --git a/did/validator.go b/did/validator.go index 74553d9..eb2ad2e 100644 --- a/did/validator.go +++ b/did/validator.go @@ -3,6 +3,7 @@ package did import ( "errors" "fmt" + ssi "github.com/nuts-foundation/go-did" "strings" ) @@ -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` @@ -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 } } diff --git a/did/validator_test.go b/did/validator_test.go index 0b0bc24..2cc4997 100644 --- a/did/validator_test.go +++ b/did/validator_test.go @@ -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) { @@ -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},