"""PostgreSQL is authoritative; Valkey caches only immutable public profiles.""" from contextlib import contextmanager import base64 import datetime as dt import hashlib import hmac import json import logging import os import re import secrets import uuid import psycopg from psycopg.rows import dict_row from psycopg_pool import ConnectionPool, PoolTimeout, TooManyRequests import redis from redis.backoff import NoBackoff from redis.retry import Retry from config import integer, secret from http_transport import APIError, fields, identifier, invalid, text LOG = logging.getLogger('messenger') USER_COLUMNS = 'id,username,display_name,created_at' CHAT_COLUMNS = 'c.id,c.title,c.created_at,ARRAY(SELECT user_id FROM chat_members WHERE chat_id=c.id ORDER BY user_id) AS member_ids' def public(row): result = {} for key, value in row.items(): if isinstance(value, uuid.UUID): value = str(value) elif isinstance(value, dt.datetime): value = value.astimezone(dt.timezone.utc).isoformat(timespec='microseconds').replace('+00:00','Z') elif isinstance(value, list): value = [str(item) for item in value] result[key] = value return result def password_hash(password, salt=None): salt = salt if salt is not None else secrets.token_bytes(16) derived = hashlib.scrypt(password.encode('utf-8'), salt=salt, n=16384, r=8, p=1, dklen=32, maxmem=64*1024*1024) return 'scrypt$16384$8$1$' + salt.hex() + '$' + derived.hex() def password_valid(password, encoded): parts = encoded.split('$') return hmac.compare_digest(password_hash(password, bytes.fromhex(parts[4])), encoded) def unauthorized(basic=False): challenge = 'Basic realm="messenger", charset="UTF-8"' if basic else 'Bearer realm="messenger"' return APIError(401, 'unauthorized', 'Неверные или отсутствующие учётные данные', {'WWW-Authenticate': challenge}) class Store: def __init__(self): if os.environ.get('SESSION_STORE','postgres') != 'postgres': raise ValueError('SESSION_STORE must be postgres') self.ttl = integer('SESSION_TTL_SECONDS', 3600) self.cache_ttl = integer('PROFILE_CACHE_TTL_SECONDS', 300) self.pool = ConnectionPool(secret('DATABASE_URL'), min_size=1, max_size=integer('DB_POOL_MAX', 8, maximum=32), max_waiting=32, timeout=2, reconnect_timeout=3, check=ConnectionPool.check_connection, kwargs={'connect_timeout': 2, 'row_factory': dict_row, 'options': '-c statement_timeout=2000 -c lock_timeout=1000 -c idle_in_transaction_session_timeout=5000'}, open=True) self.cache = redis.Redis.from_url(secret('CACHE_URL'), decode_responses=True, socket_connect_timeout=.2, socket_timeout=.2, max_connections=16, retry=Retry(NoBackoff(), 0)) with self.connection() as connection: self.cursor_key = bytes.fromhex(connection.execute("SELECT value FROM app_settings WHERE name='cursor_key'").fetchone()['value']) self.dummy_hash = password_hash('not-an-existing-password') @contextmanager def connection(self): try: with self.pool.connection() as connection: yield connection except (psycopg.OperationalError, psycopg.InterfaceError, PoolTimeout, TooManyRequests, psycopg.errors.QueryCanceled, psycopg.errors.LockNotAvailable): raise APIError(503, 'unavailable', 'Хранилище временно недоступно') from None def close(self): self.pool.close() self.cache.close() def ready(self): with self.connection() as connection: connection.execute('SELECT 1') def user(self, body): fields(body, ('username','display_name','password')) name = body['username'] if not isinstance(name, str) or not re.fullmatch(r'[a-z0-9_]{3,32}', name): raise invalid('Неверный username') display = text(body['display_name'], 100) password = text(body['password'], 128) if len(password) < 12: raise invalid('Пароль должен содержать 12–128 символов') encoded = password_hash(password) try: with self.connection() as connection: user = connection.execute('INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s) RETURNING ' + USER_COLUMNS, (uuid.uuid4(),name,display,encoded)).fetchone() return public(user) except psycopg.errors.UniqueViolation: raise APIError(409, 'conflict', 'Username занят') from None def profile(self, actor): key = 'profile:v1:' + actor try: value = self.cache.get(key) if value is not None: result = json.loads(value) if set(result) == {'id','username','display_name','created_at'} and result['id'] == actor: return result except (redis.RedisError, ValueError, TypeError): LOG.info('profile_cache_fallback') with self.connection() as connection: result = public(connection.execute('SELECT ' + USER_COLUMNS + ' FROM users WHERE id=%s', (actor,)).fetchone()) try: self.cache.setex(key, self.cache_ttl, json.dumps(result)) except redis.RedisError: LOG.info('profile_cache_write_skipped') return result def login(self, authorization): try: scheme, value = authorization.split(' ', 1) if scheme.lower() != 'basic': raise ValueError decoded = base64.b64decode(value, validate=True).decode('utf-8') username, separator, password = decoded.partition(':') if not separator or not re.fullmatch(r'[a-z0-9_]{3,32}',username) or not 12 <= len(password) <= 128: raise ValueError password.encode('utf-8') except (ValueError, AttributeError, UnicodeError): raise unauthorized(True) from None with self.connection() as connection: user = connection.execute('SELECT id,password_hash FROM users WHERE username=%s', (username,)).fetchone() valid = password_valid(password, user['password_hash'] if user else self.dummy_hash) if not valid or user is None: raise unauthorized(True) token = secrets.token_urlsafe(32) with self.connection() as connection: session = connection.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()).digest(),user['id'],self.ttl)).fetchone() return {'token': token, 'token_type': 'Bearer', 'expires_at': public(session)['expires_at'], 'user': self.profile(str(user['id']))} @staticmethod def token_hash(authorization): try: scheme, token = authorization.split(' ', 1) if scheme.lower() != 'bearer' or not re.fullmatch(r'[A-Za-z0-9_-]{43}', token): raise ValueError return hashlib.sha256(token.encode()).digest() except (ValueError, AttributeError): raise unauthorized() from None def authenticate(self, authorization): digest = self.token_hash(authorization) with self.connection() as connection: session = connection.execute('SELECT user_id FROM sessions WHERE token_hash=%s AND expires_at > clock_timestamp()', (digest,)).fetchone() if session is None: raise unauthorized() return str(session['user_id']) def logout(self, authorization): digest = self.token_hash(authorization) with self.connection() as connection: revoked = connection.execute('DELETE FROM sessions WHERE token_hash=%s AND expires_at > clock_timestamp() RETURNING user_id', (digest,)).fetchone() if revoked is None: raise unauthorized() def 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: raise invalid('Ожидался список участников длиной 1–100') members = [identifier(member) for member in members] if actor not in members or len(set(members)) != len(members): raise invalid('Неверный список участников') cid = uuid.uuid4() try: with self.connection() as connection: # Deliberately rely on FK inside the SAME transaction: any bad # member rolls back the chat and all earlier memberships. connection.execute('INSERT INTO chats(id,title) VALUES (%s,%s)', (cid,title)) with connection.cursor() as cursor: cursor.executemany('INSERT INTO chat_members(chat_id,user_id) VALUES (%s,%s)', [(cid,member) for member in members]) return public(connection.execute('SELECT ' + CHAT_COLUMNS + ' FROM chats c WHERE c.id=%s', (cid,)).fetchone()) except psycopg.errors.ForeignKeyViolation: raise invalid('Неверный список участников') from None def accessible(self, actor, cid, connection=None): cid = identifier(cid) if connection is None: with self.connection() as connection: return self.accessible(actor,cid,connection) chat = connection.execute('SELECT ' + CHAT_COLUMNS + ' FROM chats c WHERE c.id=%s AND EXISTS (SELECT 1 FROM chat_members WHERE chat_id=c.id AND user_id=%s)', (cid,actor)).fetchone() if chat is None: raise APIError(404, 'not_found', 'Объект не найден') return public(chat) def message(self, actor, cid, body): fields(body, ('text',)) value = text(body['text'],4000) with self.connection() as connection: self.accessible(actor,cid,connection) timestamp = connection.execute("UPDATE chats SET last_message_at=greatest(clock_timestamp(),last_message_at + interval '1 microsecond') WHERE id=%s RETURNING last_message_at", (cid,)).fetchone()['last_message_at'] result = connection.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(),cid,actor,value,timestamp)).fetchone() return public(result) def encode_cursor(self, scope, row): raw = json.dumps([scope,row['created_at'],row['id']], separators=(',',':')).encode() return base64.urlsafe_b64encode(raw + hmac.digest(self.cursor_key, raw, 'sha256')).decode().rstrip('=') def decode_cursor(self, scope, cursor): try: if not cursor or len(cursor) > 2048: raise ValueError data = base64.b64decode(cursor + '=' * (-len(cursor) % 4), altchars=b'-_',validate=True) raw, signature = data[:-32], data[-32:] if not hmac.compare_digest(hmac.digest(self.cursor_key,raw,'sha256'),signature): raise ValueError resource, timestamp, uid = json.loads(raw) if resource != scope or not isinstance(timestamp,str): raise ValueError return dt.datetime.fromisoformat(timestamp.replace('Z','+00:00')), identifier(uid) except (ValueError, TypeError, UnicodeError, APIError): raise invalid('Неверный cursor для данного ресурса') from None def history(self, actor, cid, limit, cursor): scope = 'messages:' + identifier(cid) after = self.decode_cursor(scope,cursor) if cursor is not None else None with self.connection() as connection: self.accessible(actor,cid,connection) query = 'SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s' values = [cid] if after: query += ' AND (created_at,id) > (%s,%s)' values.extend(after) query += ' ORDER BY created_at,id LIMIT %s' rows = connection.execute(query, (*values,limit+1)).fetchall() items = [public(row) for row in rows[:limit]] return {'items':items, 'next_cursor':self.encode_cursor(scope,items[-1]) if len(rows)>limit else None} def list_chats(self, actor, limit, cursor): scope = 'chats:' + actor after = self.decode_cursor(scope,cursor) if cursor is not None else None query = 'SELECT ' + CHAT_COLUMNS + ' FROM chats c JOIN chat_members mine ON mine.chat_id=c.id WHERE mine.user_id=%s' values = [actor] if after: query += ' AND (c.created_at,c.id) > (%s,%s)' values.extend(after) query += ' ORDER BY c.created_at,c.id LIMIT %s' with self.connection() as connection: rows = connection.execute(query, (*values,limit+1)).fetchall() items = [public(row) for row in rows[:limit]] return {'items':items, 'next_cursor':self.encode_cursor(scope,items[-1]) if len(rows)>limit else None}