-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
192 lines (171 loc) · 6.4 KB
/
web.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package web
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/goccy/go-json"
"github.com/pkg/errors"
"github.com/cyware/ssi-sdk/crypto"
"github.com/cyware/ssi-sdk/did"
"github.com/cyware/ssi-sdk/did/resolution"
"github.com/cyware/ssi-sdk/util"
)
// did:web method specification https://w3c-ccg.github.io/did-method-web/
// DID Web create and resolve methods are implemented in this package but NOT the update and deactivate methods
// please refer to web_test.go for example and test cases
type (
DIDWeb string
)
const (
WellKnownURLPath = ".well-known/"
DIDDocFilename = "did.json"
Prefix = "did:web"
)
// Validate return nil if DID is valid, otherwise the validation error.
func (d DIDWeb) Validate(ctx context.Context) error {
docBytes, header, err := d.resolveDocBytes(ctx)
if err != nil {
return errors.Wrap(err, "resolving doc bytes")
}
contentType := header.Get("Content-Type")
if contentType != util.JSONContentType {
return errors.Errorf("header Content-Type received was `%s` but expected `%s`", contentType, "application/json")
}
var doc did.Document
if err := json.Unmarshal(docBytes, &doc); err != nil {
return errors.Wrap(err, "unmarshalling received bytes")
}
return doc.IsValid()
}
func (d DIDWeb) String() string {
return string(d)
}
func (d DIDWeb) Suffix() (string, error) {
split := strings.Split(d.String(), Prefix+":")
if len(split) != 2 {
return "", errors.Wrap(util.InvalidFormatError, "did is malformed")
}
return split[1], nil
}
func (DIDWeb) Method() did.Method {
return did.WebMethod
}
// CreateDoc constructs a did:web Document from a specific key type and its corresponding public key. This method
// does not attempt to validate that the provided public key is of the specified key type. The returned Document is
// expected further turned into a JSON file named did.json and stored under the expected path of the target web domain
// specification: https://w3c-ccg.github.io/did-method-web/#create-register
func (d DIDWeb) CreateDoc(kt crypto.KeyType, publicKey []byte) (*did.Document, error) {
didWebStr := string(d)
keyReference := didWebStr + "#owner"
verificationMethod, err := did.ConstructJWKVerificationMethod(keyReference, didWebStr, publicKey, kt)
if err != nil {
return nil, fmt.Errorf("could not construct verification method for DIDWeb %+v", d)
}
verificationMethodSet := []did.VerificationMethodSet{
[]string{keyReference},
}
return &did.Document{
Context: did.KnownDIDContext,
ID: didWebStr,
VerificationMethod: []did.VerificationMethod{*verificationMethod},
Authentication: verificationMethodSet,
AssertionMethod: verificationMethodSet,
}, nil
}
// CreateDocBytes simply takes the output from CreateDoc and returns the bytes of the JSON DID document
func (d DIDWeb) CreateDocBytes(kt crypto.KeyType, publicKey []byte) ([]byte, error) {
doc, err := d.CreateDoc(kt, publicKey)
if err != nil {
return nil, errors.Wrapf(err, "could not create DID Document for did:web DID %+v", d)
}
return json.Marshal(doc)
}
// GetDocURL returns the expected URL of the DID Document where https:// prefix is required by the specification
// optional path supported
func (d DIDWeb) GetDocURL() (string, error) {
// DIDWeb must be prefixed with d:web:
if !strings.HasPrefix(string(d), Prefix) {
return "", fmt.Errorf("did:web DID %+v is missing prefix %s", d, Prefix)
}
subStrs := strings.Split(string(d), ":")
numSubStrs := len(subStrs)
if numSubStrs < 3 || len(subStrs[2]) < 1 {
return "", fmt.Errorf("did:web DID %+v is missing the required domain", d)
}
// Specification https://w3c-ccg.github.io/did-method-web/#read-resolve
// 2. If the domain contains a port percent decode the colon.
decodedDomain, err := url.QueryUnescape(subStrs[2])
if err != nil {
return "", errors.Wrapf(err, "url.QueryUnescape failed for subStr %s", subStrs[2])
}
// 3. Generate an HTTPS URL to the expected location of the DID document by prepending https://.
if numSubStrs == 3 {
// 4. If no path has been specified in the URL, append /.well-known.
// 5. Append /d.json to complete the URL.
urlStr := "https://" + decodedDomain + "/" + WellKnownURLPath + DIDDocFilename
return urlStr, nil
}
// https://w3c-ccg.github.io/did-method-web/#optional-path-considerations
// Optional Path Considerations
var sb strings.Builder
if _, err = sb.WriteString("https://" + decodedDomain + "/"); err != nil {
return "", err
}
for i := 3; i < numSubStrs; i++ {
str, err := url.QueryUnescape(subStrs[i])
if err != nil {
return "", errors.Wrapf(err, "url.QueryUnescape failed for subStr %s", subStrs[i])
}
if _, err = sb.WriteString(str + "/"); err != nil {
return "", err
}
}
if _, err := sb.WriteString(DIDDocFilename); err != nil {
return "", err
}
return sb.String(), nil
}
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)
}
resolutionResult, err := resolution.ParseDIDResolution(docBytes)
if err != nil {
return nil, errors.Wrapf(err, "resolving did:web DID<%s>", d)
}
if resolutionResult.ID != d.String() {
return nil, fmt.Errorf("doc.id<%s> does not match did:web value<%s>", resolutionResult.ID, d)
}
return &resolutionResult.Document, nil
}
// 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(ctx context.Context) ([]byte, http.Header, error) {
docURL, err := d.GetDocURL()
if err != nil {
return nil, 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.
req, err := http.NewRequestWithContext(ctx, http.MethodGet, docURL, nil)
if err != nil {
return nil, nil, errors.Wrapf(err, "constructing doc request %+v", docURL)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, nil, errors.Wrapf(err, "getting doc %+v", docURL)
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, errors.Wrapf(err, "reading response %+v", resp)
}
return body, resp.Header, nil
}