Fully support email in edit_2fa

This commit is contained in:
Lonami Exo 2019-01-14 13:56:41 +01:00
parent 21545c56bb
commit 51f0bf5d84
3 changed files with 34 additions and 15 deletions

View File

@ -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

View File

@ -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

1 name codes description
52 CHAT_WRITE_FORBIDDEN 403 You can't write in this chat
53 CODE_EMPTY 400 The provided code is empty
54 CODE_HASH_INVALID 400 Code hash invalid
55 CODE_INVALID 400 Code invalid (i.e. from email)
56 CONNECTION_API_ID_INVALID 400 The provided API id is invalid
57 CONNECTION_DEVICE_MODEL_EMPTY 400 Device model empty
58 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
65 DATE_EMPTY 400 Date empty
66 DC_ID_INVALID 400 This occurs when an authorization is tried to be exported for the same data center one is currently connected to
67 DH_G_A_INVALID 400 g_a invalid
68 EMAIL_UNCONFIRMED EMAIL_HASH_EXPIRED 400 Email unconfirmed The email hash expired and cannot be used to verify it
69 EMAIL_UNCONFIRMED_X 400 Email unconfirmed, the length of the code must be {code_length}
70 ENCRYPTED_MESSAGE_INVALID 400 Encrypted message invalid
71 ENCRYPTION_ALREADY_ACCEPTED 400 Secret chat already accepted
72 ENCRYPTION_ALREADY_DECLINED 400 The secret chat was already declined

View File

@ -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

1 method usability errors
15 account.setPrivacy user PRIVACY_KEY_INVALID
16 account.unregisterDevice user TOKEN_INVALID
17 account.updateNotifySettings user PEER_ID_INVALID
18 account.updatePasswordSettings user EMAIL_UNCONFIRMED NEW_SALT_INVALID NEW_SETTINGS_INVALID PASSWORD_HASH_INVALID EMAIL_UNCONFIRMED_X NEW_SALT_INVALID NEW_SETTINGS_INVALID PASSWORD_HASH_INVALID
19 account.updateProfile user ABOUT_TOO_LONG FIRSTNAME_INVALID
20 account.updateStatus user SESSION_PASSWORD_NEEDED
21 account.updateUsername user USERNAME_INVALID USERNAME_NOT_MODIFIED USERNAME_OCCUPIED