Support direct sign_in with code and password

This commit is contained in:
Lonami Exo 2022-02-17 11:26:53 +01:00
parent 77a98fed2c
commit 94ac667e79

View File

@ -203,29 +203,36 @@ async def sign_in(
code: typing.Union[str, int] = None, code: typing.Union[str, int] = None,
password: str = None, password: str = None,
bot_token: str = None,) -> 'typing.Union[_tl.User, _tl.auth.SentCode]': bot_token: str = None,) -> 'typing.Union[_tl.User, _tl.auth.SentCode]':
if code and bot_token:
raise ValueError('Can only provide one of code or bot_token, not both')
if not code and not bot_token and not password:
raise ValueError('You must provide code, password, or bot_token.')
if code: if code:
if not self._phone_code_hash: if not self._phone_code_hash:
raise ValueError('Must call client.send_code_request before sign in') raise ValueError('Must call client.send_code_request before sign in')
phone, phone_code_hash = self._phone_code_hash
# May raise PhoneCodeEmptyError, PhoneCodeExpiredError, # May raise PhoneCodeEmptyError, PhoneCodeExpiredError,
# PhoneCodeHashEmptyError or PhoneCodeInvalidError. # PhoneCodeHashEmptyError or PhoneCodeInvalidError.
request = _tl.fn.auth.SignIn(*self._phone_code_hash, str(code)) try:
elif password: result = await self(_tl.fn.auth.SignIn(*self._phone_code_hash, str(code)))
pwd = await self(_tl.fn.account.GetPassword()) password = None # user provided a password but it was not needed
request = _tl.fn.auth.CheckPassword( except errors.SessionPasswordNeededError:
pwd_mod.compute_check(pwd, password) if not password:
) raise
elif bot_token: elif bot_token:
request = _tl.fn.auth.ImportBotAuthorization( result = await self(_tl.fn.auth.ImportBotAuthorization(
flags=0, bot_auth_token=bot_token, flags=0, bot_auth_token=bot_token,
api_id=self._api_id, api_hash=self._api_hash api_id=self._api_id, api_hash=self._api_hash
) ))
else:
raise ValueError('You must provide code, password, or bot_token.') if password:
pwd = await self(_tl.fn.account.GetPassword())
result = await self(_tl.fn.auth.CheckPassword(
pwd_mod.compute_check(pwd, password)
))
result = await self(request)
if isinstance(result, _tl.auth.AuthorizationSignUpRequired): if isinstance(result, _tl.auth.AuthorizationSignUpRequired):
# The method must return the User but we don't have it, so raise instead (matches pre-layer 104 behaviour) # The method must return the User but we don't have it, so raise instead (matches pre-layer 104 behaviour)
self._tos = result.terms_of_service self._tos = result.terms_of_service