def restore_user_session(sio: ServerApp, encrypted_session: bytes, session_id: Optional[int]): try: decrypted_session: bytes = sio.fernet_encrypt.decrypt( encrypted_session) session = json.loads(decrypted_session.decode("utf-8")) user = User.get_by_id(session["user-id"]) if "discord-access-token" in session: # TODO: test if the discord access token is still valid flask.session["DISCORD_OAUTH2_TOKEN"] = session[ "discord-access-token"] sio.get_server().save_session(flask.request.sid, session) if session_id is not None: sio.join_game_session( GameSessionMembership.get_by_ids(user.id, session_id)) return _create_client_side_session(sio, user) except (KeyError, peewee.DoesNotExist, json.JSONDecodeError): raise InvalidSession() except Exception: logger().exception("Error decoding user session") raise InvalidSession()
def restore_user_session(sio: ServerApp, encrypted_session: bytes, session_id: Optional[int]): try: decrypted_session: bytes = sio.fernet_encrypt.decrypt( encrypted_session) session = json.loads(decrypted_session.decode("utf-8")) if "discord-access-token" in session: user, result = _create_session_with_discord_token( sio, session["discord-access-token"]) else: user = User.get_by_id(session["user-id"]) sio.save_session(session) result = _create_client_side_session(sio, user) if session_id is not None: sio.join_game_session( GameSessionMembership.get_by_ids(user.id, session_id)) return result except UserNotAuthorized: raise except (KeyError, peewee.DoesNotExist, json.JSONDecodeError, InvalidTokenError) as e: # InvalidTokenError: discord token expired and couldn't renew logger().info("Client at %s was unable to restore session: (%s) %s", sio.current_client_ip(), str(type(e)), str(e)) raise InvalidSession() except Exception: logger().exception("Error decoding user session") raise InvalidSession()
def get_current_user(self) -> User: try: return User.get_by_id(self.get_session()["user-id"]) except KeyError: raise NotLoggedIn() except peewee.DoesNotExist: raise InvalidSession()
async def test_on_connect_restore(tmpdir, valid_session: bool): client = NetworkClient(Path(tmpdir), {"server_address": "http://*****:*****@30VtI6Ba{' }} else: call_result = InvalidSession().as_json client.sio = MagicMock() client.sio.call = AsyncMock(return_value=call_result) # Run await client.on_connect() # Assert client.sio.call.assert_awaited_once_with("restore_user_session", (b"foo", None), namespace=None, timeout=30) if valid_session: assert client.connection_state == ConnectionState.Connected assert session_data_path.read_bytes() == b"new_bytes" else: assert client.connection_state == ConnectionState.ConnectedNotLogged assert not session_data_path.is_file()