Files
mtusi2026lab2/src/app.py
T
2026-11-05 01:43:30 +03:00

343 lines
13 KiB
Python

"""Lab2 API. PostgreSQL is the sole source of truth, including sessions."""
import base64
import hashlib
import json
import os
import re
import secrets
import uuid
import threading
import time
from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeout
from datetime import datetime, timezone
from pathlib import Path
from argon2 import PasswordHasher
from argon2.exceptions import VerificationError, InvalidHashError
from flask import Flask, g, jsonify, request
from jsonschema import Draft202012Validator, FormatChecker, ValidationError
from psycopg import Error as DatabaseError
from psycopg.errors import UniqueViolation
from psycopg.rows import dict_row
from psycopg_pool import ConnectionPool, PoolTimeout
from redis import Redis
from redis.exceptions import RedisError
from redis.retry import Retry
from redis.backoff import NoBackoff
from werkzeug.exceptions import HTTPException
ROOT = Path(__file__).resolve().parents[1]
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 65536
passwords = PasswordHasher(time_cost=3, memory_cost=65536, parallelism=2)
dummy_hash = passwords.hash(secrets.token_urlsafe(32))
ttl = int(os.environ.get('SESSION_TTL_SECONDS', '3600'))
if ttl < 1:
raise ValueError('SESSION_TTL_SECONDS must be positive')
dsn = Path(os.environ['DATABASE_URL_FILE']).read_text().strip() if os.environ.get('DATABASE_URL_FILE') else os.environ['DATABASE_URL']
pool = ConnectionPool(dsn, min_size=1, max_size=8, timeout=3,
kwargs={'row_factory': dict_row, 'connect_timeout': 3,
'options': '-c statement_timeout=3000 -c lock_timeout=2000'})
# Cache only immutable public profiles, never sessions or authorization decisions.
cache_url = Path(os.environ['CACHE_URL_FILE']).read_text().strip() if os.environ.get('CACHE_URL_FILE') else os.environ.get('CACHE_URL', 'redis://cache:6379/0')
cache = Redis.from_url(cache_url, socket_connect_timeout=0.2, socket_timeout=0.2,
max_connections=8, decode_responses=True,
retry=Retry(NoBackoff(), 0), retry_on_timeout=False)
PROFILE_CACHE_TTL = 60
# Socket timeouts do not bound DNS resolution. Limit the complete optional-cache
# operation, bound outstanding work and skip calls briefly after a failure.
cache_workers = ThreadPoolExecutor(max_workers=2, thread_name_prefix='profile-cache')
cache_slots = threading.BoundedSemaphore(2)
cache_breaker_lock = threading.Lock()
cache_retry_after = 0.0
def cache_call(method, *args):
global cache_retry_after
with cache_breaker_lock:
if time.monotonic() < cache_retry_after:
raise RedisError('Optional cache circuit open')
if not cache_slots.acquire(blocking=False):
raise RedisError('Optional cache busy')
try:
future = cache_workers.submit(getattr(cache, method), *args)
except Exception:
cache_slots.release()
raise
future.add_done_callback(lambda _: cache_slots.release())
try:
return future.result(timeout=0.25)
except (RedisError, FutureTimeout):
with cache_breaker_lock:
cache_retry_after = time.monotonic() + 2
raise RedisError('Optional cache unavailable') from None
schemas = json.loads((ROOT/'contracts/openapi.json').read_text())['components']['schemas']
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 unauthorized():
raise APIError(401, 'unauthorized', 'Authentication required')
@app.before_request
def request_id():
g.request_id = uuid.uuid4().hex
@app.after_request
def headers(response):
response.headers['X-Request-Id'] = g.request_id
response.headers['Cache-Control'] = 'no-store'
if response.status_code == 401:
response.headers['WWW-Authenticate'] = ('Basic realm="messenger", charset="UTF-8"'
if request.path == '/api/v1/auth/sessions' else 'Bearer realm="messenger"')
return response
@app.errorhandler(Exception)
def error(exc):
if isinstance(exc, APIError):
status, code, message = exc.status, exc.code, exc.message
elif isinstance(exc, UniqueViolation):
status, code, message = 409, 'conflict', 'Username already registered'
elif isinstance(exc, (DatabaseError, PoolTimeout)):
status, code, message = 503, 'unavailable', 'Storage temporarily unavailable'
elif isinstance(exc, HTTPException):
status = exc.code
code = {400:'invalid_request',404:'not_found',405:'not_found',413:'payload_too_large',415:'unsupported_media_type'}.get(status,'invalid_request')
message = 'Request rejected'
else:
app.logger.error('Unhandled exception class=%s request_id=%s', type(exc).__name__, g.request_id)
status, code, message = 500, 'internal_error', 'Internal error'
return jsonify(error={'code':code,'message':message,'request_id':g.request_id}), status
def wire(row):
return {k: (str(v) if isinstance(v, uuid.UUID) else
v.astimezone(timezone.utc).isoformat().replace('+00:00','Z') if isinstance(v,datetime) else v)
for k,v in row.items()}
def body(schema):
if not request.is_json:
raise APIError(415,'unsupported_media_type','Expected application/json')
data = request.get_json()
try:
Draft202012Validator(schemas[schema], format_checker=FormatChecker()).validate(data)
except ValidationError:
invalid()
for value in data.values():
if isinstance(value,str) and not value.strip():
invalid('Blank strings are not allowed')
return data
def uid(value):
try:
return str(uuid.UUID(value))
except (ValueError,TypeError,AttributeError):
invalid('Invalid UUID')
def token_digest(token):
return hashlib.sha256(token.encode()).hexdigest()
def authenticate(conn):
auth = request.headers.get('Authorization','').split()
if len(auth)!=2 or auth[0].lower()!='bearer' or len(auth[1])>256:
unauthorized()
g.token_hash = token_digest(auth[1])
session = conn.execute('SELECT user_id FROM sessions WHERE token_hash=%s AND expires_at>clock_timestamp()', (g.token_hash,)).fetchone()
if session is None:
unauthorized()
user_id = str(session['user_id'])
key = 'profile:v1:' + user_id
try:
encoded = cache_call('get', key)
if encoded:
profile = json.loads(encoded)
Draft202012Validator(schemas['User'], format_checker=FormatChecker()).validate(profile)
if profile['id'] == user_id:
return profile
except (RedisError, ValueError, ValidationError):
pass # PostgreSQL remains authoritative and is never bypassed for sessions.
row = conn.execute('SELECT id,username,display_name,created_at FROM users WHERE id=%s', (user_id,)).fetchone()
if row is None:
unauthorized()
profile = wire(row)
try:
cache_call('setex', key, PROFILE_CACHE_TTL, json.dumps(profile))
except RedisError:
pass
return profile
def chat(conn, chat_id, user_id):
row = conn.execute('SELECT c.* FROM chats c JOIN members m ON m.chat_id=c.id WHERE c.id=%s AND m.user_id=%s',(chat_id,user_id)).fetchone()
if row is None:
raise APIError(404,'not_found','Object not found')
result = wire(row)
result['member_ids'] = [str(x['user_id']) for x in conn.execute('SELECT user_id FROM members WHERE chat_id=%s ORDER BY user_id',(chat_id,))]
return result
def page_args(scope):
if set(request.args)-{'limit','cursor'} or any(len(request.args.getlist(k))!=1 for k in request.args):
invalid()
value = request.args.get('limit','50')
if not re.fullmatch(r'[0-9]{1,3}',value) or not 1<=int(value)<=100:
invalid('Invalid limit')
after = None
if 'cursor' in request.args:
try:
encoded=request.args['cursor']
if len(encoded)>1024: invalid()
cursor=json.loads(base64.b64decode(encoded,altchars=b'-_',validate=True))
if len(cursor)!=3 or cursor[0]!=scope: invalid()
stamp=datetime.fromisoformat(cursor[1].replace('Z','+00:00'))
if stamp.tzinfo is None: invalid()
after=(stamp,uid(cursor[2]))
except (ValueError,TypeError,KeyError,IndexError):
invalid('Invalid cursor')
return int(value), after
def page(rows,limit,scope):
items=rows[:limit]
cursor=None
if len(rows)>limit:
last=items[-1]
cursor=base64.urlsafe_b64encode(json.dumps([scope,last['created_at'],last['id']]).encode()).decode()
return jsonify(items=items,next_cursor=cursor)
@app.get('/health/live')
def live():
return jsonify(status='ok')
@app.get('/health/ready')
def ready():
with pool.connection() as conn:
conn.execute('SELECT 1 FROM schema_migrations LIMIT 1')
return jsonify(status='ok')
@app.post('/api/v1/users')
def register():
data=body('CreateUser')
hashed=passwords.hash(data['password'])
with pool.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(),data['username'],data['display_name'],hashed)).fetchone()
return jsonify(wire(row)),201
@app.post('/api/v1/auth/sessions')
def login():
if request.get_data(): invalid('Login has no request body')
auth=request.headers.get('Authorization','').split()
try:
if len(auth)!=2 or auth[0].lower()!='basic': unauthorized()
username,password=base64.b64decode(auth[1],validate=True).decode('utf-8').split(':',1)
except (ValueError,UnicodeError):
unauthorized()
with pool.connection() as conn:
row=conn.execute('SELECT * FROM users WHERE username=%s',(username,)).fetchone()
try:
valid=passwords.verify(row['password_hash'] if row else dummy_hash,password)
except (VerificationError,InvalidHashError):
valid=False
if not valid or row is None: unauthorized()
token=secrets.token_urlsafe(32)
session=conn.execute("INSERT INTO sessions(token_hash,user_id,expires_at) VALUES(%s,%s,clock_timestamp()+(%s * interval '1 second')) RETURNING expires_at",(token_digest(token),row['id'],ttl)).fetchone()
user=wire({k:v for k,v in row.items() if k!='password_hash'})
return jsonify(token=token,token_type='Bearer',expires_at=wire(session)['expires_at'],user=user),201
@app.delete('/api/v1/auth/sessions/current')
def logout():
with pool.connection() as conn:
authenticate(conn)
conn.execute('DELETE FROM sessions WHERE token_hash=%s',(g.token_hash,))
return '',204
@app.get('/api/v1/users/me')
def me():
with pool.connection() as conn:
result=wire(authenticate(conn))
return jsonify(result)
@app.post('/api/v1/chats')
def create_chat():
with pool.connection() as conn:
actor=authenticate(conn)
data=body('CreateChat')
ids=[uid(x) for x in data['member_ids']]
if len(set(ids))!=len(ids) or str(actor['id']) not in ids: invalid()
known=conn.execute('SELECT id FROM users WHERE id=ANY(%s::uuid[])',(ids,)).fetchall()
if len(known)!=len(ids): invalid('Unknown member')
chat_id=uuid.uuid4()
conn.execute('INSERT INTO chats(id,title) VALUES(%s,%s)',(chat_id,data['title']))
for member in ids:
conn.execute('INSERT INTO members(chat_id,user_id) VALUES(%s,%s)',(chat_id,member))
result=chat(conn,chat_id,actor['id'])
return jsonify(result),201
@app.get('/api/v1/chats')
def chats():
with pool.connection() as conn:
actor=authenticate(conn)
scope='chats:'+str(actor['id'])
limit,after=page_args(scope)
sql='SELECT c.* FROM chats c JOIN members m ON m.chat_id=c.id WHERE m.user_id=%s'
args=[actor['id']]
if after:
sql+=' AND (c.created_at,c.id)>(%s,%s)'; args.extend(after)
sql+=' ORDER BY c.created_at,c.id LIMIT %s'; args.append(limit+1)
rows=[chat(conn,x['id'],actor['id']) for x in conn.execute(sql,args).fetchall()]
return page(rows,limit,scope)
@app.get('/api/v1/chats/<chat_id>')
def get_chat(chat_id):
with pool.connection() as conn:
actor=authenticate(conn)
result=chat(conn,uid(chat_id),actor['id'])
return jsonify(result)
@app.route('/api/v1/chats/<chat_id>/messages',methods=['GET','POST'])
def messages(chat_id):
with pool.connection() as conn:
actor=authenticate(conn)
chat_id=uid(chat_id)
chat(conn,chat_id,actor['id'])
if request.method=='POST':
data=body('CreateMessage')
row=conn.execute('INSERT INTO messages(id,chat_id,sender_id,text) VALUES(%s,%s,%s,%s) RETURNING *',(uuid.uuid4(),chat_id,actor['id'],data['text'])).fetchone()
result=wire(row)
else:
scope='messages:'+chat_id
limit,after=page_args(scope)
sql='SELECT * FROM messages WHERE chat_id=%s'; args=[chat_id]
if after:
sql+=' AND (created_at,id)>(%s,%s)'; args.extend(after)
sql+=' ORDER BY created_at,id LIMIT %s'; args.append(limit+1)
rows=[wire(x) for x in conn.execute(sql,args).fetchall()]
return (jsonify(result),201) if request.method=='POST' else page(rows,limit,scope)