mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 05:18:10 +00:00
Re-implement password-protected log-ins and support password recovery
This commit is contained in:
parent
e3371c90f2
commit
d91acfe2ca
@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
import binascii
|
import binascii
|
||||||
import getpass
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
@ -46,9 +45,12 @@ from pyrogram.api.errors import (
|
|||||||
PhoneNumberUnoccupied, PhoneCodeInvalid, PhoneCodeHashEmpty,
|
PhoneNumberUnoccupied, PhoneCodeInvalid, PhoneCodeHashEmpty,
|
||||||
PhoneCodeExpired, PhoneCodeEmpty, SessionPasswordNeeded,
|
PhoneCodeExpired, PhoneCodeEmpty, SessionPasswordNeeded,
|
||||||
PasswordHashInvalid, FloodWait, PeerIdInvalid, FirstnameInvalid, PhoneNumberBanned,
|
PasswordHashInvalid, FloodWait, PeerIdInvalid, FirstnameInvalid, PhoneNumberBanned,
|
||||||
VolumeLocNotFound, UserMigrate, FileIdInvalid, ChannelPrivate, PhoneNumberOccupied)
|
VolumeLocNotFound, UserMigrate, FileIdInvalid, ChannelPrivate, PhoneNumberOccupied,
|
||||||
|
PasswordRecoveryNa, PasswordEmpty
|
||||||
|
)
|
||||||
from pyrogram.client.handlers import DisconnectHandler
|
from pyrogram.client.handlers import DisconnectHandler
|
||||||
from pyrogram.client.handlers.handler import Handler
|
from pyrogram.client.handlers.handler import Handler
|
||||||
|
from pyrogram.client.methods.password.utils import compute_check
|
||||||
from pyrogram.crypto import AES
|
from pyrogram.crypto import AES
|
||||||
from pyrogram.session import Auth, Session
|
from pyrogram.session import Auth, Session
|
||||||
from .dispatcher import Dispatcher
|
from .dispatcher import Dispatcher
|
||||||
@ -574,21 +576,46 @@ class Client(Methods, BaseClient):
|
|||||||
self.first_name = None
|
self.first_name = None
|
||||||
except SessionPasswordNeeded as e:
|
except SessionPasswordNeeded as e:
|
||||||
print(e.MESSAGE)
|
print(e.MESSAGE)
|
||||||
r = self.send(functions.account.GetPassword())
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
r = self.send(functions.account.GetPassword())
|
||||||
|
|
||||||
if self.password is None:
|
if self.password is None:
|
||||||
print("Hint: {}".format(r.hint))
|
print("Hint: {}".format(r.hint))
|
||||||
self.password = getpass.getpass("Enter password: ")
|
|
||||||
|
|
||||||
if type(self.password) is str:
|
self.password = input("Enter password (empty to recover): ")
|
||||||
self.password = r.current_salt + self.password.encode() + r.current_salt
|
|
||||||
|
|
||||||
password_hash = sha256(self.password).digest()
|
if self.password == "":
|
||||||
|
r = self.send(functions.auth.RequestPasswordRecovery())
|
||||||
|
|
||||||
r = self.send(functions.auth.CheckPassword(password_hash))
|
print("An e-mail containing the recovery code has been sent to {}".format(
|
||||||
|
r.email_pattern
|
||||||
|
))
|
||||||
|
|
||||||
|
r = self.send(
|
||||||
|
functions.auth.RecoverPassword(
|
||||||
|
code=input("Enter password recovery code: ")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
r = self.send(
|
||||||
|
functions.auth.CheckPassword(
|
||||||
|
password=compute_check(r, self.password)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except PasswordEmpty as e:
|
||||||
|
if password_hash_invalid_raises:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
print(e.MESSAGE)
|
||||||
|
self.password = None
|
||||||
|
except PasswordRecoveryNa as e:
|
||||||
|
if password_hash_invalid_raises:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
print(e.MESSAGE)
|
||||||
|
self.password = None
|
||||||
except PasswordHashInvalid as e:
|
except PasswordHashInvalid as e:
|
||||||
if password_hash_invalid_raises:
|
if password_hash_invalid_raises:
|
||||||
raise
|
raise
|
||||||
@ -601,6 +628,7 @@ class Client(Methods, BaseClient):
|
|||||||
else:
|
else:
|
||||||
print(e.MESSAGE.format(x=e.x))
|
print(e.MESSAGE.format(x=e.x))
|
||||||
time.sleep(e.x)
|
time.sleep(e.x)
|
||||||
|
self.password = None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(e, exc_info=True)
|
log.error(e, exc_info=True)
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user