3
3
4
4
logger = logging .getLogger ("aries_controller.models.presentation" )
5
5
6
+
6
7
class Presentation ():
7
8
def __init__ (self , presentation_json ):
8
- # TODO Load verify var with json.loads() and test code
9
- self .json_data = presentation_json
10
- if self .is_verified () == "false" :
11
- logger .error ('Presentation Object Verification Failed' )
12
- # TODO raise proper PresentationVerificationFailed exception
13
- raise Exception ('Presentation Object Verification Failed' )
9
+ try :
10
+ self .json_data = json .loads (presentation_json )
11
+ except Exception :
12
+ logger .error ("Failed to load presentation JSON" )
13
+ raise
14
+ self .validate_presentation_json (presentation_json )
15
+ if not self .is_verified ():
16
+ logger .error ('Presentation Object Verification not verified' )
17
+ raise Exception ('Presentation Object Verification not verified' )
14
18
15
19
def get_self_attested (self ):
16
- if self .is_verified () == "false" :
17
- logger .debug ('Verification Failed' )
18
- data = {}
19
- for (name , val ) in self .json_data ['presentation' ]['requested_proof' ]['self_attested_attrs' ].items ():
20
- data [name ] = val ['raw' ]
21
- return json .dumps (data )
20
+ if self .is_verified ():
21
+ data = {}
22
+ for (name , val ) in self .json_data ['presentation' ]['requested_proof' ]['self_attested_attrs' ].items ():
23
+ data [name ] = val ['raw' ]
24
+ return json .dumps (data )
22
25
23
26
def get_revealed (self ):
24
- if self .is_verified () == "false" :
25
- logger .debug ('Verification Failed' )
26
- data = {}
27
- for (name , val ) in self .json_data ['presentation' ]['requested_proof' ]['revealed_attrs' ].items ():
28
- data [name ] = val ['raw' ]
29
-
30
- return json .dumps (data )
27
+ if self .is_verified ():
28
+ data = {}
29
+ for (name , val ) in self .json_data ['presentation' ]['requested_proof' ]['revealed_attrs' ].items ():
30
+ data [name ] = val ['raw' ]
31
+ return json .dumps (data )
31
32
32
33
def get_unrevealed_attrs (self ):
33
- if self .is_verified () == "false" :
34
- logger .debug ('Verification Failed' )
35
- data = {}
36
- for (name , val ) in self .json_data ['presentation' ]['requested_proof' ]['unrevealed_attrs' ].items ():
37
- data [name ] = val ['raw' ]
38
- return json .dumps (data )
34
+ if self .is_verified ():
35
+ data = {}
36
+ for (name , val ) in self .json_data ['presentation' ]['requested_proof' ]['unrevealed_attrs' ].items ():
37
+ data [name ] = val ['raw' ]
38
+ return json .dumps (data )
39
39
40
40
def get_predicates (self ):
41
- if self .is_verified () == "false" :
42
- logger .debug ('Verification Failed' )
43
- data = {}
44
- for (name , val ) in self .json_data ['presentation' ]['requested_proof' ]['predicates' ].items ():
45
- data [name ] = val ['raw' ]
46
- return json .dumps (data )
41
+ if self .is_verified ():
42
+ data = {}
43
+ for (name , val ) in self .json_data ['presentation' ]['requested_proof' ]['predicates' ].items ():
44
+ data [name ] = val ['raw' ]
45
+ return json .dumps (data )
47
46
48
47
def get_identifiers (self ):
49
- data = {}
50
- identifiers = []
51
- for index in range (len (self .json_data ['presentation' ]['identifiers' ])):
52
- identifiers .extend ([self .json_data ['presentation' ]['identifiers' ][index ]])
53
- data ['identifiers' ] = identifiers
54
- return json .dumps (data )
48
+ if self .__has_identifiers ():
49
+ data = {}
50
+ identifiers = []
51
+ for index in range (len (self .json_data ['presentation' ]['identifiers' ])):
52
+ identifiers .extend ([self .json_data ['presentation' ]['identifiers' ][index ]])
53
+ data ['identifiers' ] = identifiers
54
+ return json .dumps (data )
55
55
56
56
def get_schemas (self ):
57
- data = {}
58
- creds = []
59
- for index in range (len (self .json_data ['presentation' ]['identifiers' ])):
60
- for key in self .json_data ['presentation' ]['identifiers' ][index ]:
61
- if key == 'schema_id' :
62
- creds .extend ([self .json_data ['presentation' ]['identifiers' ][index ][key ]])
63
- data ['schema_id' ] = creds
64
- return json .dumps (data )
57
+ if self .__has_identifiers ():
58
+ data = {}
59
+ creds = []
60
+ for index in range (len (self .json_data ['presentation' ]['identifiers' ])):
61
+ for key in self .json_data ['presentation' ]['identifiers' ][index ]:
62
+ if key == 'schema_id' :
63
+ creds .extend ([self .json_data ['presentation' ]['identifiers' ][index ][key ]])
64
+ data ['schema_id' ] = creds
65
+ return json .dumps (data )
65
66
66
67
def get_cred_def_ids (self ):
67
- data = {}
68
- creds = []
69
- for index in range (len (self .json_data ['presentation' ]['identifiers' ])):
70
- for key in self .json_data ['presentation' ]['identifiers' ][index ]:
71
- if key == 'cred_def_id' :
72
- creds .extend ([self .json_data ['presentation' ]['identifiers' ][index ][key ]])
73
- data ['cred_def_id' ] = creds
74
- return json .dumps (data )
68
+ if self .__has_identifiers ():
69
+ data = {}
70
+ creds = []
71
+ for index in range (len (self .json_data ['presentation' ]['identifiers' ])):
72
+ for key in self .json_data ['presentation' ]['identifiers' ][index ]:
73
+ if key == 'cred_def_id' :
74
+ creds .extend ([self .json_data ['presentation' ]['identifiers' ][index ][key ]])
75
+ data ['cred_def_id' ] = creds
76
+ return json .dumps (data )
75
77
76
78
def get_rev_reg_ids (self ):
77
- data = {}
78
- creds = []
79
- for index in range (len (self .json_data ['presentation' ]['identifiers' ])):
80
- for key in self .json_data ['presentation' ]['identifiers' ][index ]:
81
- if key == 'rev_reg_id' :
82
- creds .extend ([self .json_data ['presentation' ]['identifiers' ][index ][key ]])
83
- data ['rev_reg_id' ] = creds
84
- return json .dumps (data )
79
+ if self .__has_identifiers ():
80
+ data = {}
81
+ creds = []
82
+ for index in range (len (self .json_data ['presentation' ]['identifiers' ])):
83
+ for key in self .json_data ['presentation' ]['identifiers' ][index ]:
84
+ if key == 'rev_reg_id' :
85
+ creds .extend ([self .json_data ['presentation' ]['identifiers' ][index ][key ]])
86
+ data ['rev_reg_id' ] = creds
87
+ return json .dumps (data )
85
88
86
89
def get_role (self ):
87
- return (self .json_data ['role' ])
90
+ return (self .json_data ['role' ])
88
91
89
92
def get_threadid (self ):
90
- return (self .json_data ['thread_id' ])
93
+ return (self .json_data ['thread_id' ])
91
94
92
95
def get_presentation_request (self ):
93
- return (self .json_data ['presentation_request' ])
96
+ return (self .json_data ['presentation_request' ])
94
97
95
98
def get_verified_state (self ):
96
- return self .json_data ['state' ]
99
+ return self .json_data ['state' ]
97
100
98
101
def get_presxid (self ):
99
- return self .json_data ['presentation_exchange_id' ]
100
-
101
- def is_verified (self ):
102
- return self .json_data ['verified' ]
102
+ return self .json_data ['presentation_exchange_id' ]
103
103
104
104
def from_conn_id (self ):
105
- return self .json_data ['connection_id' ]
105
+ return self .json_data ['connection_id' ]
106
+
107
+ def is_verified (self ):
108
+ try :
109
+ assert self .json_data ['verified' ] == "true"
110
+ except AssertionError :
111
+ logger .debug ('Verification Failed' )
112
+ return False
113
+ return True
114
+
115
+ def validate_presentation_json (self , presentation_json ):
116
+ try :
117
+ # Taken from sample response in swagger UI
118
+ # TODO Determine whether this is the minimal set of keys
119
+ presentation_keys = [
120
+ "auto_present" ,
121
+ "connection_id" ,
122
+ "created_at" ,
123
+ "error_msg" ,
124
+ "initiator" ,
125
+ "presentation" ,
126
+ "presentation_exchange_id" ,
127
+ "presentation_proposal_dict" ,
128
+ "presentation_request" ,
129
+ "presentation_request_dict" ,
130
+ "role" ,
131
+ "state" ,
132
+ "thread_id" ,
133
+ "trace" ,
134
+ "updated_at" ,
135
+ "verified"
136
+ ]
137
+ for key in presentation_keys :
138
+ assert key in json .loads (presentation_json ),\
139
+ f"Invalid presentation. Missing key { key } "
140
+ except AssertionError :
141
+ raise
142
+
143
+ def __has_identifiers (self ):
144
+ try :
145
+ assert 'identifiers' in self .json_data ['presentation' ],\
146
+ "No key 'identifiers' in presentation"
147
+ return True
148
+ except AssertionError :
149
+ logger .warning ("No key 'identifiers' in presentation" )
150
+ raise
0 commit comments