diff --git a/telethon/client/auth.py b/telethon/client/auth.py index 2e5c60bc..a8743e20 100644 --- a/telethon/client/auth.py +++ b/telethon/client/auth.py @@ -452,13 +452,17 @@ class AuthMethods(MessageParseMethods, UserMethods): Has no effect if ``new_password`` is not set. email (`str`, optional): - Recovery and verification email. Raises ``EmailUnconfirmedError`` - if value differs from current one, and has no effect if - ``new_password`` is not set. + Recovery and verification email. If present, you must also + set `email_code_callback`, else it raises ``ValueError``. email_code_callback (`callable`, optional): If an email is provided, a callback that returns the code sent to it must also be set. This callback may be asynchronous. + It should return a string with the code. The length of the + code will be passed to the callback as an input parameter. + + If the callback returns an invalid code, it will raise + ``CodeInvalidError``. Returns: ``True`` if successful, ``False`` otherwise. @@ -466,6 +470,9 @@ class AuthMethods(MessageParseMethods, UserMethods): if new_password is None and current_password is None: return False + if email and not callable(email_code_callback): + raise ValueError('email present without email_code_callback') + pwd = await self(functions.account.GetPasswordRequest()) pwd.new_algo.salt1 += os.urandom(32) assert isinstance(pwd, types.account.Password) @@ -483,16 +490,26 @@ class AuthMethods(MessageParseMethods, UserMethods): else: new_password_hash = b'' - await self(functions.account.UpdatePasswordSettingsRequest( - password=password, - new_settings=types.account.PasswordInputSettings( - new_algo=pwd.new_algo, - new_password_hash=new_password_hash, - hint=hint, - email=email, - new_secure_settings=None - ) - )) + try: + await self(functions.account.UpdatePasswordSettingsRequest( + password=password, + new_settings=types.account.PasswordInputSettings( + new_algo=pwd.new_algo, + new_password_hash=new_password_hash, + hint=hint, + email=email, + new_secure_settings=None + ) + )) + except errors.EmailUnconfirmedError as e: + code = email_code_callback(e.code_length) + if inspect.isawaitable(code): + code = await code + + code = str(code) + await self(functions.account.ConfirmPasswordEmailRequest(code)) + + return True # endregion diff --git a/telethon_generator/data/errors.csv b/telethon_generator/data/errors.csv index aa4909e5..a2b00fad 100644 --- a/telethon_generator/data/errors.csv +++ b/telethon_generator/data/errors.csv @@ -52,6 +52,7 @@ CHAT_TITLE_EMPTY,400,No chat title provided CHAT_WRITE_FORBIDDEN,403,You can't write in this chat CODE_EMPTY,400,The provided code is empty CODE_HASH_INVALID,400,Code hash invalid +CODE_INVALID,400,Code invalid (i.e. from email) CONNECTION_API_ID_INVALID,400,The provided API id is invalid CONNECTION_DEVICE_MODEL_EMPTY,400,Device model empty CONNECTION_LANG_PACK_INVALID,400,"The specified language pack is not valid. This is meant to be used by official applications only so far, leave it empty" @@ -64,7 +65,8 @@ DATA_JSON_INVALID,400,The provided JSON data is invalid DATE_EMPTY,400,Date empty DC_ID_INVALID,400,This occurs when an authorization is tried to be exported for the same data center one is currently connected to DH_G_A_INVALID,400,g_a invalid -EMAIL_UNCONFIRMED,400,Email unconfirmed +EMAIL_HASH_EXPIRED,400,The email hash expired and cannot be used to verify it +EMAIL_UNCONFIRMED_X,400,"Email unconfirmed, the length of the code must be {code_length}" ENCRYPTED_MESSAGE_INVALID,400,Encrypted message invalid ENCRYPTION_ALREADY_ACCEPTED,400,Secret chat already accepted ENCRYPTION_ALREADY_DECLINED,400,The secret chat was already declined diff --git a/telethon_generator/data/methods.csv b/telethon_generator/data/methods.csv index 0ec74cf9..795ba345 100644 --- a/telethon_generator/data/methods.csv +++ b/telethon_generator/data/methods.csv @@ -15,7 +15,7 @@ account.setAccountTTL,user,TTL_DAYS_INVALID account.setPrivacy,user,PRIVACY_KEY_INVALID account.unregisterDevice,user,TOKEN_INVALID account.updateNotifySettings,user,PEER_ID_INVALID -account.updatePasswordSettings,user,EMAIL_UNCONFIRMED NEW_SALT_INVALID NEW_SETTINGS_INVALID PASSWORD_HASH_INVALID +account.updatePasswordSettings,user,EMAIL_UNCONFIRMED_X NEW_SALT_INVALID NEW_SETTINGS_INVALID PASSWORD_HASH_INVALID account.updateProfile,user,ABOUT_TOO_LONG FIRSTNAME_INVALID account.updateStatus,user,SESSION_PASSWORD_NEEDED account.updateUsername,user,USERNAME_INVALID USERNAME_NOT_MODIFIED USERNAME_OCCUPIED