This commit is contained in:
+30
-12
@@ -15,6 +15,7 @@ from app.database import get_db, engine, Base
|
||||
from app.models import User, Session, Chat, ChatMember, Message
|
||||
from app.schemas import UserCreate, UserOut, SessionOut, ChatCreate, ChatOut, MessageCreate, MessageOut, MessagePage
|
||||
from app import security
|
||||
from app.redis_client import get_redis
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@@ -82,18 +83,21 @@ async def get_current_user(
|
||||
)
|
||||
|
||||
token_hash = security.hash_token(credentials.credentials)
|
||||
stmt = select(Session).where(Session.token_hash == token_hash)
|
||||
result = await db.execute(stmt)
|
||||
session = result.scalars().first()
|
||||
|
||||
if not session or session.expires_at < datetime.now(timezone.utc):
|
||||
|
||||
try:
|
||||
r = await get_redis()
|
||||
user_id_str = await r.get(f"session:{token_hash}")
|
||||
except Exception:
|
||||
raise HTTPException(status_code=503, detail="Session store unavailable")
|
||||
|
||||
if not user_id_str:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Session expired or invalid",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'}
|
||||
)
|
||||
|
||||
stmt = select(User).where(User.id == session.user_id)
|
||||
stmt = select(User).where(User.id == uuid.UUID(user_id_str))
|
||||
result = await db.execute(stmt)
|
||||
user = result.scalars().first()
|
||||
if not user:
|
||||
@@ -109,6 +113,11 @@ async def health_live():
|
||||
|
||||
@app.get("/health/ready")
|
||||
async def health_ready():
|
||||
try:
|
||||
r = await get_redis()
|
||||
await r.ping()
|
||||
except Exception:
|
||||
raise HTTPException(status_code=503, detail="Redis unavailable")
|
||||
return {"status": "ok"}
|
||||
|
||||
@app.post("/api/v1/users", response_model=UserOut, status_code=201)
|
||||
@@ -155,9 +164,15 @@ async def create_session(request: Request, db: AsyncSession = Depends(get_db)):
|
||||
token_hash = security.hash_token(raw_token)
|
||||
expires_at = datetime.now(timezone.utc) + timedelta(seconds=settings.session_ttl_seconds)
|
||||
|
||||
new_session = Session(user_id=user.id, token_hash=token_hash, expires_at=expires_at)
|
||||
db.add(new_session)
|
||||
await db.commit()
|
||||
try:
|
||||
r = await get_redis()
|
||||
await r.setex(
|
||||
name=f"session:{token_hash}",
|
||||
time=settings.session_ttl_seconds,
|
||||
value=str(user.id)
|
||||
)
|
||||
except Exception:
|
||||
raise HTTPException(status_code=503, detail="Session store unavailable")
|
||||
|
||||
return {
|
||||
"token": raw_token,
|
||||
@@ -176,9 +191,12 @@ async def logout(request: Request, current_user: User = Depends(get_current_user
|
||||
token = auth_header.split(" ")[1]
|
||||
token_hash = security.hash_token(token)
|
||||
|
||||
stmt = delete(Session).where(Session.token_hash == token_hash)
|
||||
await db.execute(stmt)
|
||||
await db.commit()
|
||||
try:
|
||||
r = await get_redis()
|
||||
await r.delete(f"session:{token_hash}")
|
||||
except Exception:
|
||||
raise HTTPException(status_code=503, detail="Session store unavailable")
|
||||
|
||||
return Response(status_code=204)
|
||||
|
||||
@app.post("/api/v1/chats", response_model=ChatOut, status_code=201)
|
||||
|
||||
Reference in New Issue
Block a user