Catch authorization errors in get_me()

This commit is contained in:
Tulir Asokan
2021-10-20 20:02:09 +03:00
parent a132916525
commit e4a2bd2f69
3 changed files with 42 additions and 18 deletions
+7 -4
View File
@@ -46,10 +46,13 @@ except ImportError:
help_section=SECTION_AUTH, help_section=SECTION_AUTH,
help_text="Check if you're logged into Telegram.") help_text="Check if you're logged into Telegram.")
async def ping(evt: CommandEvent) -> EventID: async def ping(evt: CommandEvent) -> EventID:
me = await evt.sender.client.get_me() if await evt.sender.is_logged_in() else None if await evt.sender.is_logged_in():
if me: me = await evt.sender.get_me()
human_tg_id = f"@{me.username}" if me.username else f"+{me.phone}" if me:
return await evt.reply(f"You're logged in as {human_tg_id}") human_tg_id = f"@{me.username}" if me.username else f"+{me.phone}"
return await evt.reply(f"You're logged in as {human_tg_id}")
else:
return await evt.reply("You were logged in, but there appears to have been an error.")
else: else:
return await evt.reply("You're not logged in.") return await evt.reply("You're not logged in.")
+20 -3
View File
@@ -22,12 +22,14 @@ import asyncio
from telethon.tl.types import (TypeUpdate, UpdateNewMessage, UpdateNewChannelMessage, from telethon.tl.types import (TypeUpdate, UpdateNewMessage, UpdateNewChannelMessage,
UpdateShortChatMessage, UpdateShortMessage, User as TLUser, Chat, UpdateShortChatMessage, UpdateShortMessage, User as TLUser, Chat,
ChatForbidden, UpdateFolderPeers, UpdatePinnedDialogs, ChatForbidden, UpdateFolderPeers, UpdatePinnedDialogs,
UpdateNotifySettings, NotifyPeer) UpdateNotifySettings, NotifyPeer, InputUserSelf)
from telethon.tl.custom import Dialog from telethon.tl.custom import Dialog
from telethon.tl.types.contacts import ContactsNotModified from telethon.tl.types.contacts import ContactsNotModified
from telethon.tl.functions.contacts import GetContactsRequest, SearchRequest from telethon.tl.functions.contacts import GetContactsRequest, SearchRequest
from telethon.tl.functions.account import UpdateStatusRequest from telethon.tl.functions.account import UpdateStatusRequest
from telethon.errors import AuthKeyDuplicatedError from telethon.tl.functions.users import GetUsersRequest
from telethon.errors import (AuthKeyDuplicatedError, UserDeactivatedError, UserDeactivatedBanError,
SessionRevokedError, UnauthorizedError)
from mautrix.client import Client from mautrix.client import Client
from mautrix.errors import MatrixRequestError, MNotFound from mautrix.errors import MatrixRequestError, MNotFound
@@ -333,8 +335,22 @@ class User(AbstractUser, BaseUser):
if not self.is_bot: if not self.is_bot:
await self.client(UpdateStatusRequest(offline=not online)) await self.client(UpdateStatusRequest(offline=not online))
async def get_me(self) -> Optional[TLUser]:
try:
return (await self.client(GetUsersRequest([InputUserSelf()])))[0]
except UnauthorizedError as e:
self.log.error(f"Authorization error in get_me(): {e}")
await self.push_bridge_state(BridgeStateEvent.BAD_CREDENTIALS, error="tg-auth-error",
message=str(e), ttl=3600)
await self.stop()
return None
async def update_info(self, info: TLUser = None) -> None: async def update_info(self, info: TLUser = None) -> None:
info = info or await self.client.get_me() if not info:
info = await self.get_me()
if not info:
self.log.warning("get_me() returned None, aborting update_info()")
return
changed = False changed = False
if self.is_bot != info.bot: if self.is_bot != info.bot:
self.is_bot = info.bot self.is_bot = info.bot
@@ -350,6 +366,7 @@ class User(AbstractUser, BaseUser):
self.by_tgid[self.tgid] = self self.by_tgid[self.tgid] = self
if changed: if changed:
await self.save() await self.save()
return info
async def log_out(self) -> bool: async def log_out(self) -> bool:
puppet = pu.Puppet.get(self.tgid) puppet = pu.Puppet.get(self.tgid)
+15 -11
View File
@@ -21,7 +21,10 @@ import json
from aiohttp import web from aiohttp import web
from telethon.utils import get_peer_id, resolve_id from telethon.utils import get_peer_id, resolve_id
from telethon.tl.types import ChatForbidden, ChannelForbidden, TypeChat from telethon.tl.types import ChatForbidden, ChannelForbidden, TypeChat, InputUserSelf
from telethon.tl.functions.users import GetUsersRequest
from telethon.errors import (UserDeactivatedError, UserDeactivatedBanError, SessionRevokedError,
UnauthorizedError)
from mautrix.appservice import AppService from mautrix.appservice import AppService
from mautrix.errors import MatrixRequestError, IntentError from mautrix.errors import MatrixRequestError, IntentError
@@ -294,16 +297,17 @@ class ProvisioningAPI(AuthAPI):
user_data = None user_data = None
if await user.is_logged_in(): if await user.is_logged_in():
me = await user.client.get_me() me = await user.get_me()
await user.update_info(me) if me:
user_data = { await user.update_info(me)
"id": user.tgid, user_data = {
"username": user.username, "id": user.tgid,
"first_name": me.first_name, "username": user.username,
"last_name": me.last_name, "first_name": me.first_name,
"phone": me.phone, "last_name": me.last_name,
"is_bot": user.is_bot, "phone": me.phone,
} "is_bot": user.is_bot,
}
return web.json_response({ return web.json_response({
"telegram": user_data, "telegram": user_data,
"mxid": user.mxid, "mxid": user.mxid,