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

chore: check did web id against did #32

Merged
merged 1 commit into from
Oct 15, 2024
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
6 changes: 5 additions & 1 deletion method/web/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
var errorLogger = log.New(os.Stderr, " [did-go/vdr/web] ", log.Ldate|log.Ltime|log.LUTC)

// Read resolves a did:web did.
func (v *VDR) Read(didID string, opts ...vdrapi.DIDMethodOption) (*did.DocResolution, error) {
func (v *VDR) Read(didID string, opts ...vdrapi.DIDMethodOption) (*did.DocResolution, error) { //nolint: gocyclo
httpClient := &http.Client{}

didOpts := &vdrapi.DIDMethodOpts{Values: make(map[string]interface{})}
Expand Down Expand Up @@ -80,6 +80,10 @@ func (v *VDR) Read(didID string, opts ...vdrapi.DIDMethodOption) (*did.DocResolu
return nil, fmt.Errorf("error resolving did:web did --> error parsing did doc --> %w", err)
}

if doc.ID != didID {
return nil, fmt.Errorf("did id %s not matching did %s", doc.ID, didID)
}

return &did.DocResolution{DIDDocument: doc}, nil
}

Expand Down
42 changes: 32 additions & 10 deletions method/web/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (

validDoc = `{
"@context": ["https://w3id.org/did/v1"],
"id": "did:web:www.example.org"
"id": "%s"
}`

invalidDoc = `{}`
Expand Down Expand Up @@ -104,29 +104,46 @@ func TestResolveDID(t *testing.T) {
})
t.Run("test resolve did success", func(t *testing.T) {
s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(validDoc))
data := fmt.Sprintf(validDoc, "did:web:"+urlapi.QueryEscape(r.Host))
_, err := w.Write([]byte(data))
require.NoError(t, err)
}))
defer s.Close()
did := fmt.Sprintf("did:web:%s", urlapi.QueryEscape(strings.TrimPrefix(s.URL, "https://")))
v := New()
docResolution, err := v.Read(did, vdrapi.WithOption(HTTPClientOpt, s.Client()))
require.Nil(t, err)
expectedDoc, err := didapi.ParseDocument([]byte(validDoc))
data := fmt.Sprintf(validDoc, did)
expectedDoc, err := didapi.ParseDocument([]byte(data))
require.Nil(t, err)
require.Equal(t, expectedDoc, docResolution.DIDDocument)
})
t.Run("test resolve with wrong did id", func(t *testing.T) {
s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := fmt.Sprintf(validDoc, "did:web:123")
_, err := w.Write([]byte(data))
require.NoError(t, err)
}))
defer s.Close()
did := fmt.Sprintf("did:web:%s", urlapi.QueryEscape(strings.TrimPrefix(s.URL, "https://")))
v := New()
doc, err := v.Read(did, vdrapi.WithOption(HTTPClientOpt, s.Client()))
require.Nil(t, doc)
require.ErrorContains(t, err, "did id did:web:123 not matching did")
})
t.Run("test resolve did with path success", func(t *testing.T) {
s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(validDoc))
data := fmt.Sprintf(validDoc, "did:web:"+urlapi.QueryEscape(r.Host)+":user:example")
_, err := w.Write([]byte(data))
require.NoError(t, err)
}))
defer s.Close()
did := fmt.Sprintf("did:web:%s:user:example", urlapi.QueryEscape(strings.TrimPrefix(s.URL, "https://")))
v := New()
docResolution, err := v.Read(did, vdrapi.WithOption(HTTPClientOpt, s.Client()))
require.Nil(t, err)
expectedDoc, err := didapi.ParseDocument([]byte(validDoc))
data := fmt.Sprintf(validDoc, did)
expectedDoc, err := didapi.ParseDocument([]byte(data))
require.Nil(t, err)
require.Equal(t, expectedDoc, docResolution.DIDDocument)
})
Expand All @@ -153,8 +170,8 @@ func TestResolveDomain(t *testing.T) {
http.NotFound(w, r)
return
}

_, err := w.Write(aliceDoc)
data := fmt.Sprintf(string(aliceDoc), "did:web:"+urlapi.QueryEscape(r.Host))
_, err := w.Write([]byte(data))
require.NoError(t, err)
}))
defer s.Close()
Expand All @@ -165,7 +182,9 @@ func TestResolveDomain(t *testing.T) {
v := New()
docResolution, err := v.Read(did, vdrapi.WithOption(HTTPClientOpt, s.Client()))
require.Nil(t, err)
expectedDoc, err := didapi.ParseDocument(aliceDoc)
data := fmt.Sprintf(string(aliceDoc), did)

expectedDoc, err := didapi.ParseDocument([]byte(data))
require.Nil(t, err)
require.Equal(t, expectedDoc, docResolution.DIDDocument)
})
Expand All @@ -181,7 +200,8 @@ func TestResolveWebFixtures(t *testing.T) {
return
}

_, err := w.Write(aliceDoc)
data := fmt.Sprintf(string(aliceDoc), "did:web:"+urlapi.QueryEscape(r.Host)+":alice")
_, err := w.Write([]byte(data))
require.NoError(t, err)
}))
defer s.Close()
Expand All @@ -192,7 +212,9 @@ func TestResolveWebFixtures(t *testing.T) {
v := New()
docResolution, err := v.Read(did, vdrapi.WithOption(HTTPClientOpt, s.Client()))
require.Nil(t, err)
expectedDoc, err := didapi.ParseDocument(aliceDoc)
data := fmt.Sprintf(string(aliceDoc), did)

expectedDoc, err := didapi.ParseDocument([]byte(data))
require.Nil(t, err)
require.Equal(t, expectedDoc, docResolution.DIDDocument)
})
Expand Down
4 changes: 2 additions & 2 deletions method/web/testdata/alice/did.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"@context": "https://w3id.org/did/v0.11",
"id": "did:web:did.actor:alice",
"id": "%s",
"publicKey": [
{
"id": "did:web:did.actor:alice#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN",
Expand Down Expand Up @@ -29,4 +29,4 @@
"publicKeyBase58": "CaSHXEvLKS6SfN9aBfkVGBpp15jSnaHazqHgLHp8KZ3Y"
}
]
}
}
Loading