Files

372 lines
15 KiB
Python

"""Lab2. PostgreSQL is authoritative; Redis caches immutable public profiles only."""
import asyncio
import base64
import hashlib
import hmac
import json
import logging
import os
import re
import secrets
import time
import uuid
from contextlib import asynccontextmanager
from datetime import datetime
import psycopg
from psycopg_pool import PoolTimeout, TooManyRequests
from argon2 import PasswordHasher
from argon2.exceptions import VerificationError
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, Response
from redis.asyncio import Redis
from redis.exceptions import RedisError
from redis.retry import Retry
from redis.backoff import NoBackoff
from starlette.exceptions import HTTPException
from db import make_pool, migrate, setting
log = logging.getLogger('uvicorn.error')
ph = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
dummy_hash = ph.hash('dummy-password-never-used')
CODES = {400:'invalid_request',401:'unauthorized',404:'not_found',409:'conflict',
413:'payload_too_large',415:'unsupported_media_type',429:'rate_limited',
503:'unavailable',500:'internal_error'}
class Problem(Exception):
def __init__(self, status, message):
self.status, self.message = status, message
def uid(value):
try:
if not isinstance(value, str):
raise ValueError()
return str(uuid.UUID(value))
except (ValueError, AttributeError):
raise Problem(400, 'Invalid UUID') from None
def field(body, key, maximum, minimum=1):
value = body[key]
if not isinstance(value, str) or not minimum <= len(value) <= maximum or not value.strip() or '\x00' in value:
raise Problem(400, 'Invalid ' + key)
try:
value.encode('utf-8')
except UnicodeError:
raise Problem(400, 'Invalid Unicode') from None
return value
def wire(value):
if isinstance(value, dict):
return {k:wire(v) for k,v in value.items()}
if isinstance(value, list):
return [wire(v) for v in value]
if isinstance(value, uuid.UUID):
return str(value)
if isinstance(value, datetime):
return value.isoformat(timespec='microseconds').replace('+00:00','Z')
return value
@asynccontextmanager
async def lifespan(app):
app.state.pool = make_pool()
app.state.secret = setting('CURSOR_SECRET').encode()
if len(app.state.secret) < 32:
raise RuntimeError('CURSOR_SECRET must contain at least 32 characters')
app.state.ttl = int(os.getenv('SESSION_TTL_SECONDS','3600'))
if app.state.ttl <= 0:
raise RuntimeError('SESSION_TTL_SECONDS must be positive')
app.state.kdf_slots = asyncio.Semaphore(4)
app.state.cache = Redis.from_url(setting('CACHE_URL'), socket_timeout=.2,
socket_connect_timeout=.2, decode_responses=True,
max_connections=16, retry=Retry(NoBackoff(), 0))
await app.state.pool.open()
try:
await app.state.pool.wait(timeout=15)
await migrate(app.state.pool)
yield
finally:
await app.state.cache.aclose()
await app.state.pool.close()
app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None, openapi_url=None)
def error(request, status, message):
headers = {}
if status == 401:
headers['WWW-Authenticate'] = ('Basic realm="messenger", charset="UTF-8"' if request.url.path == '/api/v1/auth/sessions' else 'Bearer realm="messenger"')
return JSONResponse({'error':{'code':CODES.get(status,'invalid_request'),
'message':message,'request_id':request.state.rid}}, status_code=status, headers=headers)
@app.middleware('http')
async def boundary(request, call_next):
request.state.rid = str(uuid.uuid4())
start = time.monotonic()
try:
response = await call_next(request)
except (psycopg.OperationalError, psycopg.InterfaceError, psycopg.errors.QueryCanceled,
psycopg.errors.LockNotAvailable, PoolTimeout, TooManyRequests):
response = error(request,503,'Database temporarily unavailable')
except Exception:
log.error('Unhandled failure request_id=%s',request.state.rid)
response = error(request,500,'Internal server error')
response.headers['X-Request-Id'] = request.state.rid
log.info('method=%s route=%s status=%s elapsed_ms=%.2f request_id=%s',request.method,
getattr(request.scope.get('route'),'path','unmatched'),response.status_code,
(time.monotonic()-start)*1000,request.state.rid)
return response
@app.exception_handler(Problem)
async def problem(request, exc):
return error(request,exc.status,exc.message)
@app.exception_handler(HTTPException)
async def http_error(request,exc):
return error(request,exc.status_code,'Invalid request')
async def payload(request, fields):
if request.headers.get('content-type','').split(';')[0].strip().lower() != 'application/json':
raise Problem(415,'Expected application/json')
raw = bytearray()
async for part in request.stream():
raw.extend(part)
if len(raw) > 65536:
raise Problem(413,'Request body too large')
try:
body = json.loads(raw)
except (ValueError,UnicodeError,RecursionError):
raise Problem(400,'Malformed JSON') from None
if not isinstance(body,dict) or set(body) != set(fields):
raise Problem(400,'Missing or extra fields')
return body
async def kdf(function, *args):
# Admission is bounded; expensive password operations never run on the event loop.
try:
await asyncio.wait_for(app.state.kdf_slots.acquire(), timeout=1)
except TimeoutError:
raise Problem(503,'Password service busy') from None
try:
return await asyncio.to_thread(function, *args)
finally:
app.state.kdf_slots.release()
def bearer(request):
parts = request.headers.get('authorization','').split()
if len(parts) != 2 or parts[0].lower() != 'bearer' or not re.fullmatch(r'[A-Za-z0-9_-]{43}',parts[1]):
raise Problem(401,'Invalid session')
return hashlib.sha256(parts[1].encode()).hexdigest()
async def actor(conn, request):
token_hash = bearer(request)
row = await (await conn.execute('SELECT user_id FROM sessions WHERE token_hash=%s AND expires_at>clock_timestamp()', (token_hash,))).fetchone()
if not row:
raise Problem(401,'Invalid session')
return row['user_id']
async def accessible(conn, chat_id, user_id):
chat_id = uid(chat_id)
row = await (await conn.execute('SELECT 1 FROM members WHERE chat_id=%s AND user_id=%s',(chat_id,user_id))).fetchone()
if not row:
raise Problem(404,'Chat not found')
return chat_id
async def profile(conn, user_id):
key = 'profile:v1:' + str(user_id)
try:
cached = await app.state.cache.get(key)
if cached:
return json.loads(cached)
except (RedisError,ValueError):
pass
row = wire(await (await conn.execute('SELECT id,username,display_name,created_at FROM users WHERE id=%s',(user_id,))).fetchone())
try:
await app.state.cache.set(key,json.dumps(row),ex=60)
except RedisError:
pass
return row
def paging(request, scope):
raw = request.query_params.get('limit','50')
if not re.fullmatch('[0-9]{1,3}',raw) or not 1 <= int(raw) <= 100:
raise Problem(400,'Invalid limit')
cursor = request.query_params.get('cursor')
after = None
if cursor is not None:
try:
if len(cursor)>1024:
raise ValueError()
data, signature = cursor.split('.')
expected = hmac.new(app.state.secret,data.encode(),hashlib.sha256).hexdigest()
if not hmac.compare_digest(signature,expected):
raise ValueError()
resource,stamp,ident = json.loads(base64.urlsafe_b64decode(data+'='*(-len(data)%4)))
if resource != scope:
raise ValueError()
after = (datetime.fromisoformat(stamp.replace('Z','+00:00')),uuid.UUID(ident))
except (ValueError,TypeError,UnicodeError):
raise Problem(400,'Invalid cursor') from None
return int(raw),after
def page(rows, limit, scope):
rows = wire(rows)
cursor = None
if len(rows)>limit:
last = rows[limit-1]
data = base64.urlsafe_b64encode(json.dumps([scope,last['created_at'],last['id']]).encode()).decode().rstrip('=')
cursor = data+'.'+hmac.new(app.state.secret,data.encode(),hashlib.sha256).hexdigest()
return {'items':rows[:limit],'next_cursor':cursor}
@app.get('/health/live')
async def live():
return {'status':'ok'}
@app.get('/health/ready')
async def ready():
async with app.state.pool.connection() as conn:
await conn.execute('SELECT 1')
return {'status':'ok'}
@app.post('/api/v1/users',status_code=201)
async def create_user(request:Request):
body = await payload(request,['username','display_name','password'])
name = field(body,'username',32)
display = field(body,'display_name',100)
password = field(body,'password',128,12)
if not re.fullmatch('[a-z0-9_]{3,32}',name):
raise Problem(400,'Invalid username')
hashed = await kdf(ph.hash,password)
try:
async with app.state.pool.connection() as conn:
row = await (await 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,hashed))).fetchone()
return wire(row)
except psycopg.errors.UniqueViolation:
raise Problem(409,'Username already exists') from None
@app.post('/api/v1/auth/sessions',status_code=201)
async def login(request:Request):
async for part in request.stream():
if part:
raise Problem(400,'Login does not accept a body')
try:
scheme,encoded = request.headers.get('authorization','').split()
if scheme.lower() != 'basic' or len(encoded)>1024:
raise ValueError()
name,password = base64.b64decode(encoded,validate=True).decode('utf-8').split(':',1)
if not re.fullmatch('[a-z0-9_]{3,32}',name) or not 12<=len(password)<=128:
raise ValueError()
except (ValueError,UnicodeError):
raise Problem(401,'Invalid credentials') from None
async with app.state.pool.connection() as conn:
row = await (await conn.execute('SELECT id,password_hash FROM users WHERE username=%s',(name,))).fetchone()
try:
await kdf(ph.verify,row['password_hash'] if row else dummy_hash,password)
except VerificationError:
raise Problem(401,'Invalid credentials') from None
if not row:
raise Problem(401,'Invalid credentials')
token = secrets.token_urlsafe(32)
async with app.state.pool.connection() as conn:
expiry = await (await conn.execute("INSERT INTO sessions VALUES (%s,%s,clock_timestamp() + %s * interval '1 second') RETURNING expires_at",(hashlib.sha256(token.encode()).hexdigest(),row['id'],app.state.ttl))).fetchone()
user = await profile(conn,row['id'])
return {'token':token,'token_type':'Bearer','expires_at':wire(expiry['expires_at']),'user':user}
@app.delete('/api/v1/auth/sessions/current',status_code=204)
async def logout(request:Request):
async with app.state.pool.connection() as conn:
row = await (await conn.execute('DELETE FROM sessions WHERE token_hash=%s AND expires_at>clock_timestamp() RETURNING user_id',(bearer(request),))).fetchone()
if not row:
raise Problem(401,'Invalid session')
return Response(status_code=204)
@app.get('/api/v1/users/me')
async def me(request:Request):
async with app.state.pool.connection() as conn:
return await profile(conn,await actor(conn,request))
CHAT_SQL = '''SELECT c.id,c.title,c.created_at,
ARRAY(SELECT user_id FROM members WHERE chat_id=c.id ORDER BY user_id) AS member_ids FROM chats c'''
@app.post('/api/v1/chats',status_code=201)
async def create_chat(request:Request):
async with app.state.pool.connection() as conn:
user_id = await actor(conn,request)
body = await payload(request,['title','member_ids'])
title = field(body,'title',100)
members = body['member_ids']
if not isinstance(members,list) or not 1<=len(members)<=100:
raise Problem(400,'Invalid member_ids')
members = [uid(x) for x in members]
if len(set(members))!=len(members) or str(user_id) not in members:
raise Problem(400,'Invalid member_ids')
chat_id = uuid.uuid4()
try:
async with app.state.pool.connection() as conn:
# First INSERT may succeed; a missing member then rolls back the WHOLE operation.
await conn.execute('INSERT INTO chats(id,title) VALUES (%s,%s)',(chat_id,title))
for member in members:
await conn.execute('INSERT INTO members VALUES (%s,%s)',(chat_id,member))
row = await (await conn.execute(CHAT_SQL+' WHERE c.id=%s',(chat_id,))).fetchone()
return wire(row)
except psycopg.errors.ForeignKeyViolation:
raise Problem(400,'Invalid member_ids') from None
@app.get('/api/v1/chats')
async def list_chats(request:Request):
async with app.state.pool.connection() as conn:
user_id = await actor(conn,request)
scope = 'chats:'+str(user_id)
limit,after = paging(request,scope)
sql = CHAT_SQL+' WHERE EXISTS (SELECT 1 FROM members m WHERE m.chat_id=c.id AND m.user_id=%s)'
args = [user_id]
if after:
sql += ' AND (c.created_at,c.id)>(%s,%s)'
args.extend(after)
rows = await (await conn.execute(sql+' ORDER BY c.created_at,c.id LIMIT %s',args+[limit+1])).fetchall()
return page(rows,limit,scope)
@app.get('/api/v1/chats/{chat_id}')
async def get_chat(chat_id:str,request:Request):
async with app.state.pool.connection() as conn:
chat_id = await accessible(conn,chat_id,await actor(conn,request))
return wire(await (await conn.execute(CHAT_SQL+' WHERE c.id=%s',(chat_id,))).fetchone())
@app.post('/api/v1/chats/{chat_id}/messages',status_code=201)
async def send(chat_id:str,request:Request):
async with app.state.pool.connection() as conn:
user_id = await actor(conn,request)
chat_id = await accessible(conn,chat_id,user_id)
body = await payload(request,['text'])
content = field(body,'text',4000)
async with app.state.pool.connection() as conn:
row = await (await conn.execute('INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) RETURNING *',(uuid.uuid4(),chat_id,user_id,content))).fetchone()
return wire(row)
@app.get('/api/v1/chats/{chat_id}/messages')
async def history(chat_id:str,request:Request):
async with app.state.pool.connection() as conn:
chat_id = await accessible(conn,chat_id,await actor(conn,request))
scope = 'messages:'+chat_id
limit,after = paging(request,scope)
sql,args = 'SELECT * FROM messages WHERE chat_id=%s',[chat_id]
if after:
sql += ' AND (created_at,id)>(%s,%s)'
args.extend(after)
rows = await (await conn.execute(sql+' ORDER BY created_at,id LIMIT %s',args+[limit+1])).fetchall()
return page(rows,limit,scope)