Spaces:
Sleeping
Sleeping
File size: 17,989 Bytes
d810052 22e081e d810052 22e081e d810052 3456591 d810052 3456591 3dfabb3 3456591 d810052 c5156ee d810052 96f7504 d810052 c5156ee d810052 0a9b204 80e5de9 d810052 80e5de9 0a9b204 80e5de9 0a9b204 80e5de9 0a9b204 d810052 0a9b204 80e5de9 d810052 80e5de9 0a9b204 80e5de9 0a9b204 80e5de9 d810052 ef00ffa d810052 ef00ffa d810052 ef00ffa d810052 0a9b204 80e5de9 d810052 0a9b204 80e5de9 0a9b204 d810052 |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
"""
auth.py β Lightweight authentication micro-service
----------------------------------------------------
β’ Exposes /auth/verify_user (login check)
β’ Optional /auth/register_user for sign-up
β’ Uses MySQL, bcrypt, and Pydantic
β’ No Streamlit, cookies, or front-end logic
"""
from fastapi import FastAPI, Request, Response, Cookie, HTTPException, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import OAuth2PasswordRequestForm
from jose import jwt, JWTError
from pydantic import BaseModel
from dotenv import load_dotenv
import os
import time
import requests
import os
import pymysql
import bcrypt
import sys
import importlib.util
from services.api.db.token_utils import create_token, decode_token
from sqlalchemy.orm import Session
from contextlib import asynccontextmanager
# Add parent directory to path to enable imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Define lifespan context manager for startup/shutdown events
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
print("Starting up FastAPI application...")
# Start background tasks from upload endpoints
try:
# Import and start upload cleanup task
from services.api.upload_endpoints import start_cleanup_task
cleanup_task = start_cleanup_task()
print("Upload cleanup task started successfully")
except Exception as e:
print(f"Failed to start upload cleanup task: {e}")
yield
# Shutdown
print("Shutting down FastAPI application...")
# Any cleanup can be done here
# Create single FastAPI instance with lifespan
app = FastAPI(lifespan=lifespan)
# Configure CORS with all needed origins
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:8503",
"http://localhost:3000",
"http://127.0.0.1:3000",
"https://tlong-ds.github.io",
"https://tlong-ds.github.io/thelearninghouse/",
"https://*.hf.space",
"https://*.huggingface.co"
],
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["*"],
expose_headers=["*"]
)
# Import API endpoints
try:
from services.api.api_endpoints import router as api_router
except ImportError:
# Try a different import strategy for direct module execution
spec = importlib.util.spec_from_file_location(
"api_endpoints",
os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "api_endpoints.py")
)
api_endpoints = importlib.util.module_from_spec(spec)
spec.loader.exec_module(api_endpoints)
api_router = api_endpoints.router
# Load environment variables
load_dotenv()
SECRET_KEY = os.getenv("SECRET_TOKEN", "dev-secret")
ALGORITHM = os.getenv("ALGORITHM", "HS256")
MYSQL_USER = os.getenv("MYSQL_USER")
MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD")
MYSQL_HOST = os.getenv("MYSQL_HOST", "localhost")
MYSQL_DB = os.getenv("MYSQL_DB")
MYSQL_PORT = int(os.getenv("MYSQL_PORT", "3306"))
# Import and include chat router
try:
from services.api.chat_endpoints import router as chat_router
except ImportError:
spec = importlib.util.spec_from_file_location(
"chat_endpoints",
os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "chat_endpoints.py")
)
chat_endpoints = importlib.util.module_from_spec(spec)
spec.loader.exec_module(chat_endpoints)
chat_router = chat_endpoints.router
# Import and include upload router
try:
from services.api.upload_endpoints import router as upload_router
except ImportError:
spec = importlib.util.spec_from_file_location(
"upload_endpoints",
os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "upload_endpoints.py")
)
upload_endpoints = importlib.util.module_from_spec(spec)
spec.loader.exec_module(upload_endpoints)
upload_router = upload_endpoints.router
# Debug print of available routes
print("DEBUG: Available routes in api_router:")
for route in api_router.routes:
print(f"Route: {route.path}, Methods: {route.methods}")
# Include API routers
app.include_router(api_router, prefix="/api")
app.include_router(chat_router, prefix="/api")
app.include_router(upload_router, prefix="/api")
class LoginPayload(BaseModel):
username: str
password: str
role: str
class PasswordChangePayload(BaseModel):
current_password: str
new_password: str
class PasswordChangePayload(BaseModel):
current_password: str
new_password: str
# Token functions moved to token_utils.py
@app.post("/login")
async def login(response: Response, payload: LoginPayload):
try:
print(f"Login attempt: {payload.username}, role: {payload.role}")
# Directly verify the user credentials
table_map = {
"Learner": "Learners",
"Instructor": "Instructors"
}
table = table_map.get(payload.role)
if not table:
print(f"Invalid role: {payload.role}")
raise HTTPException(status_code=400, detail="Invalid role")
conn = connect_db()
user_id = None
full_name = None
try:
with conn.cursor() as cur:
# Get password, user ID, and full name for token creation
if payload.role == "Learner":
query = f"SELECT Password, LearnerID, LearnerName FROM {table} WHERE AccountName=%s LIMIT 1"
else: # Instructor
query = f"SELECT Password, InstructorID, InstructorName FROM {table} WHERE AccountName=%s LIMIT 1"
print(f"Executing query: {query} with username: {payload.username}")
cur.execute(query, (payload.username,))
row = cur.fetchone()
if not row:
print(f"No user found with username: {payload.username} in table: {table}")
raise HTTPException(status_code=401, detail="Incorrect username or password")
password_valid = check_password(payload.password, row[0])
user_id = row[1] # Get the LearnerID or InstructorID
full_name = row[2] # Get the LearnerName or InstructorName
print(f"Password check result: {password_valid}, User ID: {user_id}, Full Name: {full_name}")
if not password_valid:
print(f"Authentication failed: Invalid password")
raise HTTPException(status_code=401, detail="Incorrect username or password")
except Exception as db_err:
print(f"Database error: {str(db_err)}")
raise HTTPException(status_code=500, detail=f"Database error: {str(db_err)}")
finally:
conn.close()
# User authenticated successfully - include user_id and full name in token
user_data = {
"username": payload.username,
"role": payload.role,
"user_id": user_id,
"full_name": full_name
}
print(f"Authentication successful for: {payload.username} (ID: {user_id}, Name: {full_name})")
token = create_token(user_data)
# Set cookie with settings that work for both Chrome and Safari
# For localhost development, we need different settings than production
is_localhost = os.getenv("ENVIRONMENT", "development") == "development"
response.set_cookie(
key="auth_token",
value=token,
httponly=False, # Allow JavaScript access for localStorage fallback
samesite="Lax" if is_localhost else "None", # Lax for localhost, None for cross-origin
secure=False if is_localhost else True, # False for HTTP localhost, True for HTTPS production
path="/",
max_age=604800, # 7 days
domain=None # Let browser set the domain automatically
)
return {
"message": f"Login successful for {user_data['username']}",
"username": user_data["username"],
"role": user_data["role"],
"user_id": user_data["user_id"],
"full_name": user_data["full_name"],
"token": token
}
except Exception as e:
print(f"Login exception: {str(e)}")
raise HTTPException(status_code=500, detail=f"Login error: {str(e)}")
@app.get("/logout")
def logout(response: Response):
response.delete_cookie("auth_token")
return HTMLResponse("<h3>Logged out β cookie cleared!</h3>")
@app.get("/whoami")
async def whoami(auth_token: str = Cookie(None)):
payload = decode_token(auth_token)
return {
"username": payload["username"],
"role": payload["role"],
"user_id": payload.get("user_id"),
"full_name": payload.get("full_name")
}
@app.get("/protected")
def protected_route(auth_token: str = Cookie(None)):
payload = decode_token(auth_token)
return {"msg": "Access granted", "user": payload}
@app.put("/api/user/password")
async def change_password(payload: PasswordChangePayload, request: Request, auth_token: str = Cookie(None)):
"""Change user password endpoint."""
# Get token from cookie or Authorization header
token = auth_token
if not token and "Authorization" in request.headers:
auth_header = request.headers["Authorization"]
if auth_header.startswith("Bearer "):
token = auth_header[7:] # Remove 'Bearer ' prefix
if not token:
raise HTTPException(status_code=401, detail="No authentication token provided")
try:
# Decode token to get user info
user_data = decode_token(token)
if not user_data:
raise HTTPException(status_code=401, detail="Invalid token")
username = user_data["username"]
role = user_data["role"]
# Get the correct table based on role
table_map = {
"Learner": "Learners",
"Instructor": "Instructors"
}
table = table_map.get(role)
if not table:
raise HTTPException(status_code=400, detail="Invalid role")
conn = connect_db()
try:
with conn.cursor() as cur:
# First verify current password
query = f"SELECT Password FROM {table} WHERE AccountName=%s LIMIT 1"
cur.execute(query, (username,))
row = cur.fetchone()
if not row or not check_password(payload.current_password, row[0]):
raise HTTPException(status_code=401, detail="Current password is incorrect")
# Hash the new password
new_password_hash = hash_password(payload.new_password)
# Update the password
update_query = f"UPDATE {table} SET Password=%s WHERE AccountName=%s"
cur.execute(update_query, (new_password_hash, username))
conn.commit()
return {"message": "Password updated successfully"}
except Exception as db_err:
conn.rollback()
print(f"Database error: {str(db_err)}")
raise HTTPException(status_code=500, detail=f"Database error: {str(db_err)}")
finally:
conn.close()
except HTTPException:
raise
except Exception as e:
print(f"Unexpected error in change_password: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
def connect_db():
return pymysql.connect(
host=MYSQL_HOST,
user=MYSQL_USER,
password=MYSQL_PASSWORD,
database=MYSQL_DB,
port=MYSQL_PORT
)
def check_password(plain: str, hashed: str) -> bool:
"""Compare plaintext vs bcrypt hash stored in DB."""
return bcrypt.checkpw(plain.encode("utf-8"), hashed.encode("utf-8"))
def hash_password(plain: str) -> str:
"""Return bcrypt hash for storage."""
return bcrypt.hashpw(plain.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Pydantic models
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class UserCredentials(BaseModel):
username: str
password: str
role: str # "Learner" or "Instructor"
class NewUser(UserCredentials):
name: str
email: str
@app.post("/auth/verify_user")
def verify_user(creds: UserCredentials):
"""
Validate user credentials.
Returns {username, role} on success; 401 on failure.
"""
try:
print(f"Verify user attempt for: {creds.username}, role: {creds.role}")
table_map = {
"Learner": "Learners",
"Instructor": "Instructors"
}
table = table_map.get(creds.role)
if not table:
print(f"Invalid role: {creds.role}")
raise HTTPException(status_code=400, detail="Invalid role")
conn = connect_db()
try:
with conn.cursor() as cur:
query = f"SELECT Password FROM {table} WHERE AccountName=%s LIMIT 1"
print(f"Executing query: {query} with username: {creds.username}")
cur.execute(query, (creds.username,))
row = cur.fetchone()
if not row:
print(f"No user found with username: {creds.username} in table: {table}")
except Exception as db_err:
print(f"Database error: {str(db_err)}")
raise HTTPException(status_code=500, detail=f"Database error: {str(db_err)}")
finally:
conn.close()
if not row:
print(f"Authentication failed: User not found")
raise HTTPException(status_code=401, detail="Incorrect username or password")
password_valid = check_password(creds.password, row[0])
print(f"Password check result: {password_valid}")
if not password_valid:
print(f"Authentication failed: Invalid password")
raise HTTPException(status_code=401, detail="Incorrect username or password")
print(f"Authentication successful for: {creds.username}")
return {"username": creds.username, "role": creds.role}
except HTTPException:
raise
except Exception as e:
print(f"Unexpected error in verify_user: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
@app.post("/auth/register_user")
def register_user(user: NewUser):
"""
Simple sign-up endpoint.
Returns 201 Created on success, 409 if username/email exists.
"""
table_cfg = {
"Learner": ("Learners", "LearnerName"),
"Instructor": ("Instructors", "InstructorName")
}
cfg = table_cfg.get(user.role)
if not cfg:
raise HTTPException(status_code=400, detail="Invalid role")
table, name_col = cfg
hashed_pw = hash_password(user.password)
conn = connect_db()
try:
with conn.cursor() as cur:
# Uniqueness check
cur.execute(
f"SELECT 1 FROM {table} WHERE AccountName=%s OR Email=%s LIMIT 1",
(user.username, user.email)
)
if cur.fetchone():
raise HTTPException(status_code=409, detail="Username or email already exists")
# Insert new record
cur.execute(
f"INSERT INTO {table} ({name_col}, Email, AccountName, Password) "
f"VALUES (%s, %s, %s, %s)",
(user.name, user.email, user.username, hashed_pw)
)
conn.commit()
finally:
conn.close()
return {"msg": "User created", "username": user.username, "role": user.role}, 201
@app.post("/api/auth/logout")
async def logout():
response = JSONResponse(content={"message": "Logged out successfully"})
# Clear all auth-related cookies
response.delete_cookie(key="auth_token", path="/")
response.delete_cookie(key="session_id", path="/")
return response
@app.get("/api/users")
async def get_users(role: str):
table_map = {
"Learner": "Learners",
"Instructor": "Instructors"
}
table = table_map.get(role)
if not table:
raise HTTPException(status_code=400, detail="Invalid role")
conn = connect_db()
try:
with conn.cursor() as cur:
cur.execute(f"SELECT * FROM {table}")
columns = [desc[0] for desc in cur.description]
users = [dict(zip(columns, row)) for row in cur.fetchall()]
return users
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
finally:
conn.close()
@app.get("/api/statistics/users/count")
async def get_user_count(role: str):
table_map = {
"Learner": "Learners",
"Instructor": "Instructors"
}
table = table_map.get(role)
if not table:
raise HTTPException(status_code=400, detail="Invalid role")
conn = connect_db()
try:
with conn.cursor() as cur:
cur.execute(f"SELECT COUNT(*) FROM {table}")
count = cur.fetchone()[0]
return {"count": count}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
finally:
conn.close() |