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

Refactor IsValid() in did/web/web.go #399

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions did/web/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func (Resolver) Methods() []did.Method {

// Resolve fetches and returns the Document from the expected URL
// specification: https://w3c-ccg.github.io/did-method-web/#read-resolve
func (Resolver) Resolve(_ context.Context, id string, _ ...resolution.Option) (*resolution.Result, error) {
func (Resolver) Resolve(ctx context.Context, id string, _ ...resolution.Option) (*resolution.Result, error) {
if !strings.HasPrefix(id, WebPrefix) {
return nil, fmt.Errorf("not a did:web DID: %s", id)
}
didWeb := DIDWeb(id)
doc, err := didWeb.Resolve()
doc, err := didWeb.Resolve(ctx)
if err != nil {
return nil, errors.Wrapf(err, "cresolving did:web DID: %s", id)
}
Expand Down
23 changes: 16 additions & 7 deletions did/web/web.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -29,9 +30,13 @@ const (
WebPrefix = "did:web"
)

func (d DIDWeb) IsValid() bool {
_, err := d.resolveDocBytes()
return err == nil
// Validate return nil if DID is valid, otherwise the validation error.
func (d DIDWeb) Validate(ctx context.Context) error {
_, err := d.resolveDocBytes(ctx)
if err != nil {
return errors.Wrap(err, "failed to validate DID")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return errors.Wrap(err, "failed to validate DID")
return errors.Wrap(err, "resolving doc bytes")

}
return nil
}

func (d DIDWeb) String() string {
Expand Down Expand Up @@ -135,8 +140,8 @@ func (d DIDWeb) GetDocURL() (string, error) {
return sb.String(), nil
}

func (d DIDWeb) Resolve() (*did.Document, error) {
docBytes, err := d.resolveDocBytes()
func (d DIDWeb) Resolve(ctx context.Context) (*did.Document, error) {
docBytes, err := d.resolveDocBytes(ctx)
if err != nil {
return nil, errors.Wrapf(err, "resolving did:web DID<%s>", d)
}
Expand All @@ -152,15 +157,19 @@ func (d DIDWeb) Resolve() (*did.Document, error) {

// resolveDocBytes simply performs a http.Get on the expected URL of the DID Document from GetDocURL
// and returns the bytes of the fetched file
func (d DIDWeb) resolveDocBytes() ([]byte, error) {
func (d DIDWeb) resolveDocBytes(ctx context.Context) ([]byte, error) {
docURL, err := d.GetDocURL()
if err != nil {
return nil, errors.Wrapf(err, "getting doc url %+v", d)
}
// Specification https://w3c-ccg.github.io/did-method-web/#read-resolve
// 6. Perform an HTTP GET request to the URL using an agent that can successfully negotiate a secure HTTPS
// connection, which enforces the security requirements as described in 2.5 Security and privacy considerations.
resp, err := http.Get(docURL) // #nosec
req, err := http.NewRequestWithContext(ctx, http.MethodGet, docURL, nil)
if err != nil {
return nil, errors.Wrapf(err, "constructing doc request %+v", docURL)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "getting doc %+v", docURL)
}
Expand Down
31 changes: 26 additions & 5 deletions did/web/web_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"context"
"testing"

"gopkg.in/h2non/gock.v1"
Expand Down Expand Up @@ -55,13 +56,13 @@ func TestDIDWebResolveDocBytes(t *testing.T) {
BodyString(`{"didDocument": {"id": "did:web:demo.ssi-sdk.com"}}`)
defer gock.Off()

docBytes, err := didWebToBeResolved.resolveDocBytes()
docBytes, err := didWebToBeResolved.resolveDocBytes(context.Background())
assert.NoError(tt, err)
assert.Contains(tt, string(docBytes), "did:web:demo.ssi-sdk.com")
})

t.Run("Unresolvable Path", func(tt *testing.T) {
_, err := didWebNotADomain.resolveDocBytes()
_, err := didWebNotADomain.resolveDocBytes(context.Background())
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "did:web: is missing the required domain")
})
Expand All @@ -75,7 +76,7 @@ func TestDIDWebResolve(t *testing.T) {
BodyString(`{"didDocument": {"id": "did:web:demo.ssi-sdk.com"}}`)
defer gock.Off()

doc, err := didWebToBeResolved.Resolve()
doc, err := didWebToBeResolved.Resolve(context.Background())
assert.NoError(tt, err)
assert.Equal(tt, string(didWebToBeResolved), doc.ID)
})
Expand All @@ -87,13 +88,13 @@ func TestDIDWebResolve(t *testing.T) {
BodyString(`{"didDocument": {"id": "did:web:demo.ssi-sdk.com"}}`)
defer gock.Off()

_, err := didWebCannotBeResolved.Resolve()
_, err := didWebCannotBeResolved.Resolve(context.Background())
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "doc.id<did:web:demo.ssi-sdk.com> does not match did:web value<did:web:doesnotexist.com>")
})

t.Run("Unhappy Path - Unknown DID", func(t *testing.T) {
_, err := didWebCannotBeResolved.Resolve()
_, err := didWebCannotBeResolved.Resolve(context.Background())
assert.Error(t, err)
})
}
Expand Down Expand Up @@ -135,3 +136,23 @@ func TestDIDWebCreateDocFileBytes(t *testing.T) {
assert.Error(tt, err)
})
}

func TestDIDWebValidate(t *testing.T) {
t.Run("Happy Path - Validate DID", func(tt *testing.T) {
gock.New("https://demo.ssi-sdk.com").
Get("/.well-known/did.json").
Reply(200).
BodyString(`{"didDocument": {"id": "did:web:demo.ssi-sdk.com"}}`)
defer gock.Off()

err := didWebToBeResolved.Validate(context.Background())
assert.NoError(tt, err)
})

t.Run("Unresolvable Path - Validate DID", func(tt *testing.T) {
err := didWebNotADomain.Validate(context.Background())
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "failed to validate")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using

Suggested change
assert.Contains(tt, err.Error(), "failed to validate")
assert.ErrorContains(tt, err, "failed to validate")

assert.Contains(tt, err.Error(), "did:web: is missing the required domain")
})
}