|
| 1 | +import json |
| 2 | +import logging |
| 3 | + |
| 4 | +logger = logging.getLogger("aries_controller.models.presentation") |
| 5 | + |
| 6 | + |
| 7 | +class Presentation(): |
| 8 | + def __init__(self, presentation_json): |
| 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') |
| 18 | + |
| 19 | + def get_self_attested_attrs(self): |
| 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) |
| 25 | + |
| 26 | + def get_revealed_attrs(self): |
| 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) |
| 32 | + |
| 33 | + def get_unrevealed_attrs(self): |
| 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 | + |
| 40 | + def get_predicates(self): |
| 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) |
| 46 | + |
| 47 | + def get_identifiers(self): |
| 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 | + |
| 56 | + def get_schemas(self): |
| 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) |
| 66 | + |
| 67 | + def get_cred_def_ids(self): |
| 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) |
| 77 | + |
| 78 | + def get_rev_reg_ids(self): |
| 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) |
| 88 | + |
| 89 | + def get_role(self): |
| 90 | + return(self.json_data['role']) |
| 91 | + |
| 92 | + def get_threadid(self): |
| 93 | + return(self.json_data['thread_id']) |
| 94 | + |
| 95 | + def get_presentation_request(self): |
| 96 | + return(self.json_data['presentation_request']) |
| 97 | + |
| 98 | + def get_verified_state(self): |
| 99 | + return self.json_data['state'] |
| 100 | + |
| 101 | + def get_presxid(self): |
| 102 | + return self.json_data['presentation_exchange_id'] |
| 103 | + |
| 104 | + def get_from_conn_id(self): |
| 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