mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-07-10 16:12:22 +03:00
Created Two Factor Authorization (markdown)
parent
9c105f1657
commit
45bad03e77
43
Two-Factor-Authorization.md
Normal file
43
Two-Factor-Authorization.md
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
If you have Two Factor Authorization (from now on, 2FA) enabled on your account, calling `.sign_in` will raise a `SessionPasswordNeededError`. When this happens, just `.sign_in()` again with a `password=`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from telethon.errors import SessionPasswordNeededError
|
||||||
|
|
||||||
|
client.sign_in(phone)
|
||||||
|
try:
|
||||||
|
client.sign_in(code=input('Enter code: '))
|
||||||
|
except SessionPasswordNeededError:
|
||||||
|
client.sign_in(password=input('Enter password: '))
|
||||||
|
```
|
||||||
|
|
||||||
|
## Enabling 2FA
|
||||||
|
|
||||||
|
If you don't have 2FA enabled, but you would like to do so through Telethon, take as example the following code snippet:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import os
|
||||||
|
from hashlib import sha256
|
||||||
|
from telethon.tl.functions import account
|
||||||
|
from telethon.tl.types.account import PasswordInputSettings
|
||||||
|
|
||||||
|
new_salt = client(account.GetPasswordRequest()).new_salt
|
||||||
|
salt = new_salt + os.urandom(8) # new random salt
|
||||||
|
|
||||||
|
pw = 'secret'.encode('utf-8') # type your new password here
|
||||||
|
hint = 'hint'
|
||||||
|
|
||||||
|
pw_salted = salt + pw + salt
|
||||||
|
pw_hash = sha256(pw_salted).digest()
|
||||||
|
|
||||||
|
password_new = PasswordInputSettings(new_salt=salt, new_password_hash=pw_hash, hint='hint')
|
||||||
|
result = client(account.UpdatePasswordSettingsRequest(
|
||||||
|
current_password_hash=salt,
|
||||||
|
new_settings=PasswordInputSettings(
|
||||||
|
new_salt=salt,
|
||||||
|
new_password_hash=pw_hash,
|
||||||
|
hint=hint
|
||||||
|
)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Thanks to [issue 259](https://github.com/LonamiWebs/Telethon/issues/259) for the tip!
|
Loading…
Reference in New Issue
Block a user