From 3c092f3f2bbab0a52295081c50d52b53736ba4a1 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Mon, 2 Jun 2025 19:19:44 +0000 Subject: [PATCH] Setup FastAPI user authentication service with JWT tokens and SQLite database --- README.md | 136 +++++++++++++++++- alembic.ini | 102 +++++++++++++ app/api/deps.py | 70 +++++++++ app/api/v1/api.py | 8 ++ app/api/v1/endpoints/__init__.py | 0 app/api/v1/endpoints/auth.py | 75 ++++++++++ app/api/v1/endpoints/users.py | 92 ++++++++++++ app/core/config.py | 42 ++++++ app/core/security.py | 42 ++++++ app/crud/__init__.py | 0 app/crud/base.py | 66 +++++++++ app/crud/crud_user.py | 61 ++++++++ app/db/base.py | 3 + app/db/base_class.py | 14 ++ app/db/session.py | 24 ++++ app/models/user.py | 18 +++ app/schemas/__init__.py | 0 app/schemas/token.py | 13 ++ app/schemas/user.py | 51 +++++++ main.py | 53 +++++++ migrations/README | 8 ++ migrations/env.py | 81 +++++++++++ migrations/script.py.mako | 24 ++++ .../20230901_000001_create_user_table.py | 50 +++++++ requirements.txt | 12 ++ 25 files changed, 1043 insertions(+), 2 deletions(-) create mode 100644 alembic.ini create mode 100644 app/api/deps.py create mode 100644 app/api/v1/api.py create mode 100644 app/api/v1/endpoints/__init__.py create mode 100644 app/api/v1/endpoints/auth.py create mode 100644 app/api/v1/endpoints/users.py create mode 100644 app/core/config.py create mode 100644 app/core/security.py create mode 100644 app/crud/__init__.py create mode 100644 app/crud/base.py create mode 100644 app/crud/crud_user.py create mode 100644 app/db/base.py create mode 100644 app/db/base_class.py create mode 100644 app/db/session.py create mode 100644 app/models/user.py create mode 100644 app/schemas/__init__.py create mode 100644 app/schemas/token.py create mode 100644 app/schemas/user.py create mode 100644 main.py create mode 100644 migrations/README create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/20230901_000001_create_user_table.py create mode 100644 requirements.txt diff --git a/README.md b/README.md index e8acfba..1cf6cd5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,135 @@ -# FastAPI Application +# User Authentication Service -This is a FastAPI application bootstrapped by BackendIM, the AI-powered backend generation platform. +A FastAPI-based user authentication service with JWT token authentication, SQLite database, and full user management capabilities. + +## Features + +- User registration and login +- JWT token-based authentication +- User profile management (read, update) +- Role-based access control (normal users vs. superusers) +- Email and username validation +- Password hashing with bcrypt +- SQLite database with SQLAlchemy ORM +- Database migrations with Alembic +- OpenAPI documentation + +## Project Structure + +``` +├── app/ # Application package +│ ├── api/ # API endpoints +│ │ ├── deps.py # API dependencies +│ │ └── v1/ # API v1 endpoints +│ │ ├── endpoints/ # API endpoint modules +│ │ │ ├── auth.py # Authentication endpoints +│ │ │ └── users.py # User endpoints +│ │ └── api.py # API router +│ ├── core/ # Core modules +│ │ ├── config.py # Application settings +│ │ └── security.py # Security utilities +│ ├── crud/ # CRUD operations +│ │ ├── base.py # Base CRUD class +│ │ └── crud_user.py # User CRUD operations +│ ├── db/ # Database setup +│ │ ├── base.py # Import all models +│ │ ├── base_class.py # Base model class +│ │ └── session.py # Database session +│ ├── models/ # SQLAlchemy models +│ │ └── user.py # User model +│ └── schemas/ # Pydantic schemas +│ ├── token.py # Token schemas +│ └── user.py # User schemas +├── migrations/ # Alembic migrations +│ ├── versions/ # Migration versions +│ ├── env.py # Alembic environment +│ ├── README # Migrations README +│ └── script.py.mako # Migration script template +├── alembic.ini # Alembic configuration +├── main.py # Application entry point +└── requirements.txt # Project dependencies +``` + +## Requirements + +- Python 3.8+ +- Dependencies listed in requirements.txt + +## Installation + +1. Clone the repository +2. Install dependencies: + ```bash + pip install -r requirements.txt + ``` +3. Set up environment variables (or create a .env file): + ``` + SECRET_KEY=your-secret-key + ACCESS_TOKEN_EXPIRE_MINUTES=30 + ``` + +## Database Setup + +The application uses SQLite database. The database file will be created at `/app/storage/db/db.sqlite`. + +Initialize the database with Alembic: + +```bash +alembic upgrade head +``` + +## Running the Application + +Start the application with Uvicorn: + +```bash +uvicorn main:app --host 0.0.0.0 --port 8000 --reload +``` + +## API Documentation + +Once the application is running, you can access: + +- Swagger UI: http://localhost:8000/docs +- ReDoc: http://localhost:8000/redoc +- OpenAPI JSON: http://localhost:8000/openapi.json + +## API Endpoints + +### Authentication + +- `POST /api/v1/auth/register` - Register a new user +- `POST /api/v1/auth/login` - Login and get access token + +### User Management + +- `GET /api/v1/users/me` - Get current user +- `PUT /api/v1/users/me` - Update current user +- `GET /api/v1/users/{user_id}` - Get user by ID (current user or superuser only) +- `GET /api/v1/users/` - List users (superuser only) + +### Health Check + +- `GET /health` - Check application health + +## Environment Variables + +| Variable | Description | Default | +|---------------------------|-------------------------------|---------------------------| +| SECRET_KEY | JWT secret key | "YOUR_SECRET_KEY_CHANGE_THIS" | +| ACCESS_TOKEN_EXPIRE_MINUTES | Token expiration in minutes | 30 | +| ALGORITHM | JWT algorithm | "HS256" | +| EMAILS_ENABLED | Enable email features | False | +| EMAILS_FROM_NAME | Sender name for emails | "User Authentication Service" | +| EMAILS_FROM_EMAIL | Sender email | "info@example.com" | +| SMTP_HOST | SMTP host | "" | +| SMTP_PORT | SMTP port | 587 | +| SMTP_USER | SMTP user | "" | +| SMTP_PASSWORD | SMTP password | "" | + +## Security Considerations + +- In production, change the default SECRET_KEY +- Use HTTPS in production +- Consider rate limiting for login endpoints +- Implement additional security measures as needed diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 0000000..85d0745 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,102 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = migrations + +# template used to generate migration files +file_template = %%(year)d%%(month).2d%%(day).2d_%%(hour).2d%%(minute).2d%%(second).2d_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python-dateutil library that can be +# installed by adding `alembic[tz]` to the pip requirements +# string value is passed to dateutil.tz.gettz() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to migrations/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +# SQLite URL - Note: Using absolute path +sqlalchemy.url = sqlite:////app/storage/db/db.sqlite + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S \ No newline at end of file diff --git a/app/api/deps.py b/app/api/deps.py new file mode 100644 index 0000000..cce0272 --- /dev/null +++ b/app/api/deps.py @@ -0,0 +1,70 @@ +from fastapi import Depends, HTTPException, status +from fastapi.security import OAuth2PasswordBearer +from jose import jwt +from jose.exceptions import JWTError +from pydantic import ValidationError +from sqlalchemy.orm import Session + +from app.core.config import settings +from app.core.security import ALGORITHM +from app.db.session import get_db +from app.models.user import User +from app.schemas.token import TokenPayload + +oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.API_V1_STR}/auth/login") + + +def get_current_user( + db: Session = Depends(get_db), token: str = Depends(oauth2_scheme) +) -> User: + """ + Validate and decode the JWT token to get the current user + """ + credentials_exception = HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Could not validate credentials", + headers={"WWW-Authenticate": "Bearer"}, + ) + try: + payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[ALGORITHM]) + token_data = TokenPayload(**payload) + if token_data.sub is None: + raise credentials_exception + user_id: int = token_data.sub + except (JWTError, ValidationError): + raise credentials_exception + + user = db.query(User).filter(User.id == user_id).first() + if not user: + raise credentials_exception + if not user.is_active: + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user" + ) + return user + + +def get_current_active_user( + current_user: User = Depends(get_current_user), +) -> User: + """ + Get the current active user + """ + if not current_user.is_active: + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user" + ) + return current_user + + +def get_current_active_superuser( + current_user: User = Depends(get_current_user), +) -> User: + """ + Get the current active superuser + """ + if not current_user.is_superuser: + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions" + ) + return current_user diff --git a/app/api/v1/api.py b/app/api/v1/api.py new file mode 100644 index 0000000..91ebdad --- /dev/null +++ b/app/api/v1/api.py @@ -0,0 +1,8 @@ +from fastapi import APIRouter + +from app.api.v1.endpoints import auth, users + +api_router = APIRouter() + +api_router.include_router(auth.router, prefix="/auth", tags=["Authentication"]) +api_router.include_router(users.router, prefix="/users", tags=["Users"]) diff --git a/app/api/v1/endpoints/__init__.py b/app/api/v1/endpoints/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/api/v1/endpoints/auth.py b/app/api/v1/endpoints/auth.py new file mode 100644 index 0000000..edcd9b4 --- /dev/null +++ b/app/api/v1/endpoints/auth.py @@ -0,0 +1,75 @@ +from datetime import timedelta +from typing import Any + +from fastapi import APIRouter, Depends, HTTPException, status +from fastapi.security import OAuth2PasswordRequestForm +from sqlalchemy.orm import Session + +from app.api import deps +from app.core import security +from app.core.config import settings +from app.crud import crud_user +from app.schemas.token import Token +from app.schemas.user import User, UserCreate + +router = APIRouter() + + +@router.post("/register", response_model=User) +def register( + *, + db: Session = Depends(deps.get_db), + user_in: UserCreate, +) -> Any: + """ + Register a new user. + """ + # Check if user with this email already exists + user = crud_user.user.get_by_email(db, email=user_in.email) + if user: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="A user with this email already exists", + ) + + # Check if user with this username already exists + user = crud_user.user.get_by_username(db, username=user_in.username) + if user: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="A user with this username already exists", + ) + + # Create new user + user = crud_user.user.create(db, obj_in=user_in) + return user + + +@router.post("/login", response_model=Token) +def login_access_token( + db: Session = Depends(deps.get_db), form_data: OAuth2PasswordRequestForm = Depends() +) -> Any: + """ + OAuth2 compatible token login, get an access token for future requests. + """ + user = crud_user.user.authenticate( + db, email=form_data.username, password=form_data.password + ) + if not user: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Incorrect email or password", + ) + if not crud_user.user.is_active(user): + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail="Inactive user", + ) + + access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) + return { + "access_token": security.create_access_token( + user.id, expires_delta=access_token_expires + ), + "token_type": "bearer", + } diff --git a/app/api/v1/endpoints/users.py b/app/api/v1/endpoints/users.py new file mode 100644 index 0000000..efe41f1 --- /dev/null +++ b/app/api/v1/endpoints/users.py @@ -0,0 +1,92 @@ +from typing import Any, List + +from fastapi import APIRouter, Body, Depends, HTTPException, status +from fastapi.encoders import jsonable_encoder +from sqlalchemy.orm import Session + +from app.api import deps +from app.crud import crud_user +from app.models.user import User +from app.schemas.user import User as UserSchema +from app.schemas.user import UserUpdate + +router = APIRouter() + + +@router.get("/me", response_model=UserSchema) +def read_user_me( + current_user: User = Depends(deps.get_current_active_user), +) -> Any: + """ + Get current user. + """ + return current_user + + +@router.put("/me", response_model=UserSchema) +def update_user_me( + *, + db: Session = Depends(deps.get_db), + password: str = Body(None), + first_name: str = Body(None), + last_name: str = Body(None), + email: str = Body(None), + current_user: User = Depends(deps.get_current_active_user), +) -> Any: + """ + Update current user. + """ + current_user_data = jsonable_encoder(current_user) + user_in = UserUpdate(**current_user_data) + if password is not None: + user_in.password = password + if first_name is not None: + user_in.first_name = first_name + if last_name is not None: + user_in.last_name = last_name + if email is not None: + # Check if email already exists for another user + existing_user = crud_user.user.get_by_email(db, email=email) + if existing_user and existing_user.id != current_user.id: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Email already registered", + ) + user_in.email = email + + user = crud_user.user.update(db, db_obj=current_user, obj_in=user_in) + return user + + +@router.get("/{user_id}", response_model=UserSchema) +def read_user_by_id( + user_id: int, + current_user: User = Depends(deps.get_current_active_user), + db: Session = Depends(deps.get_db), +) -> Any: + """ + Get a specific user by id. + """ + user = crud_user.user.get(db, id=user_id) + if user == current_user: + return user + if not crud_user.user.is_superuser(current_user): + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail="Not enough permissions", + ) + return user + + +@router.get("/", response_model=List[UserSchema]) +def read_users( + db: Session = Depends(deps.get_db), + skip: int = 0, + limit: int = 100, + current_user: User = Depends(deps.get_current_active_superuser), +) -> Any: + """ + Retrieve users. Only for superusers. + """ + users = crud_user.user.get_multi(db, skip=skip, limit=limit) + return users diff --git a/app/core/config.py b/app/core/config.py new file mode 100644 index 0000000..afed4b7 --- /dev/null +++ b/app/core/config.py @@ -0,0 +1,42 @@ +from pathlib import Path +from typing import List + +from pydantic import AnyHttpUrl, EmailStr +from pydantic_settings import BaseSettings + +# Get project base directory +BASE_DIR = Path(__file__).resolve().parent.parent.parent + + +class Settings(BaseSettings): + API_V1_STR: str = "/api/v1" + PROJECT_NAME: str = "User Authentication Service" + + # SECURITY + SECRET_KEY: str = "YOUR_SECRET_KEY_CHANGE_THIS" # Change in production + ACCESS_TOKEN_EXPIRE_MINUTES: int = 30 # 30 minutes + ALGORITHM: str = "HS256" + + # CORS + BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [] + + # Database + DB_DIR = Path("/app") / "storage" / "db" + SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite" + + # Email + EMAILS_ENABLED: bool = False + EMAILS_FROM_NAME: str = "User Authentication Service" + EMAILS_FROM_EMAIL: EmailStr = "info@example.com" + SMTP_HOST: str = "" + SMTP_PORT: int = 587 + SMTP_USER: str = "" + SMTP_PASSWORD: str = "" + + class Config: + case_sensitive = True + env_file = BASE_DIR / ".env" + env_file_encoding = "utf-8" + + +settings = Settings() diff --git a/app/core/security.py b/app/core/security.py new file mode 100644 index 0000000..f47450e --- /dev/null +++ b/app/core/security.py @@ -0,0 +1,42 @@ +from datetime import datetime, timedelta +from typing import Any, Optional, Union + +from jose import jwt +from passlib.context import CryptContext + +from app.core.config import settings + +pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") + +ALGORITHM = settings.ALGORITHM + + +def create_access_token( + subject: Union[str, Any], expires_delta: Optional[timedelta] = None +) -> str: + """ + Create a JWT access token + """ + 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=ALGORITHM) + return encoded_jwt + + +def verify_password(plain_password: str, hashed_password: str) -> bool: + """ + Verify a password against a hash + """ + return pwd_context.verify(plain_password, hashed_password) + + +def get_password_hash(password: str) -> str: + """ + Hash a password + """ + return pwd_context.hash(password) diff --git a/app/crud/__init__.py b/app/crud/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/crud/base.py b/app/crud/base.py new file mode 100644 index 0000000..33da497 --- /dev/null +++ b/app/crud/base.py @@ -0,0 +1,66 @@ +from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union + +from fastapi.encoders import jsonable_encoder +from pydantic import BaseModel +from sqlalchemy.orm import Session + +from app.db.base_class import Base + +ModelType = TypeVar("ModelType", bound=Base) +CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel) +UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel) + + +class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]): + def __init__(self, model: Type[ModelType]): + """ + CRUD object with default methods to Create, Read, Update, Delete (CRUD). + + **Parameters** + + * `model`: A SQLAlchemy model class + * `schema`: A Pydantic model (schema) class + """ + self.model = model + + def get(self, db: Session, id: Any) -> Optional[ModelType]: + return db.query(self.model).filter(self.model.id == id).first() + + def get_multi( + self, db: Session, *, skip: int = 0, limit: int = 100 + ) -> List[ModelType]: + return db.query(self.model).offset(skip).limit(limit).all() + + def create(self, db: Session, *, obj_in: CreateSchemaType) -> ModelType: + obj_in_data = jsonable_encoder(obj_in) + db_obj = self.model(**obj_in_data) + db.add(db_obj) + db.commit() + db.refresh(db_obj) + return db_obj + + def update( + self, + db: Session, + *, + db_obj: ModelType, + obj_in: Union[UpdateSchemaType, Dict[str, Any]], + ) -> ModelType: + obj_data = jsonable_encoder(db_obj) + if isinstance(obj_in, dict): + update_data = obj_in + else: + update_data = obj_in.dict(exclude_unset=True) + for field in obj_data: + if field in update_data: + setattr(db_obj, field, update_data[field]) + db.add(db_obj) + db.commit() + db.refresh(db_obj) + return db_obj + + def remove(self, db: Session, *, id: int) -> ModelType: + obj = db.query(self.model).get(id) + db.delete(obj) + db.commit() + return obj diff --git a/app/crud/crud_user.py b/app/crud/crud_user.py new file mode 100644 index 0000000..a7c1839 --- /dev/null +++ b/app/crud/crud_user.py @@ -0,0 +1,61 @@ +from typing import Any, Dict, Optional, Union + +from sqlalchemy.orm import Session + +from app.core.security import get_password_hash, verify_password +from app.crud.base import CRUDBase +from app.models.user import User +from app.schemas.user import UserCreate, UserUpdate + + +class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]): + def get_by_email(self, db: Session, *, email: str) -> Optional[User]: + return db.query(User).filter(User.email == email).first() + + def get_by_username(self, db: Session, *, username: str) -> Optional[User]: + return db.query(User).filter(User.username == username).first() + + def create(self, db: Session, *, obj_in: UserCreate) -> User: + db_obj = User( + email=obj_in.email, + username=obj_in.username, + hashed_password=get_password_hash(obj_in.password), + first_name=obj_in.first_name, + last_name=obj_in.last_name, + is_active=obj_in.is_active, + is_superuser=obj_in.is_superuser, + ) + db.add(db_obj) + db.commit() + db.refresh(db_obj) + return db_obj + + def update( + self, db: Session, *, db_obj: User, obj_in: Union[UserUpdate, Dict[str, Any]] + ) -> User: + if isinstance(obj_in, dict): + update_data = obj_in + else: + update_data = obj_in.dict(exclude_unset=True) + if update_data.get("password"): + hashed_password = get_password_hash(update_data["password"]) + del update_data["password"] + update_data["hashed_password"] = hashed_password + return super().update(db, db_obj=db_obj, obj_in=update_data) + + def authenticate(self, db: Session, *, email: str, password: str) -> Optional[User]: + user = self.get_by_email(db, email=email) + if not user: + return None + if not verify_password(password, user.hashed_password): + return None + return user + + def is_active(self, user: User) -> bool: + return user.is_active + + def is_superuser(self, user: User) -> bool: + return user.is_superuser + + +user = CRUDUser(User) diff --git a/app/db/base.py b/app/db/base.py new file mode 100644 index 0000000..d80b849 --- /dev/null +++ b/app/db/base.py @@ -0,0 +1,3 @@ +# Import all the models here to ensure they are registered with SQLAlchemy's metadata +from app.db.base_class import Base # noqa +from app.models.user import User # noqa diff --git a/app/db/base_class.py b/app/db/base_class.py new file mode 100644 index 0000000..b1daeb8 --- /dev/null +++ b/app/db/base_class.py @@ -0,0 +1,14 @@ +from typing import Any + +from sqlalchemy.ext.declarative import as_declarative, declared_attr + + +@as_declarative() +class Base: + id: Any + __name__: str + + # Generate __tablename__ automatically based on class name + @declared_attr + def __tablename__(cls) -> str: + return cls.__name__.lower() diff --git a/app/db/session.py b/app/db/session.py new file mode 100644 index 0000000..60b69b3 --- /dev/null +++ b/app/db/session.py @@ -0,0 +1,24 @@ +from pathlib import Path + +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + + +# Ensure the database directory exists +DB_DIR = Path("/app") / "storage" / "db" +DB_DIR.mkdir(parents=True, exist_ok=True) + +SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite" + +engine = create_engine( + SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} +) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + + +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() diff --git a/app/models/user.py b/app/models/user.py new file mode 100644 index 0000000..86b6ba7 --- /dev/null +++ b/app/models/user.py @@ -0,0 +1,18 @@ +from datetime import datetime + +from sqlalchemy import Boolean, Column, DateTime, Integer, String + +from app.db.base_class import Base + + +class User(Base): + id = Column(Integer, primary_key=True, index=True) + email = Column(String, unique=True, index=True, nullable=False) + username = Column(String, unique=True, index=True, nullable=False) + hashed_password = Column(String, nullable=False) + first_name = Column(String, nullable=True) + last_name = Column(String, nullable=True) + is_active = Column(Boolean, default=True) + is_superuser = Column(Boolean, default=False) + created_at = Column(DateTime, default=datetime.utcnow) + updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) diff --git a/app/schemas/__init__.py b/app/schemas/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/schemas/token.py b/app/schemas/token.py new file mode 100644 index 0000000..6557b2d --- /dev/null +++ b/app/schemas/token.py @@ -0,0 +1,13 @@ +from typing import Optional + +from pydantic import BaseModel + + +class Token(BaseModel): + access_token: str + token_type: str + + +class TokenPayload(BaseModel): + sub: Optional[int] = None + exp: Optional[int] = None diff --git a/app/schemas/user.py b/app/schemas/user.py new file mode 100644 index 0000000..a347f20 --- /dev/null +++ b/app/schemas/user.py @@ -0,0 +1,51 @@ +from datetime import datetime +from typing import Optional + +from pydantic import BaseModel, EmailStr, Field, validator + + +# Shared properties +class UserBase(BaseModel): + email: Optional[EmailStr] = None + username: Optional[str] = None + is_active: Optional[bool] = True + is_superuser: Optional[bool] = False + first_name: Optional[str] = None + last_name: Optional[str] = None + + +# Properties to receive via API on creation +class UserCreate(UserBase): + email: EmailStr + username: str + password: str = Field(..., min_length=8) + + @validator("username") + def username_alphanumeric(cls, v): + assert v.isalnum(), "Username must be alphanumeric" + return v + + +# Properties to receive via API on update +class UserUpdate(UserBase): + password: Optional[str] = Field(None, min_length=8) + + +# Database model properties +class UserInDBBase(UserBase): + id: Optional[int] = None + created_at: Optional[datetime] = None + updated_at: Optional[datetime] = None + + class Config: + orm_mode = True + + +# Additional properties to return via API +class User(UserInDBBase): + pass + + +# Additional properties stored in DB but not returned by API +class UserInDB(UserInDBBase): + hashed_password: str diff --git a/main.py b/main.py new file mode 100644 index 0000000..4bed7aa --- /dev/null +++ b/main.py @@ -0,0 +1,53 @@ +import uvicorn +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from fastapi.openapi.utils import get_openapi + +from app.api.v1.api import api_router +from app.core.config import settings + +app = FastAPI( + title=settings.PROJECT_NAME, + version="0.1.0", + description="User Authentication Service API", + docs_url="/docs", + redoc_url="/redoc", + openapi_url="/openapi.json", +) + +# Add CORS middleware +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +# Include API router +app.include_router(api_router, prefix=settings.API_V1_STR) + + +# Health check endpoint +@app.get("/health", tags=["Health"]) +async def health_check(): + return {"status": "healthy"} + + +def custom_openapi(): + if app.openapi_schema: + return app.openapi_schema + openapi_schema = get_openapi( + title=app.title, + version=app.version, + description=app.description, + routes=app.routes, + ) + app.openapi_schema = openapi_schema + return app.openapi_schema + + +app.openapi = custom_openapi + +if __name__ == "__main__": + uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) diff --git a/migrations/README b/migrations/README new file mode 100644 index 0000000..75f7cb6 --- /dev/null +++ b/migrations/README @@ -0,0 +1,8 @@ +# Alembic Migrations + +This directory contains database migration scripts for the User Authentication Service. + +## Usage + +1. Run migrations: `alembic upgrade head` +2. Create new migration: `alembic revision -m "description of changes"` \ No newline at end of file diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 0000000..f993788 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,81 @@ +from logging.config import fileConfig + +from alembic import context +from sqlalchemy import engine_from_config, pool + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +from app.db.base import Base # noqa + +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + # Determine if we're using SQLite and enable batch mode if so + is_sqlite = connection.dialect.name == "sqlite" + + context.configure( + connection=connection, + target_metadata=target_metadata, + render_as_batch=is_sqlite, # Enable batch mode for SQLite + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 0000000..37d0cac --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} \ No newline at end of file diff --git a/migrations/versions/20230901_000001_create_user_table.py b/migrations/versions/20230901_000001_create_user_table.py new file mode 100644 index 0000000..2986979 --- /dev/null +++ b/migrations/versions/20230901_000001_create_user_table.py @@ -0,0 +1,50 @@ +"""create user table + +Revision ID: 20230901_000001 +Revises: +Create Date: 2023-09-01 00:00:01.000000 + +""" + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "20230901_000001" +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # Create user table + op.create_table( + "user", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("email", sa.String(), nullable=False), + sa.Column("username", sa.String(), nullable=False), + sa.Column("hashed_password", sa.String(), nullable=False), + sa.Column("first_name", sa.String(), nullable=True), + sa.Column("last_name", sa.String(), nullable=True), + sa.Column("is_active", sa.Boolean(), nullable=True, default=True), + sa.Column("is_superuser", sa.Boolean(), nullable=True, default=False), + sa.Column("created_at", sa.DateTime(), nullable=True), + sa.Column("updated_at", sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint("id"), + ) + + # Create indices for faster lookups + op.create_index(op.f("ix_user_email"), "user", ["email"], unique=True) + op.create_index(op.f("ix_user_id"), "user", ["id"], unique=False) + op.create_index(op.f("ix_user_username"), "user", ["username"], unique=True) + + +def downgrade() -> None: + # Drop indices + op.drop_index(op.f("ix_user_username"), table_name="user") + op.drop_index(op.f("ix_user_id"), table_name="user") + op.drop_index(op.f("ix_user_email"), table_name="user") + + # Drop table + op.drop_table("user") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ccc9389 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +fastapi>=0.95.0 +uvicorn>=0.21.1 +sqlalchemy>=2.0.0 +pydantic>=2.0.0 +pydantic-settings>=2.0.0 +python-jose[cryptography]>=3.3.0 +passlib[bcrypt]>=1.7.4 +python-multipart>=0.0.6 +alembic>=1.11.0 +email-validator>=2.0.0 +ruff>=0.0.270 +pathlib>=1.0.1 \ No newline at end of file