From d514b929b3a956de96e7cd50ae698282db0205fd Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 15 Nov 2018 11:45:46 +0200 Subject: [PATCH] Automatically log out when logging in with a user someone logged in with previously. Fixes #198 --- mautrix_telegram/commands/auth.py | 8 +++++++- mautrix_telegram/web/common/auth_api.py | 21 ++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/mautrix_telegram/commands/auth.py b/mautrix_telegram/commands/auth.py index fb4bc0ea..3f57cbb9 100644 --- a/mautrix_telegram/commands/auth.py +++ b/mautrix_telegram/commands/auth.py @@ -24,7 +24,7 @@ from telethon.errors import ( PhoneNumberOccupiedError, PhoneNumberUnoccupiedError, SessionPasswordNeededError) from . import command_handler, CommandEvent, SECTION_AUTH -from .. import puppet as pu +from .. import puppet as pu, user as u from ..util import format_duration @@ -298,6 +298,12 @@ async def sign_in(evt: CommandEvent, **sign_in_info) -> Dict: try: await evt.sender.ensure_started(even_if_no_session=True) user = await evt.sender.client.sign_in(**sign_in_info) + existing_user = u.User.get_by_tgid(user.id) + if existing_user != evt.sender: + await existing_user.log_out() + await evt.reply(f"[{existing_user.displayname}]" + f"(https://matrix.to/#/{existing_user.mxid})" + " was logged out from the account.") asyncio.ensure_future(evt.sender.post_login(user), loop=evt.loop) evt.sender.command_status = None name = f"@{user.username}" if user.username else f"+{user.phone}" diff --git a/mautrix_telegram/web/common/auth_api.py b/mautrix_telegram/web/common/auth_api.py index f4b2da93..700b26c1 100644 --- a/mautrix_telegram/web/common/auth_api.py +++ b/mautrix_telegram/web/common/auth_api.py @@ -108,12 +108,19 @@ class AuthAPI(abc.ABC): errcode="unknown_error", error="Internal server error while requesting code.") + async def postprocess_login(self, user: User, user_info) -> None: + existing_user = User.get_by_tgid(user_info.id) + if existing_user != user: + await existing_user.log_out() + asyncio.ensure_future(user.post_login(user_info), loop=self.loop) + if user.command_status and user.command_status["action"] == "Login": + user.command_status = None + + async def post_login_token(self, user: User, token: str) -> web.Response: try: user_info = await user.client.sign_in(bot_token=token) - asyncio.ensure_future(user.post_login(user_info), loop=self.loop) - if user.command_status and user.command_status["action"] == "Login": - user.command_status = None + await self.postprocess_login(user, user_info) return self.get_login_response(mxid=user.mxid, state="logged-in", status=200, username=user_info.username, phone=None, human_tg_id=f"@{user_info.username}") @@ -134,9 +141,7 @@ class AuthAPI(abc.ABC): ) -> Optional[web.Response]: try: user_info = await user.client.sign_in(code=code) - asyncio.ensure_future(user.post_login(user_info), loop=self.loop) - if user.command_status and user.command_status["action"] == "Login": - user.command_status = None + await self.postprocess_login(user, user_info) human_tg_id = f"@{user_info.username}" if user_info.username else f"+{user_info.phone}" return self.get_login_response(mxid=user.mxid, state="logged-in", status=200, username=user_info.username, phone=user_info.phone, @@ -169,9 +174,7 @@ class AuthAPI(abc.ABC): async def post_login_password(self, user: User, password: str) -> web.Response: try: user_info = await user.client.sign_in(password=password) - asyncio.ensure_future(user.post_login(user_info), loop=self.loop) - if user.command_status and user.command_status["action"] == "Login (password entry)": - user.command_status = None + await self.postprocess_login(user, user_info) human_tg_id = f"@{user_info.username}" if user_info.username else f"+{user_info.phone}" return self.get_login_response(mxid=user.mxid, state="logged-in", status=200, username=user_info.username, phone=user_info.phone,