Files
mtusi2026lab2/src/messenger/core.py

302 lines
13 KiB
Python

"""Validation and persistent state. HTTP 201 is emitted after transaction exit/COMMIT."""
import base64
from dataclasses import dataclass, fields
from datetime import datetime, timezone
import hashlib
import hmac
import json
import os
from pathlib import Path
import re
import secrets
import uuid
import psycopg
from psycopg.rows import dict_row
from psycopg_pool import ConnectionPool
import redis
from redis.backoff import NoBackoff
from redis.retry import Retry
class APIError(Exception):
def __init__(self, status, message):
self.status, self.message = status, message
@dataclass
class Config:
app_host: str = '0.0.0.0'
app_port: int = 8080
workers: int = 16
request_queue: int = 32
socket_timeout: int = 2
max_body_bytes: int = 65536
session_ttl_seconds: int = 3600
@classmethod
def from_env(cls):
values = {}
for field in fields(cls):
raw = os.environ.get(field.name.upper(), field.default)
values[field.name] = str(raw) if field.type is str else int(raw)
if field.type is int and values[field.name] < 1:
raise ValueError(field.name + ' must be positive')
result = cls(**values)
if result.app_port > 65535 or result.workers > 32 or result.request_queue > 128:
raise ValueError('port/workers/queue outside bounds')
if result.max_body_bytes > 1048576 or result.socket_timeout > 5:
raise ValueError('body <= 1 MiB and socket timeout <= 5 required')
if os.getenv('SESSION_STORE', 'redis') != 'redis':
raise ValueError('This solution uses Redis sessions')
return result
def secret_file(name):
return Path(os.environ[name]).read_text(encoding='utf-8').strip()
def db_connect_kwargs():
return dict(connect_timeout=2, options='-c statement_timeout=2000 -c lock_timeout=1000 '
'-c idle_in_transaction_session_timeout=5000', row_factory=dict_row)
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):
raise APIError(400, 'Invalid UUID')
return str(uuid.UUID(value))
def string(value, maximum):
if not isinstance(value, str) or not value.strip() or len(value) > maximum or '\x00' in value:
raise APIError(400, 'Invalid string')
try:
value.encode('utf-8')
except UnicodeError:
raise APIError(400, 'Invalid Unicode') from None
return value
def object_fields(body, expected):
if not isinstance(body, dict) or set(body) != set(expected):
raise APIError(400, 'Invalid object fields')
def password_value(value):
if not isinstance(value, str) or not value.strip() or not 12 <= len(value) <= 128:
raise APIError(400, 'Invalid password length')
try:
value.encode('utf-8')
except UnicodeError:
raise APIError(400, 'Invalid Unicode') from None
return value
def password_hash(value, salt=None):
salt = salt or secrets.token_bytes(16)
digest = hashlib.scrypt(value.encode('utf-8'), salt=salt, n=16384, r=8, p=1, dklen=32)
return 'scrypt$16384$8$1$' + salt.hex() + '$' + digest.hex()
def password_matches(value, encoded):
# Only this versioned format is generated by registration.
salt = bytes.fromhex(encoded.split('$')[4])
return hmac.compare_digest(password_hash(value, salt), encoded)
def stamp(value):
return value.astimezone(timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z')
def public_row(row):
return {key: (str(value) if isinstance(value, uuid.UUID) else stamp(value)
if isinstance(value, datetime) else value)
for key, value in row.items() if key != 'password_hash'}
USER_COLUMNS = 'id, username, display_name, created_at'
MESSAGE_COLUMNS = 'id, chat_id, sender_id, text, created_at'
# Shared by the real history endpoint and EXPLAIN evidence; all values are parameters.
HISTORY_SQL = ('SELECT ' + MESSAGE_COLUMNS + ' FROM messages WHERE chat_id = %s '
'AND (created_at, id) > (%s, %s) ORDER BY created_at, id LIMIT %s')
class Store:
def __init__(self, config):
self.config = config
self.secret = bytes.fromhex(secret_file('CURSOR_KEY_FILE'))
if len(self.secret) < 32:
raise ValueError('Cursor key must be at least 256 bits')
self.pool = ConnectionPool(secret_file('DATABASE_URL_FILE'), min_size=1, max_size=8,
timeout=1, max_waiting=16, kwargs=db_connect_kwargs(),
check=ConnectionPool.check_connection, open=True)
self.cache_pool = redis.ConnectionPool.from_url(
secret_file('CACHE_URL_FILE'), max_connections=8, socket_connect_timeout=.5,
socket_timeout=.5, decode_responses=True, retry=Retry(NoBackoff(), 0))
self.cache = redis.Redis(connection_pool=self.cache_pool)
self.dummy_hash = password_hash('dummy-password-for-timing')
def close(self):
self.pool.close(timeout=2)
self.cache_pool.disconnect()
def ready(self):
with self.pool.connection() as conn:
conn.execute('SELECT 1')
self.cache.ping()
def create_user(self, body):
object_fields(body, ('username', 'display_name', 'password'))
username = string(body['username'], 32)
if not re.fullmatch(r'[a-z0-9_]{3,32}', username):
raise APIError(400, 'Invalid username')
display = string(body['display_name'], 100)
hashed = password_hash(password_value(body['password']))
try:
with self.pool.connection() as conn:
row = conn.execute('INSERT INTO users(id, username, display_name, password_hash) '
'VALUES (%s,%s,%s,%s) RETURNING ' + USER_COLUMNS,
(uuid.uuid4(), username, display, hashed)).fetchone()
except psycopg.errors.UniqueViolation:
raise APIError(409, 'Username already exists') from None
return public_row(row)
def login(self, authorization):
try:
scheme, encoded = authorization.split(' ', 1)
if scheme.lower() != 'basic' or len(encoded) > 1024:
raise ValueError()
username, password = base64.b64decode(encoded, validate=True).decode('utf-8').split(':', 1)
if not re.fullmatch(r'[a-z0-9_]{3,32}', username):
raise ValueError()
password_value(password)
except (AttributeError, ValueError, UnicodeError, APIError):
raise APIError(401, 'Invalid credentials') from None
with self.pool.connection() as conn:
row = conn.execute('SELECT * FROM users WHERE username = %s', (username,)).fetchone()
matched = password_matches(password, row['password_hash'] if row else self.dummy_hash)
if not matched or row is None:
raise APIError(401, 'Invalid credentials')
token = secrets.token_urlsafe(32)
expires_ms = int(datetime.now(timezone.utc).timestamp() * 1000) + self.config.session_ttl_seconds * 1000
self.cache.set(self.session_key(token), str(row['id']), pxat=expires_ms)
return dict(token=token, token_type='Bearer',
expires_at=stamp(datetime.fromtimestamp(expires_ms / 1000, timezone.utc)),
user=public_row(row))
@staticmethod
def session_key(token):
return 'session:' + hashlib.sha256(token.encode('ascii')).hexdigest()
def auth(self, header):
try:
scheme, token = header.split(' ', 1)
if scheme.lower() != 'bearer' or not re.fullmatch(r'[A-Za-z0-9_-]{43}', token):
raise ValueError()
except (ValueError, AttributeError):
raise APIError(401, 'Invalid session') from None
key = self.session_key(token)
actor = self.cache.get(key)
if actor is None:
raise APIError(401, 'Invalid session')
return actor, key
def me(self, actor):
with self.pool.connection() as conn:
row = conn.execute('SELECT ' + USER_COLUMNS + ' FROM users WHERE id = %s', (actor,)).fetchone()
if row is None:
raise APIError(401, 'Invalid session')
return public_row(row)
def chat_access(self, actor, identifier):
identifier = uuid_value(identifier)
with self.pool.connection() as conn:
row = conn.execute('SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s',
(identifier, actor)).fetchone()
if row is None:
raise APIError(404, 'Object not found')
return identifier
@staticmethod
def chat_row(conn, row):
members = conn.execute('SELECT user_id FROM chat_members WHERE chat_id=%s ORDER BY position',
(row['id'],)).fetchall()
return dict(public_row(row), member_ids=[str(x['user_id']) for x in members])
def chat(self, chat_id):
with self.pool.connection() as conn:
row = conn.execute('SELECT * FROM chats WHERE id=%s', (chat_id,)).fetchone()
return self.chat_row(conn, row)
def create_chat(self, actor, body):
object_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:
raise APIError(400, 'Invalid members')
members = [uuid_value(x) for x in members]
if len(members) != len(set(members)) or actor not in members:
raise APIError(400, 'Invalid members')
try:
with self.pool.connection() as conn:
row = conn.execute('INSERT INTO chats(id,title) VALUES (%s,%s) RETURNING *',
(uuid.uuid4(), title)).fetchone()
with conn.cursor() as cur:
cur.executemany('INSERT INTO chat_members(chat_id,user_id,position) VALUES (%s,%s,%s)',
[(row['id'], member, pos) for pos, member in enumerate(members)])
except psycopg.errors.ForeignKeyViolation:
raise APIError(400, 'Invalid members') from None
return dict(public_row(row), member_ids=members)
def create_message(self, actor, chat_id, body):
object_fields(body, ('text',))
text = string(body['text'], 4000)
with self.pool.connection() as conn:
row = conn.execute('INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) '
'RETURNING ' + MESSAGE_COLUMNS, (uuid.uuid4(), chat_id, actor, text)).fetchone()
return public_row(row)
def encode_cursor(self, scope, key):
payload = json.dumps([1, scope, *key], separators=(',', ':')).encode()
return base64.urlsafe_b64encode(payload + hmac.digest(self.secret, payload, 'sha256')).rstrip(b'=').decode()
def decode_cursor(self, cursor, scope):
try:
if not re.fullmatch(r'[A-Za-z0-9_-]{1,1024}', cursor):
raise ValueError()
raw = base64.b64decode(cursor + '=' * (-len(cursor) % 4), altchars=b'-_', validate=True)
if base64.urlsafe_b64encode(raw).rstrip(b'=').decode() != cursor:
raise ValueError()
payload, signature = raw[:-32], raw[-32:]
if not hmac.compare_digest(signature, hmac.digest(self.secret, payload, 'sha256')):
raise ValueError()
version, resource, timestamp, identifier = json.loads(payload)
if version != 1 or resource != scope or not isinstance(timestamp, str) or not timestamp.endswith('Z'):
raise ValueError()
return datetime.fromisoformat(timestamp.replace('Z', '+00:00')), uuid_value(identifier)
except (ValueError, TypeError, UnicodeError, APIError):
raise APIError(400, 'Invalid cursor') from None
def page(self, actor, chat_id, query):
if set(query) - {'limit', 'cursor'} or any(len(v) != 1 for v in query.values()):
raise APIError(400, 'Invalid query')
limit = query.get('limit', ['50'])[0]
if not re.fullmatch(r'[0-9]{1,3}', limit) or not 1 <= int(limit) <= 100:
raise APIError(400, 'Invalid limit')
limit = int(limit)
scope = 'messages:' + chat_id if chat_id else 'chats:' + actor
key = self.decode_cursor(query['cursor'][0], scope) if 'cursor' in query else (
datetime.min.replace(tzinfo=timezone.utc), '00000000-0000-0000-0000-000000000000')
with self.pool.connection() as conn:
if chat_id:
rows = conn.execute(HISTORY_SQL, (chat_id, *key, limit + 1)).fetchall()
items = [public_row(row) for row in rows[:limit]]
else:
rows = conn.execute('SELECT c.* FROM chats c JOIN chat_members m ON m.chat_id=c.id '
'WHERE m.user_id=%s AND (c.created_at,c.id) > (%s,%s) '
'ORDER BY c.created_at,c.id LIMIT %s', (actor, *key, limit + 1)).fetchall()
items = [self.chat_row(conn, row) for row in rows[:limit]]
cursor = self.encode_cursor(scope, (items[-1]['created_at'], items[-1]['id'])) if len(rows) > limit else None
return dict(items=items, next_cursor=cursor)