From 7eba9cc14dc215333d96a4b8067bba4019918703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B8=D1=80=D0=B5=D0=BD=D0=BA=D0=BE=20=D0=98=D0=BB?= =?UTF-8?q?=D1=8C=D1=8F?= Date: Mon, 16 Nov 2026 21:47:32 +0000 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20PostgreSQL-=D1=81=D0=BB=D0=BE=D0=B9:?= =?UTF-8?q?=20=D1=81=D0=BE=D0=B5=D0=B4=D0=B8=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?,=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=B9,=20keyset-=D0=BF=D0=B0=D0=B3=D0=B8=D0=BD=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- messenger/config.py | 28 +++++++++ messenger/database.py | 20 ++++++ messenger/domain.py | 33 ++++++++++ messenger/pagination.py | 43 +++++++++++++ messenger/postgres.py | 133 ++++++++++++++++++++++++++++++++++++++++ messenger/repository.py | 17 +++++ 6 files changed, 274 insertions(+) create mode 100644 messenger/config.py create mode 100644 messenger/database.py create mode 100644 messenger/domain.py create mode 100644 messenger/pagination.py create mode 100644 messenger/postgres.py create mode 100644 messenger/repository.py diff --git a/messenger/config.py b/messenger/config.py new file mode 100644 index 00000000..177aacb4 --- /dev/null +++ b/messenger/config.py @@ -0,0 +1,28 @@ +"""Configuration is loaded only at process startup; secrets are never logged.""" +import os +from dataclasses import dataclass +from pathlib import Path + + +@dataclass(frozen=True) +class Settings: + database_url: str + host: str = '0.0.0.0' + port: int = 8080 + environment: str = 'lab' + session_ttl: int = 3600 + + @classmethod + def from_env(cls): + source = os.getenv('DATABASE_URL_FILE') + url = Path(source).read_text().strip() if source else os.getenv('DATABASE_URL', '') + if not url: + raise ValueError('Set DATABASE_URL_FILE or DATABASE_URL') + if os.getenv('SESSION_STORE', 'postgres') != 'postgres': + raise ValueError('This lab uses SESSION_STORE=postgres') + port = int(os.getenv('APP_PORT', '8080')) + ttl = int(os.getenv('SESSION_TTL_SECONDS', '3600')) + if not 1 <= port <= 65535 or ttl <= 0: + raise ValueError('Invalid APP_PORT or SESSION_TTL_SECONDS') + return cls(url, os.getenv('APP_HOST', '0.0.0.0'), port, + os.getenv('APP_ENV', 'lab'), ttl) diff --git a/messenger/database.py b/messenger/database.py new file mode 100644 index 00000000..740dbeff --- /dev/null +++ b/messenger/database.py @@ -0,0 +1,20 @@ +"""Short-lived database connections, one transaction per operation.""" +from contextlib import contextmanager +import psycopg +from psycopg.rows import dict_row +from .domain import ApiError + + +class Database: + def __init__(self, url): + self.url = url + + @contextmanager + def transaction(self): + try: + with psycopg.connect(self.url, row_factory=dict_row, connect_timeout=3, + options='-c statement_timeout=5000 -c lock_timeout=3000 -c timezone=UTC') as conn: + yield conn + except (psycopg.OperationalError, psycopg.InterfaceError, + psycopg.errors.QueryCanceled, psycopg.errors.LockNotAvailable): + raise ApiError(503, 'unavailable', 'Database temporarily unavailable') from None diff --git a/messenger/domain.py b/messenger/domain.py new file mode 100644 index 00000000..a67f0bea --- /dev/null +++ b/messenger/domain.py @@ -0,0 +1,33 @@ +"""Framework-independent validation and application errors.""" +import re +from uuid import UUID + + +class ApiError(Exception): + def __init__(self, status, code, message): + self.status, self.code, self.message = status, code, message + + +def invalid(message='Invalid request'): + raise ApiError(400, 'invalid_request', message) + + +def uuid_value(value): + if not isinstance(value, str) or not re.fullmatch( + r'[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}', value + ): + invalid('Invalid UUID') + return str(UUID(value)) + + +def fields(body, names): + if not isinstance(body, dict) or set(body) != set(names): + invalid('Required fields are missing or unknown fields supplied') + + +def string(value, maximum): + if (not isinstance(value, str) or not value.strip() + or len(value) > maximum or "\x00" in value + or any(0xD800 <= ord(c) <= 0xDFFF for c in value)): + invalid('Invalid string') + return value diff --git a/messenger/pagination.py b/messenger/pagination.py new file mode 100644 index 00000000..1744d190 --- /dev/null +++ b/messenger/pagination.py @@ -0,0 +1,43 @@ +"""Resource-scoped signed cursors; SQL fetches only limit+1 rows.""" +import base64 +import hashlib +import hmac +import json +import re +from datetime import datetime +from .domain import invalid, uuid_value + + +class Pagination: + def __init__(self, key): + self._key = key + + def decode(self, scope, limit, cursor): + if not re.fullmatch(r'[0-9]{1,3}', limit) or not 1 <= int(limit) <= 100: + invalid('Limit must be between 1 and 100') + after = None + if cursor is not None: + try: + if len(cursor) > 2048: + raise ValueError() + raw = base64.b64decode(cursor, altchars=b'-_', validate=True) + payload, signature = raw[:-32], raw[-32:] + if not hmac.compare_digest(signature, hmac.digest(self._key, payload, hashlib.sha256)): + raise ValueError() + saved_scope, timestamp, identifier = json.loads(payload) + if saved_scope != scope or not isinstance(timestamp, str): + raise ValueError() + datetime.fromisoformat(timestamp.replace('Z', '+00:00')) + after = (timestamp, uuid_value(identifier)) + except (ValueError, TypeError, UnicodeError): + invalid('Invalid cursor for this resource') + return int(limit), after + + def page(self, rows, scope, limit): + items = rows[:limit] + next_cursor = None + if len(rows) > limit: + last = items[-1] + payload = json.dumps([scope, last['created_at'], last['id']], separators=(',', ':')).encode() + next_cursor = base64.urlsafe_b64encode(payload + hmac.digest(self._key, payload, hashlib.sha256)).decode() + return {'items': items, 'next_cursor': next_cursor} diff --git a/messenger/postgres.py b/messenger/postgres.py new file mode 100644 index 00000000..8175c042 --- /dev/null +++ b/messenger/postgres.py @@ -0,0 +1,133 @@ +"""PostgreSQL repository: all SQL values are bound parameters.""" +from datetime import datetime, timezone +from uuid import UUID, uuid4 +import psycopg +from .domain import ApiError, invalid + + +def serialize(row): + if row is None: + return None + result = {} + for key, value in row.items(): + if isinstance(value, UUID): + value = str(value) + elif isinstance(value, datetime): + value = value.astimezone(timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z') + elif isinstance(value, list): + value = [str(item) if isinstance(item, UUID) else item for item in value] + result[key] = value + return result + + +USER_COLUMNS = 'id, username, display_name, created_at' +# Members retain the request order, with no password/session fields in public objects. +CHAT_COLUMNS = '''c.id, c.title, c.created_at, + ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id=c.id ORDER BY m.position) AS member_ids''' + + +class PostgresRepository: + def __init__(self, database): + self.db = database + + def ready(self): + with self.db.transaction() as conn: + conn.execute('SELECT id FROM users LIMIT 0') + conn.execute('SELECT token_hash FROM sessions LIMIT 0') + + def cursor_key(self): + with self.db.transaction() as conn: + row = conn.execute("SELECT value FROM app_settings WHERE name='cursor_key'").fetchone() + if row is None: + raise ApiError(503, 'unavailable', 'Database is not initialized') + return bytes.fromhex(row['value']) + + def create_user(self, username, display_name, password_hash): + try: + with self.db.transaction() as conn: + row = conn.execute('INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s) RETURNING ' + USER_COLUMNS, + (uuid4(), username, display_name, password_hash)).fetchone() + return serialize(row) + except psycopg.errors.UniqueViolation: + raise ApiError(409, 'conflict', 'Username already exists') from None + + def credentials(self, username): + with self.db.transaction() as conn: + return serialize(conn.execute('SELECT ' + USER_COLUMNS + ', password_hash FROM users WHERE username=%s', + (username,)).fetchone()) + + def create_session(self, user_id, token_hash, ttl): + with self.db.transaction() as conn: + row = conn.execute('INSERT INTO sessions(token_hash,user_id,expires_at) VALUES (%s,%s,clock_timestamp() + %s * interval \'1 second\') RETURNING expires_at', + (token_hash, user_id, ttl)).fetchone() + return serialize(row)['expires_at'] + + def session_user(self, token_hash): + with self.db.transaction() as conn: + row = conn.execute('''SELECT u.id,u.username,u.display_name,u.created_at FROM users u + JOIN sessions s ON s.user_id=u.id + WHERE s.token_hash=%s AND s.expires_at>clock_timestamp()''', (token_hash,)).fetchone() + if row is None: + raise ApiError(401, 'unauthorized', 'Valid Bearer session required') + return serialize(row) + + def revoke_session(self, token_hash): + with self.db.transaction() as conn: + conn.execute('DELETE FROM sessions WHERE token_hash=%s', (token_hash,)) + + @staticmethod + def _access(conn, actor, chat_id): + if conn.execute('SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s', (chat_id, actor)).fetchone() is None: + raise ApiError(404, 'not_found', 'Chat not found') + + def create_chat(self, actor, title, members): + with self.db.transaction() as conn: + count = conn.execute('SELECT count(*) AS n FROM users WHERE id=ANY(%s::uuid[])', (members,)).fetchone()['n'] + if actor not in members or count != len(members): + invalid('Members must exist and include the current user') + identifier = uuid4() + conn.execute('INSERT INTO chats(id,title) VALUES (%s,%s)', (identifier, title)) + with conn.cursor() as cur: + cur.executemany('INSERT INTO chat_members(chat_id,user_id,position) VALUES (%s,%s,%s)', + [(identifier, member, i) for i, member in enumerate(members)]) + row = conn.execute('SELECT ' + CHAT_COLUMNS + ' FROM chats c WHERE c.id=%s', (identifier,)).fetchone() + return serialize(row) + + def chat(self, actor, chat_id): + with self.db.transaction() as conn: + self._access(conn, actor, chat_id) + row = conn.execute('SELECT ' + CHAT_COLUMNS + ' FROM chats c WHERE c.id=%s', (chat_id,)).fetchone() + return serialize(row) + + def chats(self, actor, limit, after): + params = [actor] + boundary = '' + if after is not None: + boundary = ' AND (c.created_at,c.id)>(%s::timestamptz,%s::uuid)' + params.extend(after) + params.append(limit) + with self.db.transaction() as conn: + rows = conn.execute('SELECT ' + CHAT_COLUMNS + ''' FROM chats c JOIN chat_members own + ON own.chat_id=c.id WHERE own.user_id=%s''' + boundary + + ' ORDER BY c.created_at,c.id LIMIT %s', params).fetchall() + return [serialize(r) for r in rows] + + def create_message(self, actor, chat_id, text): + with self.db.transaction() as conn: + self._access(conn, actor, chat_id) + row = conn.execute('INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) RETURNING *', + (uuid4(), chat_id, actor, text)).fetchone() + return serialize(row) + + def messages(self, actor, chat_id, limit, after): + params = [chat_id] + boundary = '' + if after is not None: + boundary = ' AND (created_at,id)>(%s::timestamptz,%s::uuid)' + params.extend(after) + params.append(limit) + with self.db.transaction() as conn: + self._access(conn, actor, chat_id) + rows = conn.execute('SELECT * FROM messages WHERE chat_id=%s' + boundary + + ' ORDER BY created_at,id LIMIT %s', params).fetchall() + return [serialize(r) for r in rows] diff --git a/messenger/repository.py b/messenger/repository.py new file mode 100644 index 00000000..971de0e6 --- /dev/null +++ b/messenger/repository.py @@ -0,0 +1,17 @@ +"""Storage port. Concrete PostgreSQL adapter is wired in api.create_app.""" +from typing import Protocol + + +class Repository(Protocol): + def ready(self): ... + def cursor_key(self): ... + def create_user(self, username, display_name, password_hash): ... + def credentials(self, username): ... + def create_session(self, user_id, token_hash, ttl): ... + def session_user(self, token_hash): ... + def revoke_session(self, token_hash): ... + def create_chat(self, actor, title, members): ... + def chat(self, actor, chat_id): ... + def chats(self, actor, limit, after): ... + def create_message(self, actor, chat_id, text): ... + def messages(self, actor, chat_id, limit, after): ...