Add a warning when trying to connect to a different account

Closes #1172, and also fixed a typo.
This commit is contained in:
Lonami Exo 2020-08-08 17:47:58 +02:00
parent 958698bba7
commit ddeefff431
2 changed files with 25 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import inspect
import os import os
import sys import sys
import typing import typing
import warnings
from .. import utils, helpers, errors, password as pwd_mod from .. import utils, helpers, errors, password as pwd_mod
from ..tl import types, functions, custom from ..tl import types, functions, custom
@ -138,7 +139,29 @@ class AuthMethods:
if not self.is_connected(): if not self.is_connected():
await self.connect() await self.connect()
if await self.is_user_authorized(): # Rather than using `is_user_authorized`, use `get_me`. While this is
# more expensive and needs to retrieve more data from the server, it
# enables the library to warn users trying to login to a different
# account. See #1172.
me = await self.get_me()
if me is not None:
# The warnings here are on a best-effort and may fail.
if bot_token:
# bot_token's first part has the bot ID, but it may be invalid
# so don't try to parse as int (instead cast our ID to string).
if bot_token[:bot_token.find(':')] != str(me.id):
warnings.warn(
'the session already had an authorized user so it did '
'not login to the bot account using the provided '
'bot_token (it may not be using the user you expect)'
)
elif not callable(phone) and phone != me.phone:
warnings.warn(
'the session already had an authorized user so it did '
'not login to the user account using the provided '
'phone (it may not be using the user you expect)'
)
return self return self
if not bot_token: if not bot_token:

View File

@ -562,7 +562,7 @@ class DownloadMethods:
# Streaming `media` to an output file # Streaming `media` to an output file
# After the iteration ends, the sender is cleaned up # After the iteration ends, the sender is cleaned up
with open('photo.jpg', 'wb') as fd: with open('photo.jpg', 'wb') as fd:
async for chunk client.iter_download(media): async for chunk in client.iter_download(media):
fd.write(chunk) fd.write(chunk)
# Fetching only the header of a file (32 bytes) # Fetching only the header of a file (32 bytes)