def de_json_decrypted(cls, data, bot, credentials):
        if not data:
            return None

        data = super(EncryptedPassportElement, cls).de_json(data, bot)

        if data['type'] not in ('phone_number', 'email'):
            secure_data = getattr(credentials.secure_data, data['type'])

            if secure_data.data is not None:
                # If not already decrypted
                if not isinstance(data['data'], dict):
                    data['data'] = decrypt_json(b64decode(secure_data.data.secret),
                                                b64decode(secure_data.data.hash),
                                                b64decode(data['data']))
                if data['type'] == 'personal_details':
                    data['data'] = PersonalDetails.de_json(data['data'], bot=bot)
                elif data['type'] in ('passport', 'internal_passport',
                                      'driver_license', 'identity_card'):
                    data['data'] = IdDocumentData.de_json(data['data'], bot=bot)
                elif data['type'] == 'address':
                    data['data'] = ResidentialAddress.de_json(data['data'], bot=bot)

            data['files'] = PassportFile.de_list_decrypted(data.get('files'), bot,
                                                           secure_data.files) or None
            data['front_side'] = PassportFile.de_json_decrypted(data.get('front_side'), bot,
                                                                secure_data.front_side)
            data['reverse_side'] = PassportFile.de_json_decrypted(data.get('reverse_side'), bot,
                                                                  secure_data.reverse_side)
            data['selfie'] = PassportFile.de_json_decrypted(data.get('selfie'), bot,
                                                            secure_data.selfie)
            data['translation'] = PassportFile.de_list_decrypted(data.get('translation'), bot,
                                                                 secure_data.translation) or None

        return cls(bot=bot, **data)
Пример #2
0
    def de_json_decrypted(
        cls, data: Optional[JSONDict], bot: 'Bot', credentials: 'Credentials'
    ) -> Optional['EncryptedPassportElement']:
        """Variant of :meth:`telegram.TelegramObject.de_json` that also takes into account
        passport credentials.

        Args:
            data (Dict[:obj:`str`, ...]): The JSON data.
            bot (:class:`telegram.Bot`): The bot associated with this object.
            credentials (:class:`telegram.FileCredentials`): The credentials

        Returns:
            :class:`telegram.EncryptedPassportElement`:

        """
        if not data:
            return None

        if data['type'] not in ('phone_number', 'email'):
            secure_data = getattr(credentials.secure_data, data['type'])

            if secure_data.data is not None:
                # If not already decrypted
                if not isinstance(data['data'], dict):
                    data['data'] = decrypt_json(
                        b64decode(secure_data.data.secret),
                        b64decode(secure_data.data.hash),
                        b64decode(data['data']),
                    )
                if data['type'] == 'personal_details':
                    data['data'] = PersonalDetails.de_json(data['data'], bot=bot)
                elif data['type'] in (
                    'passport',
                    'internal_passport',
                    'driver_license',
                    'identity_card',
                ):
                    data['data'] = IdDocumentData.de_json(data['data'], bot=bot)
                elif data['type'] == 'address':
                    data['data'] = ResidentialAddress.de_json(data['data'], bot=bot)

            data['files'] = (
                PassportFile.de_list_decrypted(data.get('files'), bot, secure_data.files) or None
            )
            data['front_side'] = PassportFile.de_json_decrypted(
                data.get('front_side'), bot, secure_data.front_side
            )
            data['reverse_side'] = PassportFile.de_json_decrypted(
                data.get('reverse_side'), bot, secure_data.reverse_side
            )
            data['selfie'] = PassportFile.de_json_decrypted(
                data.get('selfie'), bot, secure_data.selfie
            )
            data['translation'] = (
                PassportFile.de_list_decrypted(
                    data.get('translation'), bot, secure_data.translation
                )
                or None
            )

        return cls(bot=bot, **data)