Перенести приложение мессенджера с сессиями и идемпотентностью в PostgreSQL

Пользователи, аутентификация Argon2id, Bearer-сессии с TTL и отзывом,
транзакционный репозиторий, keyset-пагинация, идемпотентные отправки
(advisory lock + message_requests), версияемые миграции 001/002.
This commit is contained in:
2026-11-17 03:37:07 +00:00
parent ec540a1a72
commit 4b23b44eb8
15 changed files with 795 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
"""Messenger lab series."""
+41
View File
@@ -0,0 +1,41 @@
"""One process per replica; bounded drain on SIGTERM."""
import logging
import signal
import time
from waitress import create_server
from .api import create_app
from .config import Settings
def main():
logging.basicConfig(level=logging.INFO)
settings = Settings.from_env()
app = create_app(settings=settings)
server = create_server(app, host=settings.host, port=settings.port, threads=4)
stopping = False
def stop(signum, frame):
nonlocal stopping
stopping = True
signal.signal(signal.SIGTERM, stop)
signal.signal(signal.SIGINT, stop)
logging.info('Starting messenger environment=%s', settings.environment)
try:
while not stopping:
server.asyncore.loop(timeout=0.1, count=1, map=server._map)
# Stop accepting new TCP connections. Keep the event loop alive briefly
# so responses from existing worker tasks can still be flushed.
app.config['DRAINING'] = True
server.close()
deadline = time.monotonic() + 5
while server._map and time.monotonic() < deadline:
server.asyncore.loop(timeout=0.1, count=1, map=server._map)
finally:
server.task_dispatcher.shutdown(timeout=1)
server.asyncore.close_all(map=server._map)
logging.info('Messenger stopped')
if __name__ == '__main__':
main()
+190
View File
@@ -0,0 +1,190 @@
"""HTTP adapter and composition root."""
import json
import hashlib
import re
import unicodedata
from uuid import UUID
from .images import MAX_BYTES, validate_image
from .storage import ObjectStore
from uuid import uuid4
from flask import Flask, g, request, jsonify, Response, stream_with_context
from werkzeug.exceptions import HTTPException
from .domain import ApiError, invalid, uuid_value
from .postgres import PostgresRepository
from .database import Database
from .config import Settings
from .service import Messenger
def create_app(repository=None, settings=None):
app = Flask(__name__)
app.json.ensure_ascii = False
settings = settings or Settings.from_env()
repository = repository if repository is not None else PostgresRepository(Database(settings.database_url))
service = Messenger(repository, settings.session_ttl, settings.idempotency_ttl)
instance_id = str(uuid4())
store = None if app.config.get("TESTING_STORAGE_DISABLED") else (ObjectStore(settings) if repository is None or isinstance(repository, PostgresRepository) else None)
@app.before_request
def identify():
g.request_id = str(uuid4())
if app.config.get('DRAINING'):
raise ApiError(503, 'unavailable', 'Instance is shutting down')
if request.path.startswith('/api/v1/') and not (
request.path in ('/api/v1/users', '/api/v1/auth/sessions') and request.method == 'POST'
):
g.user, g.token_hash = service.auth.bearer(request.headers.get('Authorization'))
@app.after_request
def request_id(response):
response.headers['X-Request-Id'] = g.request_id
response.headers['X-Instance-Id'] = instance_id
return response
@app.errorhandler(Exception)
def error(exc):
if isinstance(exc, ApiError):
status, code, message = exc.status, exc.code, exc.message
elif isinstance(exc, HTTPException):
status = exc.code
code = {404: 'not_found', 413: 'payload_too_large', 415: 'unsupported_media_type'}.get(status, 'invalid_request')
message = exc.name
else:
app.logger.error('Unhandled error; request_id=%s', g.request_id)
status, code, message = 500, 'internal_error', 'Internal server error'
response = jsonify(error={'code': code, 'message': message, 'request_id': g.request_id})
response.status_code = status
if status == 401:
response.headers['WWW-Authenticate'] = (
'Basic realm="messenger", charset="UTF-8"'
if request.path == '/api/v1/auth/sessions' and request.method == 'POST'
else 'Bearer realm="messenger"')
return response
def body():
if request.mimetype != 'application/json':
raise ApiError(415, 'unsupported_media_type', 'Use application/json')
try:
def reject_constant(value):
raise ValueError(value)
return json.loads(request.get_data().decode('utf-8'), parse_constant=reject_constant)
except (ValueError, UnicodeError):
invalid('Malformed UTF-8 JSON')
def paging():
for key in ('limit', 'cursor'):
if len(request.args.getlist(key)) > 1:
invalid('Duplicate query parameter')
return request.args.get('limit', '50'), request.args.get('cursor')
@app.get('/health/live')
def health():
return jsonify(status='ok')
@app.get('/health/ready')
def ready():
repository.ready()
if store is not None:
store.ready()
return jsonify(status='ok')
@app.post('/api/v1/auth/sessions')
def login():
# Session creation accepts no request body.
if request.get_data():
invalid('Session creation does not accept a body')
return jsonify(service.auth.login(request.headers.get('Authorization'))), 201
@app.delete('/api/v1/auth/sessions/current')
def logout():
repository.revoke_session(g.token_hash)
return '', 204
@app.post('/api/v1/users')
def register():
return jsonify(service.register(body())), 201
@app.get('/api/v1/users/me')
def me():
return jsonify(g.user)
@app.route('/api/v1/chats', methods=['GET', 'POST'])
def chats():
if request.method == 'POST':
return jsonify(service.create_chat(g.user['id'], body())), 201
return jsonify(service.chats(g.user['id'], *paging()))
@app.get('/api/v1/chats/<chat_id>')
def chat(chat_id):
return jsonify(service.chat(g.user['id'], chat_id))
@app.route('/api/v1/chats/<chat_id>/messages', methods=['GET', 'POST'])
def messages(chat_id):
if request.method == 'POST':
service.chat(g.user['id'], chat_id)
return jsonify(service.send(g.user['id'], chat_id, body(), request.headers.get('Idempotency-Key'))), 201
return jsonify(service.history(g.user['id'], chat_id, *paging()))
@app.post('/api/v1/chats/<chat_id>/attachments')
def upload(chat_id):
chat_id = uuid_value(chat_id)
service.chat(g.user['id'], chat_id)
values = request.args.getlist('filename')
if len(values) != 1:
invalid('Exactly one filename is required')
filename = values[0]
if (not 1 <= len(filename) <= 255 or any(
c in ('/', '\\') or unicodedata.category(c) == 'Cc' for c in filename)):
invalid('Unsafe filename')
if request.mimetype not in ('image/png', 'image/jpeg'):
raise ApiError(415, 'unsupported_media_type', 'PNG or JPEG required')
if request.content_length is not None and request.content_length > MAX_BYTES:
raise ApiError(413, 'payload_too_large', 'Image exceeds 10 MiB')
# Bounded read even if Content-Length is absent or misleading.
content = request.stream.read(MAX_BYTES + 1)
if len(content) > MAX_BYTES:
raise ApiError(413, 'payload_too_large', 'Image exceeds 10 MiB')
validate_image(content, request.mimetype)
if store is None:
raise ApiError(503, 'unavailable', 'Object storage unavailable')
identifier = str(uuid4())
key = 'originals/' + uuid4().hex + '/' + uuid4().hex
metadata = {'id': identifier, 'filename': filename, 'content_type': request.mimetype,
'size_bytes': len(content), 'sha256': hashlib.sha256(content).hexdigest(),
'object_key': key}
store.put(key, content, request.mimetype)
try:
result = repository.insert_attachment(g.user['id'], chat_id, metadata)
except Exception:
store.delete(key) # best effort compensation for DB commit failure
raise
return jsonify(result), 201
@app.get('/api/v1/attachments/<attachment_id>')
def attachment(attachment_id):
obj, _key = repository.attachment(g.user['id'], uuid_value(attachment_id))
return jsonify(obj)
@app.get('/api/v1/attachments/<attachment_id>/content')
def download(attachment_id):
obj, key = repository.attachment(g.user['id'], uuid_value(attachment_id))
variants = request.args.getlist('variant')
if len(variants) > 1:
invalid('Duplicate variant')
if variants and variants[0] != 'original':
raise ApiError(404, 'not_found', 'Variant not found')
if store is None:
raise ApiError(503, 'unavailable', 'Object storage unavailable')
body = store.get(key)
def chunks():
try:
for chunk in body.iter_chunks(chunk_size=65536):
if chunk:
yield chunk
finally:
body.close()
return Response(stream_with_context(chunks()), mimetype=obj['content_type'],
headers={'Content-Length': str(obj['size_bytes']),
'Cache-Control': 'private, no-store'})
return app
+40
View File
@@ -0,0 +1,40 @@
"""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
idempotency_ttl: int = 86400
s3_endpoint: str = "http://s3:9000"
s3_bucket: str = "messenger-attachments"
s3_access_key: str = "messengeradmin"
s3_secret_file: str = "/run/secrets/s3_password"
@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'))
idempotency_ttl = int(os.getenv('IDEMPOTENCY_TTL_SECONDS', '86400'))
if idempotency_ttl < 86400:
raise ValueError('IDEMPOTENCY_TTL_SECONDS must be at least 86400')
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, idempotency_ttl,
os.getenv("S3_ENDPOINT", "http://s3:9000"),
os.getenv("S3_BUCKET", "messenger-attachments"),
os.getenv("S3_ACCESS_KEY", "messengeradmin"),
os.getenv("S3_SECRET_FILE", "/run/secrets/s3_password"))
+20
View File
@@ -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
+33
View File
@@ -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
+24
View File
@@ -0,0 +1,24 @@
"""Versioned, transactional migrations; safe to run again."""
from pathlib import Path
import secrets
from .config import Settings
from .database import Database
def migrate(database):
directory = Path(__file__).resolve().parents[1] / 'migrations'
with database.transaction() as conn:
conn.execute('SELECT pg_advisory_xact_lock(20260201)')
conn.execute('CREATE TABLE IF NOT EXISTS schema_migrations (version text PRIMARY KEY)')
versions = {r['version'] for r in conn.execute('SELECT version FROM schema_migrations')}
for path in sorted(directory.glob('*.sql')):
if path.name not in versions:
conn.execute(path.read_text(encoding='utf-8'))
conn.execute('INSERT INTO schema_migrations(version) VALUES (%s)', (path.name,))
conn.execute("INSERT INTO app_settings(name,value) VALUES ('cursor_key',%s) ON CONFLICT DO NOTHING",
(secrets.token_hex(32),))
if __name__ == '__main__':
migrate(Database(Settings.from_env().database_url))
print('Migrations applied successfully (already applied versions skipped).')
+43
View File
@@ -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}
+196
View File
@@ -0,0 +1,196 @@
"""PostgreSQL repository: all SQL values are bound parameters."""
from datetime import datetime, timezone
import hashlib
import json
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]
@staticmethod
def _message(conn, message_id):
row = conn.execute('SELECT * FROM messages WHERE id=%s', (message_id,)).fetchone()
result = serialize(row)
result['attachment_ids'] = [str(r['attachment_id']) for r in conn.execute(
'SELECT attachment_id FROM message_attachments WHERE message_id=%s ORDER BY position',
(message_id,)).fetchall()]
return result
def create_message(self, actor, chat_id, text, attachments, key, ttl):
scope = json.dumps([actor, chat_id, key], separators=(',', ':')).encode()
lock_id = int.from_bytes(hashlib.sha256(scope).digest()[:8], 'big', signed=True)
with self.db.transaction() as conn:
self._access(conn, actor, chat_id)
conn.execute('SELECT pg_advisory_xact_lock(%s)', (lock_id,))
previous = conn.execute("""SELECT request_text, request_attachments, message_id
FROM message_requests WHERE user_id=%s AND chat_id=%s AND idempotency_key=%s
AND expires_at>clock_timestamp()""", (actor, chat_id, key)).fetchone()
if previous:
if previous['request_text'] != text or previous['request_attachments'] != attachments:
raise ApiError(409, 'conflict', 'Idempotency key already used with another message')
return self._message(conn, previous['message_id'])
conn.execute('DELETE FROM message_requests WHERE user_id=%s AND chat_id=%s AND idempotency_key=%s',
(actor, chat_id, key))
if attachments:
found = conn.execute('SELECT id FROM attachments WHERE chat_id=%s AND id=ANY(%s::uuid[])',
(chat_id, attachments)).fetchall()
if len(found) != len(attachments):
raise ApiError(404, 'not_found', 'Attachment not found')
message_id = uuid4()
conn.execute('INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s)',
(message_id, chat_id, actor, text))
for i, aid in enumerate(attachments):
conn.execute('INSERT INTO message_attachments(message_id,attachment_id,position) VALUES (%s,%s,%s)',
(message_id, aid, i))
conn.execute("""INSERT INTO message_requests
(user_id,chat_id,idempotency_key,request_text,request_attachments,message_id,expires_at)
VALUES (%s,%s,%s,%s,%s::jsonb,%s,clock_timestamp() + %s * interval '1 second')""",
(actor, chat_id, key, text, json.dumps(attachments), message_id, ttl))
return self._message(conn, message_id)
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 id FROM messages WHERE chat_id=%s' + boundary +
' ORDER BY created_at,id LIMIT %s', params).fetchall()
return [self._message(conn, row['id']) for row in rows]
@staticmethod
def _attachment(row):
obj = serialize(row)
obj.pop('object_key', None)
obj['status'] = 'ready'
obj['variants'] = []
obj['error_code'] = None
return obj
def insert_attachment(self, actor, chat_id, metadata):
with self.db.transaction() as conn:
self._access(conn, actor, chat_id)
row = conn.execute("""INSERT INTO attachments
(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s) RETURNING *""",
(metadata['id'], chat_id, actor, metadata['filename'], metadata['content_type'],
metadata['size_bytes'], metadata['sha256'], metadata['object_key'])).fetchone()
return self._attachment(row)
def attachment(self, actor, attachment_id):
with self.db.transaction() as conn:
row = conn.execute("""SELECT a.* FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id
WHERE a.id=%s AND m.user_id=%s""", (attachment_id, actor)).fetchone()
if row is None:
raise ApiError(404, 'not_found', 'Attachment not found')
return self._attachment(row), row['object_key']
+17
View File
@@ -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, key, ttl): ...
def messages(self, actor, chat_id, limit, after): ...
+60
View File
@@ -0,0 +1,60 @@
"""Password KDF and HTTP credential parsing, without framework dependencies."""
import base64
import binascii
import hashlib
import re
import secrets
from argon2 import PasswordHasher, Type
from argon2.exceptions import VerificationError, InvalidHashError
from .domain import ApiError, invalid, string
class Authentication:
def __init__(self, repository, ttl):
self.repository = repository
self.ttl = ttl
self.hasher = PasswordHasher(time_cost=3, memory_cost=65536, parallelism=2,
hash_len=32, salt_len=16, type=Type.ID)
self._dummy = self.hasher.hash(secrets.token_urlsafe(32))
@staticmethod
def password(value):
string(value, 128)
if len(value) < 12:
invalid('Password must contain 12 to 128 Unicode characters')
return value
@staticmethod
def digest(token):
return hashlib.sha256(token.encode('ascii')).hexdigest()
def bearer(self, header):
parts = (header or '').split()
if len(parts) != 2 or parts[0].lower() != 'bearer' or not re.fullmatch(r'[A-Za-z0-9_-]{43}', parts[1]):
raise ApiError(401, 'unauthorized', 'Valid Bearer session required')
token_hash = self.digest(parts[1])
return self.repository.session_user(token_hash), token_hash
def login(self, header):
def denied():
raise ApiError(401, 'unauthorized', 'Invalid credentials')
try:
parts = (header or '').split()
if len(parts) != 2 or parts[0].lower() != 'basic':
denied()
username, password = base64.b64decode(parts[1], validate=True).decode('utf-8').split(':', 1)
if not re.fullmatch(r'[a-z0-9_]{3,32}', username) or not 12 <= len(password) <= 128:
denied()
except (ValueError, UnicodeError, binascii.Error):
denied()
row = self.repository.credentials(username)
try:
self.hasher.verify(row['password_hash'] if row else self._dummy, password)
except (VerificationError, InvalidHashError):
denied()
if row is None:
denied()
user = {key: value for key, value in row.items() if key != 'password_hash'}
token = secrets.token_urlsafe(32)
expires_at = self.repository.create_session(user['id'], self.digest(token), self.ttl)
return {'token': token, 'token_type': 'Bearer', 'expires_at': expires_at, 'user': user}
+68
View File
@@ -0,0 +1,68 @@
"""Use cases; independent of Flask and the concrete storage adapter."""
import re
from .domain import fields, string, uuid_value, invalid, ApiError
from .repository import Repository
from .pagination import Pagination
from .security import Authentication
class Messenger:
def __init__(self, repository: Repository, session_ttl=3600, idempotency_ttl=86400):
self.repository = repository
self.idempotency_ttl = idempotency_ttl
self.pagination = Pagination(repository.cursor_key())
self.auth = Authentication(repository, session_ttl)
def register(self, body):
fields(body, ('username', 'display_name', 'password'))
username = string(body['username'], 32)
if not re.fullmatch(r'[a-z0-9_]{3,32}', username):
invalid('Invalid username')
display_name = string(body['display_name'], 100)
password = self.auth.password(body['password'])
return self.repository.create_user(username, display_name, self.auth.hasher.hash(password))
def create_chat(self, actor, body):
fields(body, ('title', 'member_ids'))
title = string(body['title'], 100)
members = body['member_ids']
if not isinstance(members, list) or not 1 <= len(members) <= 100:
invalid('Invalid member list')
members = [uuid_value(m) for m in members]
if len(set(members)) != len(members):
invalid('Duplicate members')
return self.repository.create_chat(actor, title, members)
def chat(self, actor, chat_id):
return self.repository.chat(actor, uuid_value(chat_id))
def send(self, actor, chat_id, body, key):
self.chat(actor, chat_id)
if not isinstance(key, str) or not re.fullmatch(r'[A-Za-z0-9._:-]{1,128}', key):
invalid('Valid Idempotency-Key required')
if not isinstance(body, dict) or set(body) not in ({'text'}, {'text', 'attachment_ids'}):
invalid('Invalid message fields')
if not isinstance(body['text'], str) or len(body['text']) > 4000 or '\x00' in body['text']:
invalid('Invalid message text')
attachments = body.get('attachment_ids', [])
if not isinstance(attachments, list) or len(attachments) > 10:
invalid('Invalid attachment IDs')
attachments = [uuid_value(item) for item in attachments]
if len(attachments) != len(set(attachments)):
invalid('Duplicate attachment IDs')
if not body['text'].strip() and not attachments:
invalid('Message needs text or attachments')
return self.repository.create_message(actor, uuid_value(chat_id), body['text'],
attachments, key, self.idempotency_ttl)
def chats(self, actor, limit, cursor):
scope = 'chats:' + actor
limit, after = self.pagination.decode(scope, limit, cursor)
return self.pagination.page(self.repository.chats(actor, limit + 1, after), scope, limit)
def history(self, actor, chat_id, limit, cursor):
chat_id = uuid_value(chat_id)
self.repository.chat(actor, chat_id)
scope = 'messages:' + chat_id
limit, after = self.pagination.decode(scope, limit, cursor)
return self.pagination.page(self.repository.messages(actor, chat_id, limit + 1, after), scope, limit)
+37
View File
@@ -0,0 +1,37 @@
CREATE TABLE users (
id uuid PRIMARY KEY,
username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE INDEX chats_order_idx ON chats(created_at, id);
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
user_id uuid NOT NULL REFERENCES users(id),
position integer NOT NULL CHECK (position >= 0),
PRIMARY KEY(chat_id, user_id),
UNIQUE(chat_id, position)
);
CREATE INDEX chat_members_user_idx ON chat_members(user_id, chat_id);
CREATE TABLE messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
sender_id uuid NOT NULL REFERENCES users(id),
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY(chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
);
CREATE INDEX messages_history_idx ON messages(chat_id, created_at, id);
CREATE TABLE sessions (
token_hash text PRIMARY KEY CHECK (char_length(token_hash) = 64),
user_id uuid NOT NULL REFERENCES users(id),
expires_at timestamptz NOT NULL
);
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
CREATE TABLE app_settings (name text PRIMARY KEY, value text NOT NULL);
+10
View File
@@ -0,0 +1,10 @@
CREATE TABLE message_requests (
user_id uuid NOT NULL REFERENCES users(id),
chat_id uuid NOT NULL REFERENCES chats(id),
idempotency_key text NOT NULL CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'),
request_text text NOT NULL,
message_id uuid NOT NULL REFERENCES messages(id),
expires_at timestamptz NOT NULL,
PRIMARY KEY (user_id, chat_id, idempotency_key)
);
CREATE INDEX message_requests_expiry_idx ON message_requests(expires_at);
+15
View File
@@ -0,0 +1,15 @@
Flask==3.1.0
waitress==3.0.2
Werkzeug==3.1.3
Jinja2==3.1.6
MarkupSafe==3.0.3
itsdangerous==2.2.0
click==8.1.8
blinker==1.9.0
psycopg[binary]==3.2.6
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
cffi==1.17.1
pycparser==2.22
boto3==1.36.26
Pillow==11.1.0