-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
35 lines (26 loc) · 1.26 KB
/
Copy pathauth.py
File metadata and controls
35 lines (26 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from fastapi import HTTPException, Request
from datetime import datetime
from database import get_master_conn
async def verify_token(request: Request) -> dict:
"""Dependency to verify authentication token."""
# Get token from Authorization header
auth_header = request.headers.get('Authorization')
if not auth_header or not auth_header.startswith('Bearer '):
raise HTTPException(status_code=401, detail="Missing or invalid authorization token")
token = auth_header.replace('Bearer ', '')
conn = get_master_conn()
cursor = conn.cursor()
try:
# Check if token exists and is not expired
cursor.execute("""SELECT tournament_id, table_id, expires_at FROM session_tokens
WHERE token = ?""", (token,))
result = cursor.fetchone()
finally:
conn.close()
if not result:
raise HTTPException(status_code=401, detail="Invalid authentication token")
tournament_id, table_id, expires_at = result
# Check if token is expired
if datetime.fromisoformat(expires_at) < datetime.now():
raise HTTPException(status_code=401, detail="Authentication token expired")
return {"tournament_id": tournament_id, "table_id": table_id, "token": token}