feat(login): enhance login flow with UserID check
All checks were successful
Docker CI/CD / Docker Build and Push to Docker Hub (push) Successful in 8m17s
Python Package CI/CD / Setup and Test (push) Successful in 1m11s
Python Package CI/CD / Publish to PyPI (push) Successful in 36s

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.
This commit is contained in:
Kumi 2024-05-21 08:14:04 +02:00
parent e1695f0cce
commit 75e637546a
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 17 additions and 5 deletions

View file

@ -1,5 +1,9 @@
# Changelog # 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) ### 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. - **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.

View file

@ -7,7 +7,7 @@ allow-direct-references = true
[project] [project]
name = "matrix-gptbot" name = "matrix-gptbot"
version = "0.3.13" version = "0.3.14"
authors = [ authors = [
{ name = "Kumi Mitterer", email = "gptbot@kumi.email" }, { name = "Kumi Mitterer", email = "gptbot@kumi.email" },

View file

@ -31,6 +31,7 @@ from nio import (
RoomGetStateError, RoomGetStateError,
DiskDownloadResponse, DiskDownloadResponse,
MemoryDownloadResponse, MemoryDownloadResponse,
LoginError,
) )
from nio.store import SqliteStore from nio.store import SqliteStore
@ -243,17 +244,24 @@ class GPTBot:
assert "Matrix" in config, "Matrix config not found" assert "Matrix" in config, "Matrix config not found"
homeserver = config["Matrix"]["Homeserver"] homeserver = config["Matrix"]["Homeserver"]
bot.matrix_client = AsyncClient(homeserver)
if ("Password" in config["Matrix"]) and config.get("Matrix", "Password"): if config.get("Matrix", "Password"):
await bot.matrix_client.login(password=config["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"]["AccessToken"] = bot.matrix_client.access_token
config["Matrix"]["UserID"] = bot.matrix_client.user_id
config["Matrix"]["DeviceID"] = bot.matrix_client.device_id config["Matrix"]["DeviceID"] = bot.matrix_client.device_id
config["Matrix"]["Password"] = "" config["Matrix"]["Password"] = ""
else: else:
bot.matrix_client = AsyncClient(homeserver)
bot.matrix_client.access_token = config["Matrix"]["AccessToken"] bot.matrix_client.access_token = config["Matrix"]["AccessToken"]
bot.matrix_client.user_id = config["Matrix"].get("UserID") bot.matrix_client.user_id = config["Matrix"].get("UserID")
bot.matrix_client.device_id = config["Matrix"].get("DeviceID") bot.matrix_client.device_id = config["Matrix"].get("DeviceID")