diff --git a/packages/python-backend/src/shared_backend/__init__.py b/packages/python-backend/src/shared_backend/__init__.py index b506ebb..8a5cf60 100644 --- a/packages/python-backend/src/shared_backend/__init__.py +++ b/packages/python-backend/src/shared_backend/__init__.py @@ -1,10 +1,10 @@ """Shared PostgreSQL and authentication primitives for Python services.""" from .auth import ( + RESET_CODE_TTL, ROLE_ADMIN, ROLE_SUPERADMIN, ROLE_USER, - RESET_CODE_TTL, AuthStore, AuthUser, ResetManager, @@ -16,8 +16,8 @@ from .auth import ( ) from .config import DatabaseConfig, database_url_from_env from .postgres import Database -from .totp import generate_totp_secret, totp_provisioning_uri, verify_totp from .tokens import hash_token, new_session_token +from .totp import generate_totp_secret, totp_provisioning_uri, verify_totp __all__ = [ "AuthStore", diff --git a/packages/python-backend/src/shared_backend/auth.py b/packages/python-backend/src/shared_backend/auth.py index faacb7f..3c3dd46 100644 --- a/packages/python-backend/src/shared_backend/auth.py +++ b/packages/python-backend/src/shared_backend/auth.py @@ -63,7 +63,10 @@ def can_manage_users(role: str | None) -> bool: def can_assign_role(actor_role: str | None, target_role: str | None) -> bool: - return normalize_role(actor_role) == ROLE_SUPERADMIN and normalize_role(target_role) != ROLE_SUPERADMIN + return ( + normalize_role(actor_role) == ROLE_SUPERADMIN + and normalize_role(target_role) != ROLE_SUPERADMIN + ) class ResetManager: @@ -153,7 +156,8 @@ class AuthStore: """ INSERT INTO auth_users (username, email, password_hash, role, updated_at) VALUES ($1, $2, $3, - CASE WHEN EXISTS (SELECT 1 FROM auth_users) THEN 'user' ELSE 'superadmin' END, + CASE WHEN EXISTS (SELECT 1 FROM auth_users) THEN 'user' + ELSE 'superadmin' END, now()) RETURNING id, username, email, password_hash, role, created_at, updated_at """, @@ -256,17 +260,23 @@ class AuthStore: ) -> None: fields: list[str] = [] values: list[object] = [] - for column, value in (("username", username), ("email", email), ("password_hash", password_hash), ("role", normalize_role(role) if role is not None else None)): + for column, value in ( + ("username", username), + ("email", email), + ("password_hash", password_hash), + ("role", normalize_role(role) if role is not None else None), + ): if value is not None: fields.append(f"{column} = ${len(values) + 1}") values.append(value) if not fields: return values.append(user_id) - await self.database.execute( - f"UPDATE auth_users SET {', '.join(fields)}, updated_at = now() WHERE id = ${len(values)}", - *values, + query = ( + f"UPDATE auth_users SET {', '.join(fields)}, " + f"updated_at = now() WHERE id = ${len(values)}" ) + await self.database.execute(query, *values) async def delete_user(self, user_id: int) -> None: await self.database.execute("DELETE FROM auth_users WHERE id = $1", user_id) @@ -284,7 +294,8 @@ class AuthStore: async def set_totp_secret(self, user_id: int, secret: str) -> None: await self.database.execute( - "UPDATE auth_users SET totp_secret = $1, totp_enabled = false, updated_at = now() WHERE id = $2", + "UPDATE auth_users SET totp_secret = $1, totp_enabled = false, " + "updated_at = now() WHERE id = $2", secret, user_id, ) @@ -298,7 +309,8 @@ class AuthStore: async def disable_totp(self, user_id: int) -> None: await self.database.execute( - "UPDATE auth_users SET totp_secret = '', totp_enabled = false, updated_at = now() WHERE id = $1", + "UPDATE auth_users SET totp_secret = '', totp_enabled = false, " + "updated_at = now() WHERE id = $1", user_id, ) diff --git a/packages/python-backend/tests/test_core.py b/packages/python-backend/tests/test_core.py index 95e4193..65ce7f8 100644 --- a/packages/python-backend/tests/test_core.py +++ b/packages/python-backend/tests/test_core.py @@ -1,12 +1,11 @@ import os import unittest - from datetime import timedelta from shared_backend.auth import ResetManager, hash_password, verify_password from shared_backend.config import DatabaseConfig -from shared_backend.totp import totp_provisioning_uri, verify_totp from shared_backend.tokens import hash_token, new_session_token +from shared_backend.totp import totp_provisioning_uri, verify_totp class SharedBackendTests(unittest.TestCase):