forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
245 lines (185 loc) · 7.44 KB
/
command.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package connection
import (
"encoding/json"
"fmt"
"io"
"github.com/hyperledger/aries-framework-go/pkg/client/connection"
"github.com/hyperledger/aries-framework-go/pkg/common/log"
"github.com/hyperledger/aries-framework-go/pkg/controller/command"
"github.com/hyperledger/aries-framework-go/pkg/controller/internal/cmdutil"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/middleware"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr"
"github.com/hyperledger/aries-framework-go/pkg/internal/logutil"
"github.com/hyperledger/aries-framework-go/pkg/kms"
"github.com/hyperledger/aries-framework-go/pkg/store/did"
"github.com/hyperledger/aries-framework-go/spi/storage"
)
var logger = log.New("aries-framework/controller/connection")
// constants for connection management endpoints.
const (
CommandName = "connection"
RotateDIDCommandMethod = "RotateDID"
CreateV2CommandMethod = "CreateConnectionV2"
SetToV2CommandMethod = "SetConnectionToDIDCommV2"
errEmptyConnID = "empty connection ID"
errEmptyKID = "empty signing KID"
errEmptyNewDID = "empty new DID"
errEmptyMyDID = "empty my DID"
errEmptyTheirDID = "empty their DID"
// log constants.
connectionIDString = "connectionID"
newDIDString = "newDID"
successString = "success"
)
const (
// InvalidRequestErrorCode is typically a code for validation errors
// for invalid connection controller requests.
InvalidRequestErrorCode = command.Code(iota + command.Connection)
// CreateConnectionErrorCode is for failures in create connection command.
CreateConnectionErrorCode
// SetToDIDCommV2ErrorCode is for failures in create peer DID command.
SetToDIDCommV2ErrorCode
// RotateDIDErrorCode is for failures in rotate DID command.
RotateDIDErrorCode
)
type provider interface {
VDRegistry() vdr.Registry
DIDRotator() *middleware.DIDCommMessageMiddleware
StorageProvider() storage.Provider
ProtocolStateStorageProvider() storage.Provider
DIDConnectionStore() did.ConnectionStore
KMS() kms.KeyManager
KeyType() kms.KeyType
KeyAgreementType() kms.KeyType
}
// Command provides controller API for connection commands.
type Command struct {
client *connection.Client
}
// New creates connection Command.
func New(prov provider) (*Command, error) {
client, err := connection.New(prov)
if err != nil {
return nil, fmt.Errorf("creating client: %w", err)
}
return &Command{
client: client,
}, nil
}
// GetHandlers returns list of all commands supported by this controller command.
func (c *Command) GetHandlers() []command.Handler {
return []command.Handler{
cmdutil.NewCommandHandler(CommandName, RotateDIDCommandMethod, c.RotateDID),
cmdutil.NewCommandHandler(CommandName, CreateV2CommandMethod, c.CreateConnectionV2),
cmdutil.NewCommandHandler(CommandName, SetToV2CommandMethod, c.SetConnectionToDIDCommV2),
}
}
// CreateConnectionV2 creates a DIDComm v2 connection.
func (c *Command) CreateConnectionV2(rw io.Writer, req io.Reader) command.Error {
var request CreateConnectionRequest
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, CreateV2CommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
if request.MyDID == "" {
logutil.LogDebug(logger, CommandName, CreateV2CommandMethod, errEmptyMyDID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyMyDID))
}
if request.TheirDID == "" {
logutil.LogDebug(logger, CommandName, CreateV2CommandMethod, errEmptyTheirDID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyTheirDID))
}
connID, err := c.client.CreateConnectionV2(request.MyDID, request.TheirDID)
if err != nil {
logutil.LogInfo(logger, CommandName, CreateV2CommandMethod, err.Error())
return command.NewExecuteError(CreateConnectionErrorCode, err)
}
command.WriteNillableResponse(rw, &IDMessage{
ConnectionID: connID,
}, logger)
logutil.LogDebug(logger, CommandName, CreateV2CommandMethod, successString,
logutil.CreateKeyValueString(connectionIDString, connID))
return nil
}
// SetConnectionToDIDCommV2 sets that a connection is using didcomm v2, and associated versions of protocols.
func (c *Command) SetConnectionToDIDCommV2(_ io.Writer, req io.Reader) command.Error {
var request IDMessage
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, SetToV2CommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
if request.ConnectionID == "" {
logutil.LogInfo(logger, CommandName, SetToV2CommandMethod, errEmptyConnID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyConnID))
}
err = c.client.SetConnectionToDIDCommV2(request.ConnectionID)
if err != nil {
logutil.LogInfo(logger, CommandName, SetToV2CommandMethod, err.Error())
return command.NewExecuteError(SetToDIDCommV2ErrorCode, err)
}
logutil.LogDebug(logger, CommandName, SetToV2CommandMethod, successString)
return nil
}
// RotateDIDGivenConnIDCmd takes a connection ID and returns a command.Exec that rotates the given connection's DID.
func (c *Command) RotateDIDGivenConnIDCmd(connID string) command.Exec {
return func(rw io.Writer, req io.Reader) command.Error {
var request RotateDIDRequest
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, RotateDIDCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
request.ID = connID
return c.rotateDID(rw, &request)
}
}
// RotateDID handles a didcomm v2 DID rotation request.
func (c *Command) RotateDID(rw io.Writer, req io.Reader) command.Error {
var request RotateDIDRequest
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, RotateDIDCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
return c.rotateDID(rw, &request)
}
func (c *Command) rotateDID(rw io.Writer, request *RotateDIDRequest) command.Error {
if request.ID == "" {
logutil.LogDebug(logger, CommandName, RotateDIDCommandMethod, errEmptyConnID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyConnID))
}
if request.KID == "" {
logutil.LogDebug(logger, CommandName, RotateDIDCommandMethod, errEmptyKID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyKID))
}
if request.NewDID == "" && !request.CreatePeerDID {
logutil.LogDebug(logger, CommandName, RotateDIDCommandMethod, errEmptyNewDID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyNewDID))
}
var opts []connection.RotateDIDOption
if request.NewDID != "" {
opts = append(opts, connection.WithNewDID(request.NewDID))
}
if request.CreatePeerDID {
opts = append(opts, connection.ByCreatingPeerDID())
}
newDID, err := c.client.RotateDID(request.ID, request.KID, opts...)
if err != nil {
logutil.LogDebug(logger, CommandName, RotateDIDCommandMethod, err.Error())
return command.NewExecuteError(RotateDIDErrorCode, err)
}
command.WriteNillableResponse(rw, &RotateDIDResponse{
NewDID: newDID,
}, logger)
logutil.LogDebug(logger, CommandName, RotateDIDCommandMethod, successString,
logutil.CreateKeyValueString(connectionIDString, request.ID),
logutil.CreateKeyValueString(newDIDString, request.NewDID),
)
return nil
}