From 03b237ec14a4b8876c3c5498558fee5d73211264 Mon Sep 17 00:00:00 2001 From: Lonami Date: Tue, 27 Mar 2018 17:32:23 +0200 Subject: [PATCH] things --- telethon/telegram_client.py | 43 +++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index f91994f8..2e858dac 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -2474,52 +2474,53 @@ class TelegramClient(TelegramBareClient): def edit_2fa(self, current_password=None, new_password=None, hint='', email=None): """ Changes the 2FA settings of the logged in user, according to the - passed parameters. Take note of the parameter explanations + passed parameters. Take note of the parameter explanations. + + This method has no effect if both current and new password are omitted. current_password (:obj:`str`, optional): - The current password, to authorize changing to ``new_password`` + The current password, to authorize changing to ``new_password``. Must be set if changing existing 2FA settings. - Must not be set if 2FA is currently disabled. - Passing this by itself will remove 2FA (if correct) + Must **not** be set if 2FA is currently disabled. + Passing this by itself will remove 2FA (if correct). new_password (:obj:`str`, optional): The password to set as 2FA. If 2FA was already enabled, ``current_password`` - must be set. Leaving this blank or ``None`` will remove the password + **must** be set. Leaving this blank or ``None`` will remove the password. hint (:obj:`str`, optional): Hint to be displayed by Telegram when it asks for 2FA. Leaving unspecified is highly discouraged. - Has no effect if ``new_password`` is not set + Has no effect if ``new_password`` is not set. email (:obj:`str`, optional): Recovery and verification email. - Throws ``EmailUnconfirmedError`` if value differs from current one - Has no effect if ``new_password`` is not set + Raises ``EmailUnconfirmedError`` if value differs from current one + Has no effect if ``new_password`` is not set. Returns: - ``True`` if successful, ``False`` otherwise + ``True`` if successful, ``False`` otherwise. + + Raises: + :obj:`PasswordEmptyError` if """ if new_password is None and current_password is None: - raise PasswordEmptyError('Both new_password and current_password '\ - 'were not specified') return False - + pass_result = self(GetPasswordRequest()) if isinstance(pass_result, NoPassword) and current_password: - __log__.warn('You supplied current_password even though this '\ - 'account does not have one set') current_password = None + salt_random = os.urandom(8) salt = pass_result.new_salt + salt_random - - if current_password: + if not current_password: + current_password_hash = salt + else: current_password = pass_result.current_salt +\ current_password.encode() + pass_result.current_salt current_password_hash = hashlib.sha256(current_password).digest() - else: - current_password_hash = salt - if new_password: # setting new password + if new_password: # Setting new password new_password = salt + new_password.encode('utf-8') + salt new_password_hash = hashlib.sha256(new_password).digest() new_settings = PasswordInputSettings( @@ -2527,12 +2528,12 @@ class TelegramClient(TelegramBareClient): new_password_hash=new_password_hash, hint=hint ) - if email: # if enabling 2FA or changing email + if email: # If enabling 2FA or changing email new_settings.email = email # TG counts empty string as None return self(UpdatePasswordSettingsRequest( current_password_hash, new_settings=new_settings )) - else: # removing password + else: # Removing existing password return self(UpdatePasswordSettingsRequest( current_password_hash, new_settings=PasswordInputSettings(