-
Notifications
You must be signed in to change notification settings - Fork 515
/
Copy pathconn_record.py
766 lines (677 loc) · 25.4 KB
/
conn_record.py
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
"""Handle connection information interface with non-secrets storage."""
import json
from enum import Enum
from typing import Any, Optional, Union
from marshmallow import fields, validate
from ...core.profile import ProfileSession
from ...messaging.models.base_record import BaseRecord, BaseRecordSchema
from ...messaging.valid import (
INDY_DID_EXAMPLE,
INDY_DID_VALIDATE,
INDY_RAW_PUBLIC_KEY_EXAMPLE,
INDY_RAW_PUBLIC_KEY_VALIDATE,
UUID4_EXAMPLE,
)
from ...protocols.connections.v1_0.message_types import ARIES_PROTOCOL as CONN_PROTO
from ...protocols.connections.v1_0.message_types import (
CONNECTION_INVITATION,
CONNECTION_REQUEST,
)
from ...protocols.connections.v1_0.messages.connection_invitation import (
ConnectionInvitation,
)
from ...protocols.connections.v1_0.messages.connection_request import ConnectionRequest
from ...protocols.didcomm_prefix import DIDCommPrefix
from ...protocols.didexchange.v1_0.message_types import ARIES_PROTOCOL as DIDX_PROTO
from ...protocols.didexchange.v1_0.messages.request import DIDXRequest
from ...protocols.out_of_band.v1_0.messages.invitation import (
InvitationMessage as OOBInvitation,
)
from ...storage.base import BaseStorage
from ...storage.error import StorageNotFoundError
from ...storage.record import StorageRecord
class ConnRecord(BaseRecord):
"""Represents a single pairwise connection."""
class Meta:
"""ConnRecord metadata."""
schema_class = "ConnRecordSchema"
class Protocol(Enum):
"""Supported Protocols for Connection."""
RFC_0160 = CONN_PROTO
RFC_0023 = DIDX_PROTO
@classmethod
def get(cls, label: Union[str, "ConnRecord.Protocol"]):
"""Get aries protocol enum for label."""
if isinstance(label, str):
for proto in ConnRecord.Protocol:
if label in proto.value:
return proto
elif isinstance(label, ConnRecord.Protocol):
return label
return None
@property
def aries_protocol(self):
"""Return used connection protocol."""
return self.value
class Role(Enum):
"""RFC 160 (inviter, invitee) = RFC 23 (responder, requester)."""
REQUESTER = ("invitee", "requester") # == RFC 23 initiator, RFC 434 receiver
RESPONDER = ("inviter", "responder") # == RFC 160 initiator(!), RFC 434 sender
@property
def rfc160(self):
"""Return RFC 160 (connection protocol) nomenclature."""
return self.value[0]
@property
def rfc23(self):
"""Return RFC 23 (DID exchange protocol) nomenclature."""
return self.value[1]
@classmethod
def get(cls, label: Union[str, "ConnRecord.Role"]):
"""Get role enum for label."""
if isinstance(label, str):
for role in ConnRecord.Role:
if label in role.value:
return role
elif isinstance(label, ConnRecord.Role):
return label
return None
def flip(self):
"""Return opposite interlocutor role: theirs for ours, ours for theirs."""
return (
ConnRecord.Role.REQUESTER
if self is ConnRecord.Role.RESPONDER
else ConnRecord.Role.RESPONDER
)
def __eq__(self, other: Union[str, "ConnRecord.Role"]) -> bool:
"""Comparison between roles."""
return self is ConnRecord.Role.get(other)
class State(Enum):
"""Collator for equivalent states between RFC 160 and RFC 23.
On the connection record, the state has to serve for both RFCs.
Hence, internally, RFC23 requester/responder states collate to
their RFC160 condensed equivalent.
"""
INIT = ("init", "start")
INVITATION = ("invitation", "invitation")
REQUEST = ("request", "request")
RESPONSE = ("response", "response")
COMPLETED = ("active", "completed")
ABANDONED = ("error", "abandoned")
@property
def rfc160(self):
"""Return RFC 160 (connection protocol) nomenclature."""
return self.value[0]
@property
def rfc23(self):
"""Return RFC 23 (DID exchange protocol) nomenclature to record logic."""
return self.value[1]
def rfc23strict(self, their_role: "ConnRecord.Role"):
"""Return RFC 23 (DID exchange protocol) nomenclature to role as per RFC."""
if not their_role or self in (
ConnRecord.State.INIT,
ConnRecord.State.COMPLETED,
ConnRecord.State.ABANDONED,
):
return self.value[1]
if self is ConnRecord.State.REQUEST:
return self.value[1] + (
"-sent"
if ConnRecord.Role.get(their_role) is ConnRecord.Role.RESPONDER
else "-received"
)
else:
return self.value[1] + (
"-received"
if ConnRecord.Role.get(their_role) is ConnRecord.Role.RESPONDER
else "-sent"
)
@classmethod
def get(cls, label: Union[str, "ConnRecord.State"]):
"""Get state enum for label."""
if isinstance(label, str):
for state in ConnRecord.State:
if label in state.value:
return state
elif isinstance(label, ConnRecord.State):
return label
return None
def __eq__(self, other: Union[str, "ConnRecord.State"]) -> bool:
"""Comparison between states."""
return self is ConnRecord.State.get(other)
RECORD_ID_NAME = "connection_id"
RECORD_TOPIC = "connections"
LOG_STATE_FLAG = "debug.connections"
TAG_NAMES = {
"my_did",
"their_did",
"request_id",
"invitation_key",
"their_public_did",
"invitation_msg_id",
"state",
"their_role",
}
RECORD_TYPE = "connection"
RECORD_TYPE_INVITATION = "connection_invitation"
RECORD_TYPE_REQUEST = "connection_request"
RECORD_TYPE_METADATA = "connection_metadata"
INVITATION_MODE_ONCE = "once"
INVITATION_MODE_MULTI = "multi"
INVITATION_MODE_STATIC = "static"
ROUTING_STATE_NONE = "none"
ROUTING_STATE_REQUEST = "request"
ROUTING_STATE_ACTIVE = "active"
ROUTING_STATE_ERROR = "error"
ACCEPT_MANUAL = "manual"
ACCEPT_AUTO = "auto"
def __init__(
self,
*,
connection_id: str = None,
my_did: str = None,
their_did: str = None,
their_label: str = None,
their_role: Union[str, "ConnRecord.Role"] = None,
invitation_key: str = None,
invitation_msg_id: str = None,
request_id: str = None,
state: Union[str, "ConnRecord.State"] = None,
inbound_connection_id: str = None,
error_msg: str = None,
routing_state: str = None,
accept: str = None,
invitation_mode: str = None,
alias: str = None,
their_public_did: str = None,
rfc23_state: str = None, # from state: formalism for base_record.from_storage()
initiator: str = None, # for backward compatibility with old ConnectionRecord
connection_protocol: Union[str, "ConnRecord.Protocol"] = None,
**kwargs,
):
"""Initialize a new ConnRecord."""
super().__init__(
connection_id,
state=(ConnRecord.State.get(state) or ConnRecord.State.INIT).rfc160,
**kwargs,
)
self.my_did = my_did
self.their_did = their_did
self.their_label = their_label
self.their_role = (
ConnRecord.Role.get(their_role).rfc160
if isinstance(their_role, str)
else None
if their_role is None
else their_role.rfc160
)
self.invitation_key = invitation_key
self.invitation_msg_id = invitation_msg_id
self.request_id = request_id
self.error_msg = error_msg
self.inbound_connection_id = inbound_connection_id
self.routing_state = routing_state or self.ROUTING_STATE_NONE
self.accept = accept or self.ACCEPT_MANUAL
self.invitation_mode = invitation_mode or self.INVITATION_MODE_ONCE
self.alias = alias
self.their_public_did = their_public_did
self.connection_protocol = (
ConnRecord.Protocol.get(connection_protocol).aries_protocol
if isinstance(connection_protocol, str)
else (
None
if connection_protocol is None
else connection_protocol.aries_protocol
)
)
@property
def connection_id(self) -> str:
"""Accessor for the ID associated with this connection."""
return self._id
@property
def rfc23_state(self) -> str:
"""RFC23 state per RFC text, potentially particular to role."""
return ConnRecord.State.get(self.state).rfc23strict(self.their_role)
@property
def record_value(self) -> dict:
"""Accessor to for the JSON record value properties for this connection."""
return {
prop: getattr(self, prop)
for prop in (
"their_role",
"inbound_connection_id",
"routing_state",
"accept",
"invitation_mode",
"invitation_msg_id",
"alias",
"error_msg",
"their_label",
"their_public_did",
"state",
"connection_protocol",
)
}
@classmethod
async def retrieve_by_did(
cls,
session: ProfileSession,
their_did: Optional[str] = None,
my_did: Optional[str] = None,
their_role: Optional[str] = None,
) -> "ConnRecord":
"""Retrieve a connection record by target DID.
Args:
session: The active profile session
their_did: The target DID to filter by
my_did: One of our DIDs to filter by
my_role: Filter connections by their role
"""
tag_filter = {}
if their_did:
tag_filter["their_did"] = their_did
if my_did:
tag_filter["my_did"] = my_did
post_filter = {}
if their_role:
post_filter["their_role"] = cls.Role.get(their_role).rfc160
return await cls.retrieve_by_tag_filter(session, tag_filter, post_filter)
@classmethod
async def retrieve_by_invitation_key(
cls, session: ProfileSession, invitation_key: str, their_role: str = None
) -> "ConnRecord":
"""Retrieve a connection record by invitation key.
Args:
session: The active profile session
invitation_key: The key on the originating invitation
initiator: Filter by the initiator value
"""
tag_filter = {
"invitation_key": invitation_key,
"state": cls.State.INVITATION.rfc160,
}
post_filter = {"state": cls.State.INVITATION.rfc160}
if their_role:
post_filter["their_role"] = cls.Role.get(their_role).rfc160
tag_filter["their_role"] = cls.Role.get(their_role).rfc160
return await cls.retrieve_by_tag_filter(session, tag_filter, post_filter)
@classmethod
async def retrieve_by_invitation_msg_id(
cls, session: ProfileSession, invitation_msg_id: str, their_role: str = None
) -> Optional["ConnRecord"]:
"""Retrieve a connection record by invitation_msg_id.
Args:
session: The active profile session
invitation_msg_id: Invitation message identifier
initiator: Filter by the initiator value
"""
tag_filter = {"invitation_msg_id": invitation_msg_id}
post_filter = {
"state": cls.State.INVITATION.rfc160,
}
if their_role:
post_filter["their_role"] = cls.Role.get(their_role).rfc160
try:
return await cls.retrieve_by_tag_filter(session, tag_filter, post_filter)
except StorageNotFoundError:
return None
@classmethod
async def find_existing_connection(
cls, session: ProfileSession, their_public_did: str
) -> Optional["ConnRecord"]:
"""Retrieve existing active connection records (public did).
Args:
session: The active profile session
their_public_did: Inviter public DID
"""
tag_filter = {"their_public_did": their_public_did}
conn_records = await cls.query(
session,
tag_filter=tag_filter,
)
for conn_record in conn_records:
if conn_record.state == ConnRecord.State.COMPLETED:
return conn_record
return None
@classmethod
async def retrieve_by_request_id(
cls, session: ProfileSession, request_id: str, their_role: str = None
) -> "ConnRecord":
"""Retrieve a connection record from our previous request ID.
Args:
session: The active profile session
request_id: The ID of the originating connection request
"""
tag_filter = {"request_id": request_id}
if their_role:
tag_filter["their_role"] = their_role
return await cls.retrieve_by_tag_filter(session, tag_filter)
@classmethod
async def retrieve_by_alias(
cls, session: ProfileSession, alias: str
) -> "ConnRecord":
"""Retrieve a connection record from an alias.
Args:
session: The active profile session
alias: The alias of the connection
"""
post_filter = {"alias": alias}
return await cls.query(session, post_filter_positive=post_filter)
async def attach_invitation(
self,
session: ProfileSession,
invitation: Union[ConnectionInvitation, OOBInvitation],
):
"""Persist the related connection invitation to storage.
Args:
session: The active profile session
invitation: The invitation to relate to this connection record
"""
assert self.connection_id
record = StorageRecord(
self.RECORD_TYPE_INVITATION, # conn- or oob-invitation, to retrieve easily
invitation.to_json(),
{"connection_id": self.connection_id},
)
storage = session.inject(BaseStorage)
await storage.add_record(record)
async def retrieve_invitation(
self, session: ProfileSession
) -> Union[ConnectionInvitation, OOBInvitation]:
"""Retrieve the related connection invitation.
Args:
session: The active profile session
"""
assert self.connection_id
storage = session.inject(BaseStorage)
result = await storage.find_record(
self.RECORD_TYPE_INVITATION,
{"connection_id": self.connection_id},
)
ser = json.loads(result.value)
return (
ConnectionInvitation
if DIDCommPrefix.unqualify(ser["@type"]) == CONNECTION_INVITATION
else OOBInvitation
).deserialize(ser)
async def attach_request(
self,
session: ProfileSession,
request: Union[ConnectionRequest, DIDXRequest],
):
"""Persist the related connection request to storage.
Args:
session: The active profile session
request: The request to relate to this connection record
"""
assert self.connection_id
record = StorageRecord(
self.RECORD_TYPE_REQUEST, # conn- or didx-request, to retrieve easily
request.to_json(),
{"connection_id": self.connection_id},
)
storage: BaseStorage = session.inject(BaseStorage)
await storage.add_record(record)
async def retrieve_request(
self,
session: ProfileSession,
) -> Union[ConnectionRequest, DIDXRequest]:
"""Retrieve the related connection invitation.
Args:
session: The active profile session
"""
assert self.connection_id
storage: BaseStorage = session.inject(BaseStorage)
result = await storage.find_record(
self.RECORD_TYPE_REQUEST, {"connection_id": self.connection_id}
)
ser = json.loads(result.value)
return (
ConnectionRequest
if DIDCommPrefix.unqualify(ser["@type"]) == CONNECTION_REQUEST
else DIDXRequest
).deserialize(ser)
@property
def is_ready(self) -> str:
"""Accessor for connection readiness."""
return ConnRecord.State.get(self.state) in (
ConnRecord.State.COMPLETED,
ConnRecord.State.RESPONSE,
)
@property
def is_multiuse_invitation(self) -> bool:
"""Accessor for multi use invitation mode."""
return self.invitation_mode == self.INVITATION_MODE_MULTI
async def post_save(self, session: ProfileSession, *args, **kwargs):
"""Perform post-save actions.
Args:
session: The active profile session
"""
await super().post_save(session, *args, **kwargs)
# clear cache key set by connection manager
cache_key = f"connection_target::{self.connection_id}"
await self.clear_cached_key(session, cache_key)
async def delete_record(self, session: ProfileSession):
"""Perform connection record deletion actions.
Args:
session (ProfileSession): session
"""
await super().delete_record(session)
# Delete metadata
if self.connection_id:
storage = session.inject(BaseStorage)
await storage.delete_all_records(
self.RECORD_TYPE_METADATA,
{"connection_id": self.connection_id},
)
async def abandon(self, session: ProfileSession, *, reason: Optional[str] = None):
"""Set state to abandoned."""
reason = reason or "Connectin abandoned"
self.state = ConnRecord.State.ABANDONED.rfc23
self.error_msg = reason
await self.save(session, reason=reason)
async def metadata_get(
self, session: ProfileSession, key: str, default: Any = None
) -> Any:
"""Retrieve arbitrary metadata associated with this connection.
Args:
session (ProfileSession): session used for storage
key (str): key identifying metadata
default (Any): default value to get; type should be a JSON
compatible value.
Returns:
Any: metadata stored by key
"""
assert self.connection_id
storage: BaseStorage = session.inject(BaseStorage)
try:
record = await storage.find_record(
self.RECORD_TYPE_METADATA,
{"key": key, "connection_id": self.connection_id},
)
return json.loads(record.value)
except StorageNotFoundError:
return default
async def metadata_set(self, session: ProfileSession, key: str, value: Any):
"""Set arbitrary metadata associated with this connection.
Args:
session (ProfileSession): session used for storage
key (str): key identifying metadata
value (Any): value to set
"""
assert self.connection_id
value = json.dumps(value)
storage: BaseStorage = session.inject(BaseStorage)
try:
record = await storage.find_record(
self.RECORD_TYPE_METADATA,
{"key": key, "connection_id": self.connection_id},
)
await storage.update_record(record, value, record.tags)
except StorageNotFoundError:
record = StorageRecord(
self.RECORD_TYPE_METADATA,
value,
{"key": key, "connection_id": self.connection_id},
)
await storage.add_record(record)
async def metadata_delete(self, session: ProfileSession, key: str):
"""Delete custom metadata associated with this connection.
Args:
session (ProfileSession): session used for storage
key (str): key of metadata to delete
"""
assert self.connection_id
storage: BaseStorage = session.inject(BaseStorage)
try:
record = await storage.find_record(
self.RECORD_TYPE_METADATA,
{"key": key, "connection_id": self.connection_id},
)
await storage.delete_record(record)
except StorageNotFoundError as err:
raise KeyError(f"{key} not found in connection metadata") from err
async def metadata_get_all(self, session: ProfileSession) -> dict:
"""Return all custom metadata associated with this connection.
Args:
session (ProfileSession): session used for storage
Returns:
dict: dictionary representation of all metadata values
"""
assert self.connection_id
storage: BaseStorage = session.inject(BaseStorage)
records = await storage.find_all_records(
self.RECORD_TYPE_METADATA,
{"connection_id": self.connection_id},
)
return {record.tags["key"]: json.loads(record.value) for record in records}
def __eq__(self, other: Any) -> bool:
"""Comparison between records."""
return super().__eq__(other)
class ConnRecordSchema(BaseRecordSchema):
"""Schema to allow serialization/deserialization of connection records."""
class Meta:
"""ConnRecordSchema metadata."""
model_class = ConnRecord
connection_id = fields.Str(
required=False,
metadata={"description": "Connection identifier", "example": UUID4_EXAMPLE},
)
my_did = fields.Str(
required=False,
validate=INDY_DID_VALIDATE,
metadata={"description": "Our DID for connection", "example": INDY_DID_EXAMPLE},
)
their_did = fields.Str(
required=False,
validate=INDY_DID_VALIDATE,
metadata={
"description": "Their DID for connection",
"example": INDY_DID_EXAMPLE,
},
)
their_label = fields.Str(
required=False,
metadata={"description": "Their label for connection", "example": "Bob"},
)
their_role = fields.Str(
required=False,
validate=validate.OneOf(
[label for role in ConnRecord.Role for label in role.value]
),
metadata={
"description": "Their role in the connection protocol",
"example": ConnRecord.Role.REQUESTER.rfc23,
},
)
connection_protocol = fields.Str(
required=False,
validate=validate.OneOf([proto.value for proto in ConnRecord.Protocol]),
metadata={
"description": "Connection protocol used",
"example": ConnRecord.Protocol.RFC_0160.aries_protocol,
},
)
rfc23_state = fields.Str(
dump_only=True,
metadata={"description": "State per RFC 23", "example": "invitation-sent"},
)
inbound_connection_id = fields.Str(
required=False,
metadata={
"description": "Inbound routing connection id to use",
"example": UUID4_EXAMPLE,
},
)
invitation_key = fields.Str(
required=False,
validate=INDY_RAW_PUBLIC_KEY_VALIDATE,
metadata={
"description": "Public key for connection",
"example": INDY_RAW_PUBLIC_KEY_EXAMPLE,
},
)
invitation_msg_id = fields.Str(
required=False,
metadata={
"description": "ID of out-of-band invitation message",
"example": UUID4_EXAMPLE,
},
)
request_id = fields.Str(
required=False,
metadata={
"description": "Connection request identifier",
"example": UUID4_EXAMPLE,
},
)
routing_state = fields.Str(
required=False,
validate=validate.OneOf(
ConnRecord.get_attributes_by_prefix("ROUTING_STATE_", walk_mro=False)
),
metadata={
"description": "Routing state of connection",
"example": ConnRecord.ROUTING_STATE_ACTIVE,
},
)
accept = fields.Str(
required=False,
validate=validate.OneOf(
ConnRecord.get_attributes_by_prefix("ACCEPT_", walk_mro=False)
),
metadata={
"description": "Connection acceptance: manual or auto",
"example": ConnRecord.ACCEPT_AUTO,
},
)
error_msg = fields.Str(
required=False,
metadata={
"description": "Error message",
"example": "No DIDDoc provided; cannot connect to public DID",
},
)
invitation_mode = fields.Str(
required=False,
validate=validate.OneOf(
ConnRecord.get_attributes_by_prefix("INVITATION_MODE_", walk_mro=False)
),
metadata={
"description": "Invitation mode",
"example": ConnRecord.INVITATION_MODE_ONCE,
},
)
alias = fields.Str(
required=False,
metadata={
"description": "Optional alias to apply to connection for later use",
"example": "Bob, providing quotes",
},
)
their_public_did = fields.Str(
required=False,
metadata={
"description": "Other agent's public DID for connection",
"example": "2cpBmR3FqGKWi5EyUbpRY8",
},
)