Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSON-LD graph in document @context #92

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion did/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 @@ -55,8 +55,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