3 Commits

Author SHA1 Message Date
David Rodenkirchen 36418470a6 make login more sturdy 2026-05-28 13:16:00 +02:00
David Rodenkirchen 11724ad0d9 improve error handling 2026-05-28 13:10:01 +02:00
David Rodenkirchen edeefe072d Cleanup requirements 2026-05-28 13:08:48 +02:00
4 changed files with 14 additions and 3 deletions
BIN
View File
Binary file not shown.
+3
View File
@@ -32,6 +32,9 @@ class LoginPage(Component):
self.login_in_progress = True self.login_in_progress = True
user_name = copy(self.user_name) # Prevents race condition name swap user_name = copy(self.user_name) # Prevents race condition name swap
is_valid = await self.session[UserService].is_login_valid(user_name, self.password) is_valid = await self.session[UserService].is_login_valid(user_name, self.password)
if not is_valid: # Migrated users
user_name = user_name.lower().capitalize()
is_valid = await self.session[UserService].is_login_valid(user_name, self.password)
if is_valid: if is_valid:
user: User = await self.session[UserService].get_user(user_name) user: User = await self.session[UserService].get_user(user_name)
self.error_on_last_attempt = False self.error_on_last_attempt = False
+11
View File
@@ -1,8 +1,10 @@
import logging import logging
import sys
from beanie import init_beanie from beanie import init_beanie
from pymongo import AsyncMongoClient from pymongo import AsyncMongoClient
from pymongo.asynchronous.collection import AsyncCollection from pymongo.asynchronous.collection import AsyncCollection
from pymongo.errors import ServerSelectionTimeoutError, OperationFailure
from elm.types import User, Transaction, Ticket, Seat, CateringTypes from elm.types import User, Transaction, Ticket, Seat, CateringTypes
from elm.types.ConfigurationTypes import DatabaseConfiguration from elm.types.ConfigurationTypes import DatabaseConfiguration
@@ -30,6 +32,15 @@ class DatabaseService:
if self._client is None: if self._client is None:
self._client = AsyncMongoClient(mongo_uri) self._client = AsyncMongoClient(mongo_uri)
try:
await self._client.admin.command("ping")
except ServerSelectionTimeoutError:
print("Could not connect to mongodb")
sys.exit(1)
except OperationFailure:
print("Authentication with mongodb failed")
sys.exit(1)
self._database = self._client[ self._database = self._client[
self._db_config.database_name self._db_config.database_name
] ]
-3
View File
@@ -1,4 +1,3 @@
from asyncio import sleep
from hashlib import sha256 from hashlib import sha256
from typing import Optional from typing import Optional
from string import ascii_letters, digits from string import ascii_letters, digits
@@ -69,8 +68,6 @@ class UserService:
async def is_login_valid(self, user_name: str, password_clear_text: str) -> bool: async def is_login_valid(self, user_name: str, password_clear_text: str) -> bool:
user = await self.get_user(user_name) user = await self.get_user(user_name)
if not user:
user = await self.get_user(user_name.lower()) # Migrated users had all lowercase names
user_password_hash = sha256(password_clear_text.encode(encoding="utf-8")).hexdigest() user_password_hash = sha256(password_clear_text.encode(encoding="utf-8")).hexdigest()
if not user: if not user:
return False return False