
- Implemented user authentication with JWT tokens - Created comprehensive task management with CRUD operations - Added category system for task organization - Set up SQLite database with SQLAlchemy ORM - Configured Alembic for database migrations - Added API documentation with OpenAPI/Swagger - Implemented proper authorization and user scoping - Created health check and root endpoints - Updated README with complete documentation
39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
from jose import jwt, JWTError
|
|
from sqlalchemy.orm import Session
|
|
from app.db.session import get_db
|
|
from app.core.security import SECRET_KEY, ALGORITHM
|
|
from app.models.user import User
|
|
|
|
security = HTTPBearer()
|
|
|
|
def get_current_user(
|
|
db: Session = Depends(get_db),
|
|
credentials: HTTPAuthorizationCredentials = Depends(security)
|
|
) -> User:
|
|
credentials_exception = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Could not validate credentials",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
try:
|
|
payload = jwt.decode(credentials.credentials, SECRET_KEY, algorithms=[ALGORITHM])
|
|
user_id: str = payload.get("sub")
|
|
if user_id is None:
|
|
raise credentials_exception
|
|
except JWTError:
|
|
raise credentials_exception
|
|
|
|
user = db.query(User).filter(User.id == int(user_id)).first()
|
|
if user is None:
|
|
raise credentials_exception
|
|
return user
|
|
|
|
def get_current_active_user(
|
|
current_user: User = Depends(get_current_user),
|
|
) -> User:
|
|
if not current_user.is_active:
|
|
raise HTTPException(status_code=400, detail="Inactive user")
|
|
return current_user |