diff --git a/server/lobbyconnection.py b/server/lobbyconnection.py index c50c10964..d82197809 100644 --- a/server/lobbyconnection.py +++ b/server/lobbyconnection.py @@ -648,6 +648,26 @@ async def on_player_login( if not conforms_policy: return + old_player = self.player_service.get_player(player_id) + if old_player: + self._logger.debug( + "player %s already signed in: %s", + player_id, old_player + ) + if old_player.lobby_connection is self: + await self.send_warning( + "You are already signed in from this location!" + ) + return + elif old_player.lobby_connection is not None: + with contextlib.suppress(DisconnectedError): + old_player.lobby_connection.write_warning( + "You have been signed out because you signed in " + "elsewhere.", + fatal=True, + style="kick" + ) + self._logger.info( "Login from: %s(id=%s), using method '%s' for session %s", username, @@ -675,22 +695,6 @@ async def on_player_login( lobby_connection=self, leaderboards=self.rating_service.leaderboards ) - - old_player = self.player_service.get_player(self.player.id) - if old_player: - self._logger.debug( - "player %s already signed in: %s", - self.player.id, old_player - ) - if old_player.lobby_connection is not None: - with contextlib.suppress(DisconnectedError): - old_player.lobby_connection.write_warning( - "You have been signed out because you signed in " - "elsewhere.", - fatal=True, - style="kick" - ) - await self.player_service.fetch_player_data(self.player) self.player_service[self.player.id] = self.player diff --git a/tests/integration_tests/test_login.py b/tests/integration_tests/test_login.py index d51a9205d..01fa9447a 100644 --- a/tests/integration_tests/test_login.py +++ b/tests/integration_tests/test_login.py @@ -3,6 +3,8 @@ import jwt import pytest +from tests.utils import fast_forward + from .conftest import ( connect_and_sign_in, connect_client, @@ -244,19 +246,18 @@ async def test_policy_server_contacted(lobby_server, policy_server, player_servi policy_server.verify.assert_called_once() +@fast_forward(15) async def test_server_login_double(lobby_server): proto = await connect_client(lobby_server) await perform_login(proto, ("test", "test_password")) - msg = await proto.read_message() - msg["command"] == "welcome" + await read_until_command(proto, "game_info", timeout=5) # Sign in again with a new protocol object proto2 = await connect_client(lobby_server) await perform_login(proto2, ("test", "test_password")) - msg = await proto2.read_message() - msg["command"] == "welcome" + await read_until_command(proto2, "welcome", timeout=5) - msg = await read_until_command(proto, "notice") + msg = await read_until_command(proto, "notice", timeout=10) assert msg == { "command": "notice", "style": "kick", @@ -264,6 +265,22 @@ async def test_server_login_double(lobby_server): } +@fast_forward(20) +async def test_server_login_double_message(lobby_server): + proto = await connect_client(lobby_server) + await perform_login(proto, ("test", "test_password")) + await read_until_command(proto, "game_info", timeout=5) + + # Sign in again with the same connection + await perform_login(proto, ("test", "test_password")) + msg = await read_until_command(proto, "notice", timeout=10) + assert msg == { + "command": "notice", + "style": "info", + "text": "You are already signed in from this location!" + } + + async def test_server_login_token_valid(lobby_server, jwk_priv_key, jwk_kid, fixed_time): proto = await connect_client(lobby_server) await proto.send_message({