forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
498 lines (378 loc) · 16.8 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package didexchange
import (
"encoding/json"
"fmt"
"io"
"strings"
"github.com/hyperledger/aries-framework-go/pkg/client/didexchange"
"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/controller/webnotifier"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
protocol "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/didexchange"
"github.com/hyperledger/aries-framework-go/pkg/doc/did"
"github.com/hyperledger/aries-framework-go/pkg/internal/logutil"
"github.com/hyperledger/aries-framework-go/pkg/kms"
"github.com/hyperledger/aries-framework-go/spi/storage"
)
var logger = log.New("aries-framework/controller/did-exchange")
// constants for endpoints of DIDExchange.
const (
CommandName = "didexchange"
// error messages.
errEmptyInviterDID = "empty inviter DID"
errEmptyConnID = "empty connection ID"
AcceptExchangeRequestCommandMethod = "AcceptExchangeRequest"
AcceptInvitationCommandMethod = "AcceptInvitation"
CreateImplicitInvitationCommandMethod = "CreateImplicitInvitation"
CreateInvitationCommandMethod = "CreateInvitation"
QueryConnectionByIDCommandMethod = "QueryConnectionByID"
QueryConnectionsCommandMethod = "QueryConnections"
ReceiveInvitationCommandMethod = "ReceiveInvitation"
CreateConnectionCommandMethod = "CreateConnection"
RemoveConnectionCommandMethod = "RemoveConnection"
// log constants.
connectionIDString = "connectionID"
successString = "success"
invitationIDString = "invitationID"
)
const (
// InvalidRequestErrorCode is typically a code for validation errors
// for invalid didexchange controller requests.
InvalidRequestErrorCode = command.Code(iota + command.DIDExchange)
// CreateInvitationErrorCode is for failures in create invitation command.
CreateInvitationErrorCode
// CreateImplicitInvitationErrorCode is for failures in create implicit invitation command.
CreateImplicitInvitationErrorCode
// ReceiveInvitationErrorCode is for failures in receive invitation command.
ReceiveInvitationErrorCode
// AcceptInvitationErrorCode is for failures in accept invitation command.
AcceptInvitationErrorCode
// AcceptExchangeRequestErrorCode is for failures in accept exchange request command.
AcceptExchangeRequestErrorCode
// QueryConnectionsErrorCode is for failures in query connection command.
QueryConnectionsErrorCode
// RemoveConnectionErrorCode is for failures in remove connection command.
RemoveConnectionErrorCode
// CreateConnectionErrorCode is for failures in create connection command.
CreateConnectionErrorCode
_actions = "_actions"
_states = "_states"
)
// provider contains dependencies for the DID Exchange command and is typically created by using aries.Context().
type provider interface {
Service(id string) (interface{}, error)
KMS() kms.KeyManager
ServiceEndpoint() string
StorageProvider() storage.Provider
ProtocolStateStorageProvider() storage.Provider
KeyType() kms.KeyType
KeyAgreementType() kms.KeyType
MediaTypeProfiles() []string
}
// New returns new DID Exchange controller command instance.
func New(ctx provider, notifier command.Notifier, defaultLabel string, autoAccept bool) (*Command, error) {
didExchange, err := didexchange.New(ctx)
if err != nil {
return nil, err
}
// creates action channel
actions := make(chan service.DIDCommAction)
// registers action channel to listen for events
if err := didExchange.RegisterActionEvent(actions); err != nil {
return nil, fmt.Errorf("register action event: %w", err)
}
// creates state channel
states := make(chan service.StateMsg)
// registers state channel to listen for events
if err := didExchange.RegisterMsgEvent(states); err != nil {
return nil, fmt.Errorf("register msg event: %w", err)
}
subscribers := []chan service.DIDCommAction{
make(chan service.DIDCommAction),
}
if autoAccept {
subscribers = append(subscribers, make(chan service.DIDCommAction))
go service.AutoExecuteActionEvent(subscribers[1])
}
go func() {
for action := range actions {
for i := range subscribers {
action.Message = action.Message.Clone()
subscribers[i] <- action
}
}
}()
obs := webnotifier.NewObserver(notifier)
obs.RegisterAction(protocol.DIDExchange+_actions, subscribers[0])
obs.RegisterStateMsg(protocol.DIDExchange+_states, states)
cmd := &Command{
ctx: ctx,
client: didExchange,
msgCh: make(chan service.StateMsg),
defaultLabel: defaultLabel,
}
return cmd, nil
}
// Command is controller command for DID Exchange.
type Command struct {
ctx provider
client *didexchange.Client
msgCh chan service.StateMsg
defaultLabel string
}
// GetHandlers returns list of all commands supported by this controller command.
func (c *Command) GetHandlers() []command.Handler {
return []command.Handler{
cmdutil.NewCommandHandler(CommandName, CreateInvitationCommandMethod, c.CreateInvitation),
cmdutil.NewCommandHandler(CommandName, ReceiveInvitationCommandMethod, c.ReceiveInvitation),
cmdutil.NewCommandHandler(CommandName, AcceptInvitationCommandMethod, c.AcceptInvitation),
cmdutil.NewCommandHandler(CommandName, CreateConnectionCommandMethod, c.CreateConnection),
cmdutil.NewCommandHandler(CommandName, RemoveConnectionCommandMethod, c.RemoveConnection),
cmdutil.NewCommandHandler(CommandName, QueryConnectionByIDCommandMethod, c.QueryConnectionByID),
cmdutil.NewCommandHandler(CommandName, QueryConnectionsCommandMethod, c.QueryConnections),
cmdutil.NewCommandHandler(CommandName, AcceptExchangeRequestCommandMethod, c.AcceptExchangeRequest),
cmdutil.NewCommandHandler(CommandName, CreateImplicitInvitationCommandMethod, c.CreateImplicitInvitation),
}
}
// CreateInvitation Creates a new connection invitation.
func (c *Command) CreateInvitation(rw io.Writer, req io.Reader) command.Error {
var request CreateInvitationArgs
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, CreateInvitationCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
var invitation *didexchange.Invitation
// call didexchange client
if request.Public != "" {
invitation, err = c.client.CreateInvitationWithDID(c.defaultLabel, request.Public)
} else {
invitation, err = c.client.CreateInvitation(c.defaultLabel,
didexchange.WithRouterConnectionID(request.RouterConnectionID))
}
if err != nil {
logutil.LogError(logger, CommandName, CreateInvitationCommandMethod, err.Error())
return command.NewExecuteError(CreateInvitationErrorCode, err)
}
command.WriteNillableResponse(rw, &CreateInvitationResponse{
Invitation: invitation,
Alias: request.Alias,
},
logger)
logutil.LogDebug(logger, CommandName, CreateInvitationCommandMethod, successString,
logutil.CreateKeyValueString(invitationIDString, invitation.ID))
return nil
}
// ReceiveInvitation receives a new connection invitation.
func (c *Command) ReceiveInvitation(rw io.Writer, req io.Reader) command.Error {
var request didexchange.Invitation
err := json.NewDecoder(req).Decode(&request.Invitation)
if err != nil {
logutil.LogInfo(logger, CommandName, ReceiveInvitationCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
connectionID, err := c.client.HandleInvitation(&request)
if err != nil {
logutil.LogError(logger, CommandName, ReceiveInvitationCommandMethod, err.Error(),
logutil.CreateKeyValueString(invitationIDString, request.ID),
logutil.CreateKeyValueString("label", request.Label),
logutil.CreateKeyValueString(connectionIDString, connectionID))
return command.NewExecuteError(ReceiveInvitationErrorCode, err)
}
command.WriteNillableResponse(rw, ReceiveInvitationResponse{
ConnectionID: connectionID,
}, logger)
logutil.LogDebug(logger, CommandName, ReceiveInvitationCommandMethod, successString,
logutil.CreateKeyValueString(invitationIDString, request.ID),
logutil.CreateKeyValueString("label", request.Label),
logutil.CreateKeyValueString(connectionIDString, connectionID))
return nil
}
// AcceptInvitation accepts a stored connection invitation.
func (c *Command) AcceptInvitation(rw io.Writer, req io.Reader) command.Error {
var request AcceptInvitationArgs
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, AcceptInvitationCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
if request.ID == "" {
logutil.LogDebug(logger, CommandName, AcceptInvitationCommandMethod, errEmptyConnID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyConnID))
}
err = c.client.AcceptInvitation(request.ID, request.Public, c.defaultLabel,
didexchange.WithRouterConnections(strings.Split(request.RouterConnections, ",")...))
if err != nil {
logutil.LogError(logger, CommandName, AcceptInvitationCommandMethod, err.Error(),
logutil.CreateKeyValueString(connectionIDString, request.ID))
return command.NewExecuteError(AcceptInvitationErrorCode, err)
}
command.WriteNillableResponse(rw, &AcceptInvitationResponse{
ConnectionID: request.ID,
}, logger)
logutil.LogDebug(logger, CommandName, AcceptInvitationCommandMethod, successString,
logutil.CreateKeyValueString(connectionIDString, request.ID))
return nil
}
// CreateImplicitInvitation creates implicit invitation using inviter DID.
func (c *Command) CreateImplicitInvitation(rw io.Writer, req io.Reader) command.Error {
var request ImplicitInvitationArgs
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, CreateImplicitInvitationCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
if request.InviterDID == "" {
logutil.LogDebug(logger, CommandName, CreateImplicitInvitationCommandMethod, errEmptyInviterDID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyInviterDID))
}
logger.Debugf("create implicit invitation: inviterDID[%s], inviterLabel[%s], inviteeDID[%s], inviteeLabel[%s]",
request.InviterDID, request.InviterLabel, request.InviteeDID, request.InviterLabel)
inviter := &didexchange.DIDInfo{DID: request.InviterDID, Label: request.InviterLabel}
var id string
if request.InviteeDID != "" {
invitee := &didexchange.DIDInfo{DID: request.InviteeDID, Label: request.InviteeLabel}
id, err = c.client.CreateImplicitInvitationWithDID(inviter, invitee)
} else {
id, err = c.client.CreateImplicitInvitation(inviter.Label, inviter.DID,
didexchange.WithRouterConnections(strings.Split(request.RouterConnections, ",")...))
}
if err != nil {
logutil.LogError(logger, CommandName, CreateImplicitInvitationCommandMethod, err.Error())
return command.NewExecuteError(CreateImplicitInvitationErrorCode, err)
}
command.WriteNillableResponse(rw, &ImplicitInvitationResponse{
ConnectionID: id,
}, logger)
logutil.LogDebug(logger, CommandName, CreateImplicitInvitationCommandMethod, successString)
return nil
}
// AcceptExchangeRequest accepts a stored connection request.
func (c *Command) AcceptExchangeRequest(rw io.Writer, req io.Reader) command.Error {
var request AcceptExchangeRequestArgs
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, AcceptExchangeRequestCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
if request.ID == "" {
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyConnID))
}
err = c.client.AcceptExchangeRequest(request.ID, request.Public,
c.defaultLabel, didexchange.WithRouterConnections(strings.Split(request.RouterConnections, ",")...))
if err != nil {
logutil.LogError(logger, CommandName, AcceptExchangeRequestCommandMethod, err.Error(),
logutil.CreateKeyValueString(connectionIDString, request.ID))
return command.NewExecuteError(AcceptExchangeRequestErrorCode, err)
}
command.WriteNillableResponse(rw, &ExchangeResponse{
ConnectionID: request.ID,
}, logger)
logutil.LogDebug(logger, CommandName, AcceptExchangeRequestCommandMethod, successString,
logutil.CreateKeyValueString(connectionIDString, request.ID))
return nil
}
// QueryConnections queries agent to agent connections.
func (c *Command) QueryConnections(rw io.Writer, req io.Reader) command.Error {
var request QueryConnectionsArgs
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, QueryConnectionsCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
results, err := c.client.QueryConnections(&request.QueryConnectionsParams)
if err != nil {
logutil.LogError(logger, CommandName, QueryConnectionsCommandMethod, err.Error())
return command.NewExecuteError(QueryConnectionsErrorCode, err)
}
command.WriteNillableResponse(rw, &QueryConnectionsResponse{
Results: results,
}, logger)
logutil.LogDebug(logger, CommandName, QueryConnectionsCommandMethod, successString)
return nil
}
// QueryConnectionByID fetches a single connection record by connection ID.
func (c *Command) QueryConnectionByID(rw io.Writer, req io.Reader) command.Error {
var request ConnectionIDArg
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, QueryConnectionByIDCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
if request.ID == "" {
logutil.LogDebug(logger, CommandName, QueryConnectionByIDCommandMethod, errEmptyConnID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyConnID))
}
result, err := c.client.GetConnection(request.ID)
if err != nil {
logutil.LogError(logger, CommandName, QueryConnectionByIDCommandMethod, err.Error(),
logutil.CreateKeyValueString(connectionIDString, request.ID))
return command.NewExecuteError(QueryConnectionsErrorCode, err)
}
command.WriteNillableResponse(rw, &QueryConnectionResponse{
Result: result,
}, logger)
logutil.LogDebug(logger, CommandName, QueryConnectionByIDCommandMethod, successString,
logutil.CreateKeyValueString(connectionIDString, request.ID))
return nil
}
// CreateConnection creates a new connection record in completed state and returns the generated connectionID.
func (c *Command) CreateConnection(rw io.Writer, req io.Reader) command.Error {
request := &CreateConnectionRequest{}
err := json.NewDecoder(req).Decode(request)
if err != nil {
logutil.LogInfo(logger, CommandName, CreateConnectionCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
theirDID, err := did.ParseDocument(request.TheirDID.Contents)
if err != nil {
logutil.LogInfo(logger, CommandName, CreateConnectionCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
id, err := c.client.CreateConnection(request.MyDID, theirDID,
didexchange.WithImplicit(request.Implicit),
didexchange.WithTheirLabel(request.TheirLabel),
didexchange.WithInvitationDID(request.InvitationDID),
didexchange.WithInvitationID(request.InvitationID),
didexchange.WithParentThreadID(request.ParentThreadID),
didexchange.WithThreadID(request.ThreadID))
if err != nil {
logutil.LogError(logger, CommandName, CreateConnectionCommandMethod, err.Error())
return command.NewExecuteError(CreateConnectionErrorCode, err)
}
command.WriteNillableResponse(rw, &ConnectionIDArg{
ID: id,
}, logger)
logutil.LogDebug(logger, CommandName, CreateConnectionCommandMethod, successString,
logutil.CreateKeyValueString(connectionIDString, id))
return nil
}
// RemoveConnection removes given connection record.
func (c *Command) RemoveConnection(rw io.Writer, req io.Reader) command.Error {
var request ConnectionIDArg
err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, RemoveConnectionCommandMethod, err.Error())
return command.NewValidationError(InvalidRequestErrorCode, err)
}
if request.ID == "" {
logutil.LogDebug(logger, CommandName, RemoveConnectionCommandMethod, errEmptyConnID)
return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyConnID))
}
logger.Debugf("Removing connection record for id [%s]", request.ID)
err = c.client.RemoveConnection(request.ID)
if err != nil {
logutil.LogError(logger, CommandName, RemoveConnectionCommandMethod, err.Error(),
logutil.CreateKeyValueString(connectionIDString, request.ID))
return command.NewExecuteError(RemoveConnectionErrorCode, err)
}
logutil.LogDebug(logger, CommandName, RemoveConnectionCommandMethod, successString,
logutil.CreateKeyValueString(connectionIDString, request.ID))
return nil
}