forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vdr.go
119 lines (96 loc) · 2.82 KB
/
vdr.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package httpbinding
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/common/log"
"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"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/kms"
)
var logger = log.New("aries-framework/vdr/httpbinding")
// VDR via HTTP(s) endpoint.
type VDR struct {
endpointURL string
client *http.Client
accept Accept
resolveAuthToken string
}
// Accept is method to accept did method.
type Accept func(method string) bool
// New creates new DID Resolver.
func New(endpointURL string, opts ...Option) (*VDR, error) {
v := &VDR{client: &http.Client{}, accept: func(method string) bool { return true }}
for _, opt := range opts {
opt(v)
}
// Validate host
_, err := url.ParseRequestURI(endpointURL)
if err != nil {
return nil, fmt.Errorf("base URL invalid: %w", err)
}
v.endpointURL = endpointURL
return v, nil
}
// Accept did method - attempt to resolve any method.
func (v *VDR) Accept(method string) bool {
return v.accept(method)
}
// Create did doc.
func (v *VDR) Create(keyManager kms.KeyManager, didDoc *did.Doc,
opts ...vdrapi.DIDMethodOption) (*did.DocResolution, error) {
return nil, fmt.Errorf("build not supported in http binding vdr")
}
// Close frees resources being maintained by vdr.
func (v *VDR) Close() error {
return nil
}
// Update did doc.
func (v *VDR) Update(didDoc *did.Doc, opts ...vdrapi.DIDMethodOption) error {
return fmt.Errorf("not supported")
}
// Deactivate did doc.
func (v *VDR) Deactivate(didID string, opts ...vdrapi.DIDMethodOption) error {
return fmt.Errorf("not supported")
}
// Option configures the peer vdr.
type Option func(opts *VDR)
// WithTimeout option is for definition of HTTP(s) timeout value of DID Resolver.
func WithTimeout(timeout time.Duration) Option {
return func(opts *VDR) {
opts.client.Timeout = timeout
}
}
// WithTLSConfig option is for definition of secured HTTP transport using a tls.Config instance.
func WithTLSConfig(tlsConfig *tls.Config) Option {
return func(opts *VDR) {
opts.client.Transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
}
}
// WithAccept option is for accept did method.
func WithAccept(accept Accept) Option {
return func(opts *VDR) {
opts.accept = accept
}
}
// WithResolveAuthToken add auth token for resolve.
func WithResolveAuthToken(authToken string) Option {
return func(opts *VDR) {
opts.resolveAuthToken = "Bearer " + authToken
}
}
func closeResponseBody(respBody io.Closer) {
e := respBody.Close()
if e != nil {
logger.Errorf("Failed to close response body: %v", e)
}
}