Files
mtusi2026lab2/src/postgres.py
T

262 lines
11 KiB
Python

import base64
import contextlib
import datetime as dt
import hashlib
import hmac
import json
import os
from pathlib import Path
import secrets
import threading
import uuid
import psycopg
from psycopg.rows import dict_row
from psycopg_pool import ConnectionPool, PoolTimeout
import redis
from common import APIError, fields, identifier, invalid, missing, page, text, unauthorized, username
KDF_SLOTS = threading.BoundedSemaphore(2)
def setting(name):
path = os.environ.get(name + '_FILE')
return Path(path).read_text().strip() if path else os.environ[name]
def public(row):
return {k: (v.decode('utf-8') if isinstance(v, bytes) else str(v) if isinstance(v, uuid.UUID) else v.isoformat(timespec='microseconds').replace('+00:00', 'Z') if isinstance(v, dt.datetime) else v) for k, v in row.items() if k != 'password_hash'}
def password_hash(password, salt=None):
salt = salt or secrets.token_bytes(16)
if not KDF_SLOTS.acquire(timeout=1.5):
raise APIError(503, 'unavailable', 'Аутентификация временно перегружена')
try:
digest = hashlib.scrypt(password.encode('utf-8'), salt=salt, n=16384, r=8, p=1, dklen=32, maxmem=64 * 1024 * 1024)
finally:
KDF_SLOTS.release()
return 'scrypt$16384$8$1$' + salt.hex() + '$' + digest.hex()
class Store:
persistent = True
def __init__(self):
self.ttl = int(os.environ.get('SESSION_TTL_SECONDS', '3600'))
if not 1 <= self.ttl <= 86400:
raise ValueError('SESSION_TTL_SECONDS must be 1..86400')
self.pool = ConnectionPool(setting('DATABASE_URL'), min_size=1, max_size=8, timeout=2, kwargs={'row_factory': dict_row, 'connect_timeout': 2, 'options': '-c statement_timeout=2000 -c lock_timeout=1500'}, open=True)
self.pool.wait(timeout=10)
self.cache = redis.Redis.from_url(setting('CACHE_URL'), socket_connect_timeout=0.15, socket_timeout=0.15, decode_responses=True)
self.dummy_hash = password_hash('dummy-password-for-unknown-users')
self.migrate()
@contextlib.contextmanager
def connection(self):
try:
with self.pool.connection() as conn:
yield conn
except psycopg.errors.UniqueViolation:
raise APIError(409, 'conflict', 'Имя пользователя занято') from None
except (psycopg.Error, PoolTimeout):
raise APIError(503, 'unavailable', 'Хранилище временно недоступно') from None
def migrate(self):
with self.connection() as conn:
conn.execute('SELECT pg_advisory_xact_lock(2301002)')
conn.execute('CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY)')
for path in sorted((Path(__file__).resolve().parents[1] / 'migrations').glob('*.sql')):
if not conn.execute('SELECT version FROM schema_migrations WHERE 1=1 AND version = %s', (path.name,)).fetchone():
conn.execute(path.read_text())
conn.execute('INSERT INTO schema_migrations (version) VALUES (%s)', (path.name,))
def close(self):
self.pool.close()
self.cache.close()
def ready(self):
with self.connection() as conn:
conn.execute('SELECT 1')
def register(self, body):
fields(body, ('username', 'display_name', 'password'))
name, display = username(body['username']), text(body['display_name'], 100)
# Passwords may contain spaces and colons; only length/UTF-8 are restricted.
password = body['password']
if not isinstance(password, str) or not 12 <= len(password) <= 128:
invalid()
try:
password.encode('utf-8')
except UnicodeError:
invalid()
hashed = password_hash(password)
with self.connection() as conn:
row = conn.execute('''
INSERT INTO users (id, username, display_name, password_hash)
VALUES (%s, %s, %s, %s)
RETURNING id, username, display_name, created_at
''', (uuid.uuid4(), name, display.encode('utf-8'), hashed)).fetchone()
return public(row)
def login(self, headers):
authorization = headers.get('Authorization', '')
try:
scheme, encoded = authorization.split(' ', 1)
if scheme.lower() != 'basic':
unauthorized()
name, password = base64.b64decode(encoded, validate=True).decode('utf-8').split(':', 1)
except (ValueError, UnicodeError):
unauthorized()
if not re_username(name) or not 12 <= len(password) <= 128:
unauthorized()
with self.connection() as conn:
row = conn.execute('SELECT id, username, display_name, created_at, password_hash FROM users WHERE 1=1 AND username = %s', (name,)).fetchone()
hashed = row['password_hash'] if row else self.dummy_hash
verified = hmac.compare_digest(password_hash(password, bytes.fromhex(hashed.split('$')[4])), hashed)
if row is None or not verified:
unauthorized()
token = secrets.token_urlsafe(32)
with self.connection() as conn:
expiry = conn.execute('''
INSERT INTO sessions (token_hash, user_id, expires_at)
VALUES (%s, %s, clock_timestamp() + %s * INTERVAL '1 second')
RETURNING expires_at
''', (hashlib.sha256(token.encode()).hexdigest(), row['id'], self.ttl)).fetchone()['expires_at']
return {'token': token, 'token_type': 'Bearer', 'expires_at': public({'expires_at': expiry})['expires_at'], 'user': public(row)}
def token_hash(self, headers):
parts = headers.get('Authorization', '').split(' ')
if len(parts) != 2 or parts[0].lower() != 'bearer' or len(parts[1]) != 43 or not parts[1].isascii():
unauthorized()
return hashlib.sha256(parts[1].encode()).hexdigest()
def authenticate(self, headers):
token_hash = self.token_hash(headers)
with self.connection() as conn:
session = conn.execute('''
SELECT user_id
FROM sessions
WHERE 1=1
AND token_hash = %s
AND expires_at > clock_timestamp()
''', (token_hash,)).fetchone()
if session is None:
unauthorized()
key = 'user:' + str(session['user_id'])
try:
cached = self.cache.get(key)
if cached:
return json.loads(cached)
except (redis.RedisError, ValueError):
pass
row = conn.execute('SELECT id, username, display_name, created_at FROM users WHERE 1=1 AND id = %s', (session['user_id'],)).fetchone()
if row is None:
unauthorized()
user = public(row)
try:
self.cache.setex(key, 60, json.dumps(user))
except redis.RedisError:
pass
return user
def logout(self, headers):
with self.connection() as conn:
conn.execute('DELETE FROM sessions WHERE 1=1 AND token_hash = %s', (self.token_hash(headers),))
def chat_in(self, conn, actor, chat_id):
row = conn.execute('''
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE 1=1
AND c.id = %s
AND cm.user_id = %s
''', (chat_id, actor['id'])).fetchone()
if row is None:
missing()
members = conn.execute('SELECT user_id FROM chat_members WHERE 1=1 AND chat_id = %s ORDER BY user_id', (chat_id,)).fetchall()
return dict(public(row), member_ids=[str(v['user_id']) for v in members])
def chat(self, actor, chat_id):
with self.connection() as conn:
return self.chat_in(conn, actor, chat_id)
def create_chat(self, actor, body):
fields(body, ('title', 'member_ids'))
title = text(body['title'], 100)
members = body['member_ids']
if not isinstance(members, list) or not 1 <= len(members) <= 100:
invalid()
members = [identifier(v) for v in members]
if len(set(members)) != len(members) or actor['id'] not in members:
invalid()
with self.connection() as conn:
chat_id = uuid.uuid4()
conn.execute('INSERT INTO chats (id, title) VALUES (%s, %s)', (chat_id, title.encode('utf-8')))
# Checking after INSERT exercises rollback of the whole composite operation.
found = conn.execute('SELECT id FROM users WHERE 1=1 AND id = ANY(%s::uuid[])', (members,)).fetchall()
if len(found) != len(members):
invalid()
with conn.cursor() as cur:
cur.executemany('INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)', [(chat_id, v) for v in members])
return self.chat_in(conn, actor, chat_id)
def list_chats(self, actor, limit, cursor, scope):
with self.connection() as conn:
where = 'AND (c.created_at, c.id) > (%s::timestamptz, %s::uuid)' if cursor else ''
params = [actor['id']] + list(cursor or ()) + [limit + 1]
rows = conn.execute('''
SELECT c.id
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id
WHERE 1=1
AND cm.user_id = %s
''' + where + '''
ORDER BY c.created_at, c.id
LIMIT %s
''', params).fetchall()
return page([self.chat_in(conn, actor, v['id']) for v in rows], limit, scope)
def send(self, actor, chat_id, body):
fields(body, ('text',))
content = text(body['text'], 4000)
with self.connection() as conn:
self.chat_in(conn, actor, chat_id)
# Row locking serializes sends per chat, including the timestamp assignment.
stamp = conn.execute('''
UPDATE chats
SET last_message_at = GREATEST(clock_timestamp(), last_message_at + INTERVAL '1 microsecond')
WHERE 1=1
AND id = %s
RETURNING last_message_at
''', (chat_id,)).fetchone()['last_message_at']
row = conn.execute('''
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
VALUES (%s, %s, %s, %s, %s)
RETURNING id, chat_id, sender_id, text, created_at
''', (uuid.uuid4(), chat_id, actor['id'], content.encode('utf-8'), stamp)).fetchone()
return public(row)
def history(self, actor, chat_id, limit, cursor, scope):
with self.connection() as conn:
self.chat_in(conn, actor, chat_id)
where = 'AND (created_at, id) > (%s::timestamptz, %s::uuid)' if cursor else ''
params = [chat_id] + list(cursor or ()) + [limit + 1]
rows = conn.execute('''
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE 1=1
AND chat_id = %s
''' + where + '''
ORDER BY created_at, id
LIMIT %s
''', params).fetchall()
return page([public(v) for v in rows], limit, scope)
def re_username(value):
import re
return re.fullmatch(r'[a-z0-9_]{3,32}', value)