From 75e637546aa924bb238e1117d37d89cf1b53ffda Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 21 May 2024 08:14:04 +0200 Subject: [PATCH] feat(login): enhance login flow with UserID check Improved the login logic in the bot's initialization process to require a UserID when a Password is provided for login. This update ensures a more secure and fail-proof login procedure by validating the presence of a UserID before attempting to log in, and by handling LoginError more explicitly with a clear error message. This change addresses the need for better error handling and validation during the bot's login phase to avoid silent failures and improve debuggability. - Added LoginError import to handle login-related exceptions more gracefully. - Refined the login process to create the AsyncClient instance with a UserID when password authentication is used, following best practices for client identification. - Introduced explicit error raising for missing UserID configuration, enhancing configuration validation before attempting a login. - Improved clarity and security by clearing the password from the configuration post-login, preventing inadvertent storage or reuse. This update enhances the bot's robustness and configuration validation, ensuring smoother operations and better error handling during the initialization phase. --- CHANGELOG.md | 4 ++++ pyproject.toml | 2 +- src/gptbot/classes/bot.py | 16 ++++++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa00e3f..e03eb82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### 0.3.14 (2024-05-21) + +- Fixed issue in handling of login credentials, added error handling for login failures + ### 0.3.13 (2024-05-20) - **Breaking Change**: The `ForceTools` configuration option behavior has changed. Instead of using a separate model for tools, the bot will now try to use the default chat model for tool requests, even if that model is not known to support tools. diff --git a/pyproject.toml b/pyproject.toml index 3511bed..46ac095 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ allow-direct-references = true [project] name = "matrix-gptbot" -version = "0.3.13" +version = "0.3.14" authors = [ { name = "Kumi Mitterer", email = "gptbot@kumi.email" }, diff --git a/src/gptbot/classes/bot.py b/src/gptbot/classes/bot.py index 183ee7f..41ea82d 100644 --- a/src/gptbot/classes/bot.py +++ b/src/gptbot/classes/bot.py @@ -31,6 +31,7 @@ from nio import ( RoomGetStateError, DiskDownloadResponse, MemoryDownloadResponse, + LoginError, ) from nio.store import SqliteStore @@ -243,17 +244,24 @@ class GPTBot: assert "Matrix" in config, "Matrix config not found" homeserver = config["Matrix"]["Homeserver"] - bot.matrix_client = AsyncClient(homeserver) - if ("Password" in config["Matrix"]) and config.get("Matrix", "Password"): - await bot.matrix_client.login(password=config["Matrix"]["Password"]) + if config.get("Matrix", "Password"): + if not config.get("Matrix", "UserID"): + raise Exception("Cannot log in: UserID not set in config") + + bot.matrix_client = AsyncClient(homeserver, user=config["Matrix"]["UserID"]) + login = await bot.matrix_client.login(password=config["Matrix"]["Password"]) + + if isinstance(login, LoginError): + raise Exception(f"Could not log in: {login.message}") config["Matrix"]["AccessToken"] = bot.matrix_client.access_token - config["Matrix"]["UserID"] = bot.matrix_client.user_id config["Matrix"]["DeviceID"] = bot.matrix_client.device_id config["Matrix"]["Password"] = "" else: + bot.matrix_client = AsyncClient(homeserver) + bot.matrix_client.access_token = config["Matrix"]["AccessToken"] bot.matrix_client.user_id = config["Matrix"].get("UserID") bot.matrix_client.device_id = config["Matrix"].get("DeviceID")