|
1 | 1 | #!/usr/bin/env python |
2 | | -# |
| 2 | +# flake8: noqa: E501 |
3 | 3 | # A library that provides a Python interface to the Telegram Bot API |
4 | 4 | # Copyright (C) 2015-2018 |
5 | 5 | # Leandro Toledo de Souza <devs@python-telegram-bot.org> |
|
24 | 24 |
|
25 | 25 |
|
26 | 26 | class EncryptedPassportElement(TelegramObject): |
27 | | - """Contains information about documents or other Telegram Passport elements shared with the bot |
28 | | - by the user. The data has been automatically decrypted by python-telegram-bot. |
| 27 | + """ |
| 28 | + Contains information about documents or other Telegram Passport elements shared with the bot |
| 29 | + by the user. The data has been automatically decrypted by python-telegram-bot. |
29 | 30 |
|
30 | 31 | Attributes: |
31 | 32 | type (:obj:`str`): Element type. One of "personal_details", "passport", "driver_license", |
32 | 33 | "identity_card", "internal_passport", "address", "utility_bill", "bank_statement", |
33 | 34 | "rental_agreement", "passport_registration", "temporary_registration", "phone_number", |
34 | 35 | "email". |
35 | | - data (:obj:`str`): Optional. Base64-encoded encrypted Telegram Passport element data |
36 | | - provided by the user, available for "personal_details", "passport", "driver_license", |
37 | | - "identity_card", "identity_passport" and "address" types. Can be decrypted and verified |
38 | | - using the accompanying EncryptedCredentials. |
| 36 | + data (:class:`telegram.PersonalDetails` or :class:`telegram.IdDocument` or :class:`telegram.ResidentialAddress` or :obj:`str`): |
| 37 | + Optional. Decrypted or encrypted data, available for "personal_details", "passport", |
| 38 | + "driver_license", "identity_card", "identity_passport" and "address" types. |
39 | 39 | phone_number (:obj:`str`): Optional. User's verified phone number, available only for |
40 | | - "phone_number" type |
| 40 | + "phone_number" type. |
41 | 41 | email (:obj:`str`): Optional. User's verified email address, available only for "email" |
42 | | - type |
43 | | - files (List[:class:`telegram.PassportFile`]): Optional. Array of encrypted files with |
44 | | - documents provided by the user, available for "utility_bill", "bank_statement", |
| 42 | + type. |
| 43 | + files (List[:class:`telegram.PassportFile`]): Optional. Array of encrypted/decrypted files |
| 44 | + with documents provided by the user, available for "utility_bill", "bank_statement", |
45 | 45 | "rental_agreement", "passport_registration" and "temporary_registration" types. |
46 | | - Files can be decrypted and verified using the accompanying EncryptedCredentials. |
47 | | - front_side (:class:`PassportFile`): Optional. Encrypted file with the front side of the |
48 | | - document, provided by the user. Available for "passport", "driver_license", |
49 | | - "identity_card" and "internal_passport". The file can be decrypted and verified using |
50 | | - the accompanying EncryptedCredentials. |
51 | | - reverse_side (:class:`PassportFile`): Optional. Encrypted file with the reverse side of the |
52 | | - document, provided by the user. Available for "driver_license" and "identity_card". |
53 | | - The file can be decrypted and verified using the accompanying EncryptedCredentials. |
54 | | - selfie (:class:`PassportFile`): Optional. Encrypted file with the selfie of the user |
55 | | - holding a document, provided by the user; available for "passport", "driver_license", |
56 | | - "identity_card" and "internal_passport". The file can be decrypted and verified using |
57 | | - the accompanying EncryptedCredentials. |
| 46 | + front_side (:class:`PassportFile`): Optional. Encrypted/decrypted file with the front side |
| 47 | + of the document, provided by the user. Available for "passport", "driver_license", |
| 48 | + "identity_card" and "internal_passport". |
| 49 | + reverse_side (:class:`PassportFile`): Optional. Encrypted/decrypted file with the reverse |
| 50 | + side of the document, provided by the user. Available for "driver_license" and |
| 51 | + "identity_card". |
| 52 | + selfie (:class:`PassportFile`): Optional. Encrypted/decrypted file with the selfie of the |
| 53 | + user holding a document, provided by the user; available for "passport", |
| 54 | + "driver_license", "identity_card" and "internal_passport". |
58 | 55 | bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods. |
59 | 56 |
|
60 | 57 | Args: |
61 | 58 | type (:obj:`str`): Element type. One of "personal_details", "passport", "driver_license", |
62 | 59 | "identity_card", "internal_passport", "address", "utility_bill", "bank_statement", |
63 | 60 | "rental_agreement", "passport_registration", "temporary_registration", "phone_number", |
64 | 61 | "email". |
65 | | - data (:obj:`str`, optional): Base64-encoded encrypted Telegram Passport element data |
66 | | - provided by the user, available for "personal_details", "passport", "driver_license", |
67 | | - "identity_card", "identity_passport" and "address" types. Can be decrypted and verified |
68 | | - using the accompanying EncryptedCredentials. |
| 62 | + data (:class:`telegram.PersonalDetails` or :class:`telegram.IdDocument` or :class:`telegram.ResidentialAddress` or :obj:`str`, optional): |
| 63 | + Decrypted or encrypted data, available for "personal_details", "passport", |
| 64 | + "driver_license", "identity_card", "identity_passport" and "address" types. |
69 | 65 | phone_number (:obj:`str`, optional): User's verified phone number, available only for |
70 | | - "phone_number" type |
| 66 | + "phone_number" type. |
71 | 67 | email (:obj:`str`, optional): User's verified email address, available only for "email" |
72 | | - type |
73 | | - files (List[:class:`telegram.PassportFile`], optional): Array of encrypted files with |
74 | | - documents provided by the user, available for "utility_bill", "bank_statement", |
| 68 | + type. |
| 69 | + files (List[:class:`telegram.PassportFile`], optional): Array of encrypted/decrypted files |
| 70 | + with documents provided by the user, available for "utility_bill", "bank_statement", |
75 | 71 | "rental_agreement", "passport_registration" and "temporary_registration" types. |
76 | | - Files can be decrypted and verified using the accompanying EncryptedCredentials. |
77 | | - front_side (:class:`PassportFile`, optional): Encrypted file with the front side of the |
78 | | - document, provided by the user. Available for "passport", "driver_license", |
79 | | - "identity_card" and "internal_passport". The file can be decrypted and verified using |
80 | | - the accompanying EncryptedCredentials. |
81 | | - reverse_side (:class:`PassportFile`, optional): Encrypted file with the reverse side of the |
82 | | - document, provided by the user. Available for "driver_license" and "identity_card". |
83 | | - The file can be decrypted and verified using the accompanying EncryptedCredentials. |
84 | | - selfie (:class:`PassportFile`, optional): Encrypted file with the selfie of the user |
85 | | - holding a document, provided by the user; available for "passport", "driver_license", |
86 | | - "identity_card" and "internal_passport". The file can be decrypted and verified using |
87 | | - the accompanying EncryptedCredentials. |
| 72 | + front_side (:class:`PassportFile`, optional): Encrypted/decrypted file with the front side |
| 73 | + of the document, provided by the user. Available for "passport", "driver_license", |
| 74 | + "identity_card" and "internal_passport". |
| 75 | + reverse_side (:class:`PassportFile`, optional): Encrypted/decrypted file with the reverse |
| 76 | + side of the document, provided by the user. Available for "driver_license" and |
| 77 | + "identity_card". |
| 78 | + selfie (:class:`PassportFile`, optional): Encrypted/decrypted file with the selfie of the |
| 79 | + user holding a document, provided by the user; available for "passport", |
| 80 | + "driver_license", "identity_card" and "internal_passport". |
88 | 81 | bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods. |
89 | 82 | **kwargs (:obj:`dict`): Arbitrary keyword arguments. |
90 | 83 |
|
| 84 | + Note: |
| 85 | + This object is decrypted only when originating from |
| 86 | + :obj:`telegram.PassportData.decrypted_data`. |
91 | 87 | """ |
92 | 88 |
|
93 | 89 | def __init__(self, |
@@ -117,53 +113,55 @@ def __init__(self, |
117 | 113 | self.front_side, self.reverse_side, self.selfie) |
118 | 114 |
|
119 | 115 | self.bot = bot |
120 | | - self._credentials = credentials |
121 | 116 |
|
122 | 117 | # noinspection PyMethodOverriding |
123 | 118 | @classmethod |
124 | | - def de_json(cls, data, bot, credentials): |
| 119 | + def de_json(cls, data, bot, decrypt=False, credentials=None): |
125 | 120 | if not data: |
126 | 121 | return None |
127 | 122 |
|
128 | 123 | data = super(EncryptedPassportElement, cls).de_json(data, bot) |
129 | 124 |
|
130 | 125 | secure_data = None |
131 | | - if data['type'] not in ('phone_number', 'email'): |
132 | | - secure_data = getattr(credentials.data.secure_data, data['type']) |
133 | | - |
134 | | - if secure_data.data is not None: |
135 | | - # If not already decrypted |
136 | | - if not isinstance(data['data'], dict): |
137 | | - data['data'] = decrypt_json(secure_data.data.secret, |
138 | | - secure_data.data.hash, |
139 | | - data['data']) |
140 | | - if data['type'] == 'personal_details': |
141 | | - data['data'] = PersonalDetails.de_json(data['data'], bot=bot) |
142 | | - elif data['type'] in ('passport', 'internal_passport', |
143 | | - 'driver_license', 'identity_card'): |
144 | | - data['data'] = IdDocumentData.de_json(data['data'], bot=bot) |
145 | | - elif data['type'] == 'address': |
146 | | - data['data'] = ResidentialAddress.de_json(data['data'], bot=bot) |
147 | | - |
148 | | - if secure_data: |
149 | | - data['files'] = PassportFile.de_list(data.get('files'), bot, secure_data) |
150 | | - data['front_side'] = PassportFile.de_json(data.get('front_side'), |
151 | | - bot, secure_data.front_side) |
152 | | - data['reverse_side'] = PassportFile.de_json(data.get('reverse_side'), |
153 | | - bot, secure_data.reverse_side) |
154 | | - data['selfie'] = PassportFile.de_json(data.get('selfie'), |
155 | | - bot, secure_data.selfie) |
156 | | - |
157 | | - return cls(bot=bot, credentials=secure_data, **data) |
| 126 | + if decrypt: |
| 127 | + if data['type'] not in ('phone_number', 'email'): |
| 128 | + secure_data = getattr(credentials.secure_data, data['type']) |
| 129 | + |
| 130 | + if secure_data.data is not None: |
| 131 | + # If not already decrypted |
| 132 | + if not isinstance(data['data'], dict): |
| 133 | + data['data'] = decrypt_json(secure_data.data.secret, |
| 134 | + secure_data.data.hash, |
| 135 | + data['data']) |
| 136 | + if data['type'] == 'personal_details': |
| 137 | + data['data'] = PersonalDetails.de_json(data['data'], bot=bot) |
| 138 | + elif data['type'] in ('passport', 'internal_passport', |
| 139 | + 'driver_license', 'identity_card'): |
| 140 | + data['data'] = IdDocumentData.de_json(data['data'], bot=bot) |
| 141 | + elif data['type'] == 'address': |
| 142 | + data['data'] = ResidentialAddress.de_json(data['data'], bot=bot) |
| 143 | + |
| 144 | + data['files'] = PassportFile.de_list(data.get('files'), bot, |
| 145 | + secure_data if secure_data else None) |
| 146 | + data['front_side'] = PassportFile.de_json(data.get('front_side'), bot, |
| 147 | + secure_data.front_side |
| 148 | + if secure_data else None) |
| 149 | + data['reverse_side'] = PassportFile.de_json(data.get('reverse_side'), bot, |
| 150 | + secure_data.reverse_side |
| 151 | + if secure_data else None) |
| 152 | + data['selfie'] = PassportFile.de_json(data.get('selfie'), bot, |
| 153 | + secure_data.selfie if secure_data else None) |
| 154 | + |
| 155 | + return cls(bot=bot, **data) |
158 | 156 |
|
159 | 157 | @classmethod |
160 | | - def de_list(cls, data, bot, credentials): |
| 158 | + def de_list(cls, data, bot): |
161 | 159 | if not data: |
162 | 160 | return [] |
163 | 161 |
|
164 | 162 | encrypted_passport_elements = list() |
165 | 163 | for element in data: |
166 | | - encrypted_passport_elements.append(cls.de_json(element, bot, credentials)) |
| 164 | + encrypted_passport_elements.append(cls.de_json(element, bot)) |
167 | 165 |
|
168 | 166 | return encrypted_passport_elements |
169 | 167 |
|
|
0 commit comments