from datetime import datetime, timedelta from typing import Optional, Any, Union import jwt import bcrypt from fastapi import HTTPException, Security, status, Depends from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from app.core.config import settings security = HTTPBearer() def get_password_hash(password: str) -> str: pwd_bytes = password.encode('utf-8') salt = bcrypt.gensalt() return bcrypt.hashpw(pwd_bytes, salt).decode('utf-8') def verify_password(plain_password: str, hashed_password: str) -> bool: try: return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8')) except Exception: return False def create_access_token(subject: Union[str, Any], expires_delta: Optional[timedelta] = None) -> str: if expires_delta: expire = datetime.utcnow() + expires_delta else: expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) to_encode = {"exp": expire, "sub": str(subject)} encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM) return encoded_jwt def create_student_access_token(student_db_id: int, student_id: str) -> str: """Create a JWT token for a student portal session.""" expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) to_encode = { "exp": expire, "sub": str(student_db_id), "role": "STUDENT", "student_id": student_id, } return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM) def decode_access_token(token: str) -> dict: try: payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) return payload except jwt.PyJWTError: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) def get_current_user_token(credentials: HTTPAuthorizationCredentials = Security(security)) -> dict: return decode_access_token(credentials.credentials) def get_current_student_token(credentials: HTTPAuthorizationCredentials = Security(security)) -> dict: """Dependency for student-only endpoints. Validates JWT has role=STUDENT.""" payload = decode_access_token(credentials.credentials) if payload.get("role") != "STUDENT": raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Access restricted to student accounts only." ) return payload