Skip to content

Commit

Permalink
Automatically log out when logging in with a user someone logged in w…
Browse files Browse the repository at this point in the history
…ith previously. Fixes #198
  • Loading branch information
tulir committed Nov 15, 2018
1 parent 720210a commit d514b92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
8 changes: 7 additions & 1 deletion mautrix_telegram/commands/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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


Expand Down Expand Up @@ -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}"
Expand Down
21 changes: 12 additions & 9 deletions mautrix_telegram/web/common/auth_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,19 @@ async def post_login_phone(self, user: User, phone: str) -> web.Response:
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}")
Expand All @@ -134,9 +141,7 @@ async def post_login_code(self, user: User, code: int, password_in_data: bool
) -> 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,
Expand Down Expand Up @@ -169,9 +174,7 @@ async def post_login_code(self, user: User, code: int, password_in_data: bool
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,
Expand Down

0 comments on commit d514b92

Please sign in to comment.