fix: satisfy Ruff lint checks
Lint / Lint (Node/Svelte) (push) Successful in 14s
Lint / Lint (Go) (push) Successful in 16s
Lint / Lint (Python) (push) Successful in 9s
Tests / Test (Node/Svelte) (push) Successful in 14s
Tests / Test (Go) (push) Successful in 18s
Tests / Test (Python) (push) Successful in 10s
Lint / Lint (Node/Svelte) (push) Successful in 14s
Lint / Lint (Go) (push) Successful in 16s
Lint / Lint (Python) (push) Successful in 9s
Tests / Test (Node/Svelte) (push) Successful in 14s
Tests / Test (Go) (push) Successful in 18s
Tests / Test (Python) (push) Successful in 10s
This commit is contained in:
1 parent
da97fd4fb1
commit
2093c39ed2
3 files changed
+23
-12
No files matched your search
@@ -1,10 +1,10 @@
|
|||||||
"""Shared PostgreSQL and authentication primitives for Python services."""
|
"""Shared PostgreSQL and authentication primitives for Python services."""
|
||||||
|
|
||||||
from .auth import (
|
from .auth import (
|
||||||
|
RESET_CODE_TTL,
|
||||||
ROLE_ADMIN,
|
ROLE_ADMIN,
|
||||||
ROLE_SUPERADMIN,
|
ROLE_SUPERADMIN,
|
||||||
ROLE_USER,
|
ROLE_USER,
|
||||||
RESET_CODE_TTL,
|
|
||||||
AuthStore,
|
AuthStore,
|
||||||
AuthUser,
|
AuthUser,
|
||||||
ResetManager,
|
ResetManager,
|
||||||
@@ -16,8 +16,8 @@ from .auth import (
|
|||||||
)
|
)
|
||||||
from .config import DatabaseConfig, database_url_from_env
|
from .config import DatabaseConfig, database_url_from_env
|
||||||
from .postgres import Database
|
from .postgres import Database
|
||||||
from .totp import generate_totp_secret, totp_provisioning_uri, verify_totp
|
|
||||||
from .tokens import hash_token, new_session_token
|
from .tokens import hash_token, new_session_token
|
||||||
|
from .totp import generate_totp_secret, totp_provisioning_uri, verify_totp
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"AuthStore",
|
"AuthStore",
|
||||||
|
|||||||
@@ -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:
|
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:
|
class ResetManager:
|
||||||
@@ -153,7 +156,8 @@ class AuthStore:
|
|||||||
"""
|
"""
|
||||||
INSERT INTO auth_users (username, email, password_hash, role, updated_at)
|
INSERT INTO auth_users (username, email, password_hash, role, updated_at)
|
||||||
VALUES ($1, $2, $3,
|
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())
|
now())
|
||||||
RETURNING id, username, email, password_hash, role, created_at, updated_at
|
RETURNING id, username, email, password_hash, role, created_at, updated_at
|
||||||
""",
|
""",
|
||||||
@@ -256,17 +260,23 @@ class AuthStore:
|
|||||||
) -> None:
|
) -> None:
|
||||||
fields: list[str] = []
|
fields: list[str] = []
|
||||||
values: list[object] = []
|
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:
|
if value is not None:
|
||||||
fields.append(f"{column} = ${len(values) + 1}")
|
fields.append(f"{column} = ${len(values) + 1}")
|
||||||
values.append(value)
|
values.append(value)
|
||||||
if not fields:
|
if not fields:
|
||||||
return
|
return
|
||||||
values.append(user_id)
|
values.append(user_id)
|
||||||
await self.database.execute(
|
query = (
|
||||||
f"UPDATE auth_users SET {', '.join(fields)}, updated_at = now() WHERE id = ${len(values)}",
|
f"UPDATE auth_users SET {', '.join(fields)}, "
|
||||||
*values,
|
f"updated_at = now() WHERE id = ${len(values)}"
|
||||||
)
|
)
|
||||||
|
await self.database.execute(query, *values)
|
||||||
|
|
||||||
async def delete_user(self, user_id: int) -> None:
|
async def delete_user(self, user_id: int) -> None:
|
||||||
await self.database.execute("DELETE FROM auth_users WHERE id = $1", user_id)
|
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:
|
async def set_totp_secret(self, user_id: int, secret: str) -> None:
|
||||||
await self.database.execute(
|
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,
|
secret,
|
||||||
user_id,
|
user_id,
|
||||||
)
|
)
|
||||||
@@ -298,7 +309,8 @@ class AuthStore:
|
|||||||
|
|
||||||
async def disable_totp(self, user_id: int) -> None:
|
async def disable_totp(self, user_id: int) -> None:
|
||||||
await self.database.execute(
|
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,
|
user_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from shared_backend.auth import ResetManager, hash_password, verify_password
|
from shared_backend.auth import ResetManager, hash_password, verify_password
|
||||||
from shared_backend.config import DatabaseConfig
|
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.tokens import hash_token, new_session_token
|
||||||
|
from shared_backend.totp import totp_provisioning_uri, verify_totp
|
||||||
|
|
||||||
|
|
||||||
class SharedBackendTests(unittest.TestCase):
|
class SharedBackendTests(unittest.TestCase):
|
||||||
|
|||||||
Reference in new issue
Block a user