forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
97 lines (76 loc) · 2.36 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package httpbinding
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/doc/did"
vdrapi "github.com/Universal-Health-Chain/aries-framework-go/pkg/framework/aries/api/vdr"
)
const (
didLDJson = "application/did+ld+json"
)
// resolveDID makes DID resolution via HTTP.
func (v *VDR) resolveDID(uri string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("HTTP create get request failed: %w", err)
}
req.Header.Add("Accept", didLDJson)
if v.resolveAuthToken != "" {
req.Header.Add("Authorization", v.resolveAuthToken)
}
resp, err := v.client.Do(req)
if err != nil {
return nil, fmt.Errorf("HTTP Get request failed: %w", err)
}
defer closeResponseBody(resp.Body)
var gotBody []byte
gotBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response body failed: %w", err)
}
if resp.StatusCode == http.StatusOK && strings.Contains(resp.Header.Get("Content-type"), didLDJson) {
return gotBody, nil
} else if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("DID does not exist for request: %s", uri)
}
return nil, fmt.Errorf("unsupported response from DID resolver [%v] header [%s] body [%s]",
resp.StatusCode, resp.Header.Get("Content-type"), gotBody)
}
// Read implements didresolver.DidMethod.Read interface (https://w3c-ccg.github.io/did-resolution/#resolving-input)
func (v *VDR) Read(didID string, _ ...vdrapi.ResolveOption) (*did.DocResolution, error) {
reqURL, err := url.ParseRequestURI(v.endpointURL)
if err != nil {
return nil, fmt.Errorf("url parse request uri failed: %w", err)
}
reqURL.Path = path.Join(reqURL.Path, didID)
data, err := v.resolveDID(reqURL.String())
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, vdrapi.ErrNotFound
}
documentResolution, err := did.ParseDocumentResolution(data)
if err != nil {
if !errors.Is(err, did.ErrDIDDocumentNotExist) {
return nil, err
}
logger.Warnf("parse document resolution failed %w", err)
} else {
return documentResolution, nil
}
didDoc, err := did.ParseDocument(data)
if err != nil {
return nil, err
}
return &did.DocResolution{DIDDocument: didDoc}, nil
}