Skip to main content

A comprehensive authentication library for FastAPI with JWT and cookie support

Project description

FastAuth

A comprehensive authentication library for FastAPI applications with JWT-based authentication and SQLModel integration. Now with a modular architecture for better maintainability and extensibility.

Features

  • OAuth2 and JWT authentication built-in
  • Cookie-based authentication option
  • Token refresh mechanism for extended sessions
  • SQLModel integration for easy database operations
  • Ready-to-use authentication routes with minimal setup
  • Password hashing with bcrypt
  • Modular architecture for better code organization and extensibility

Installation

pip install fastauth_iq

Or install from source:

git clone https://github.com/hu55ain3laa/fastauth.git
cd fastauth
pip install -e .

Quick Start

1. Create a User Model

FastAuth works with SQLModel's user model. You can use the built-in User model or create your own:

from sqlmodel import SQLModel, Field

class User(SQLModel, table=True):
    id: int = Field(primary_key=True)
    username: str = Field(unique=True)
    email: str = Field(unique=True)
    hashed_password: str
    disabled: bool = Field(default=False)

2. Initialize FastAuth in Your Application

from fastapi import FastAPI, Depends
from sqlmodel import create_engine, Session, SQLModel

from fastauth import FastAuth, User

# Create FastAPI app
app = FastAPI()

# Setup database
engine = create_engine("sqlite:///./app.db")

# Session dependency
def get_session():
    with Session(engine) as session:
        yield session

# Initialize FastAuth with your configuration
auth = FastAuth(
    secret_key="your-secure-secret-key",  # Use strong secret in production
    algorithm="HS256",
    user_model=User,
    engine=engine,
    use_cookie=True,  # Enable cookie-based auth (optional)
    token_url="/token",
    access_token_expires_in=30,  # minutes
    refresh_token_expires_in=7   # days
)

# Add all authentication routes automatically
auth_router = auth.get_auth_router(get_session)
app.include_router(auth_router, tags=["authentication"])

3. Protect Your Routes

@app.get("/protected")
def protected_route(current_user = Depends(auth.get_current_active_user_dependency())):
    return {"message": f"Hello, {current_user.username}!"}

Available Authentication Endpoints

The get_auth_router() method automatically adds these endpoints to your application:

  • POST /token - Get access and refresh tokens with username/password
  • POST /token/refresh - Get a new access token using a refresh token
  • POST /users - Register a new user
  • GET /users/me - Get the current authenticated user's information

Customization Options

Cookie-Based Authentication

Enable cookie-based authentication by setting use_cookie=True:

auth = FastAuth(
    # ... other parameters
    use_cookie=True
)

Custom Token Expiration

Set custom expiration times for tokens:

auth = FastAuth(
    # ... other parameters
    access_token_expires_in=60,  # 60 minutes
    refresh_token_expires_in=30  # 30 days
)

Advanced Usage: Custom Authentication Routes

You can create your own authentication routes instead of using the built-in router:

@app.post("/custom-login")
async def custom_login(
    username: str, 
    password: str, 
    session: Session = Depends(get_session)
):
    user = auth.authenticate_user(username, password)
    if not user:
        raise HTTPException(status_code=401, detail="Invalid credentials")
    
    access_token = auth.create_access_token(data={"sub": user.username})
    return {"access_token": access_token, "token_type": "bearer"}

Security Best Practices

  1. Always use HTTPS in production
  2. Use a strong secret key and keep it secure
  3. Set appropriate token expiration times
  4. Enable httpOnly and secure flags for cookies
  5. Consider implementing rate limiting for authentication endpoints

License

MIT

Modular Architecture

With version 0.2.0, FastAuth has been refactored into a modular architecture to improve maintainability, testability, and extensibility. The code is now organized into specialized modules:

fastauth/
├── core/       # Core FastAuth class and OAuth2 implementation
├── security/   # Password management and token handling
├── models/     # User and token data models
├── routers/    # Authentication route handlers
├── dependencies/ # FastAPI dependencies for authentication
└── utils/      # Utility functions and helpers

This modular structure makes it easier to:

  • Understand and modify specific parts of the library
  • Write targeted tests for each component
  • Extend functionality with new features
  • Reuse components in other projects

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastauth_iq-0.2.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastauth_iq-0.2.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file fastauth_iq-0.2.0.tar.gz.

File metadata

  • Download URL: fastauth_iq-0.2.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for fastauth_iq-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9cf756a4a6c7852da81ddffcd1387b95294921e976e042b336818569e4bf39d6
MD5 3eb05044e081747fa2b93c68692eb04c
BLAKE2b-256 bbfbfc62dae3676f40071ff2f936ed1d2a59af37bbf266caea540e7b1073a07d

See more details on using hashes here.

File details

Details for the file fastauth_iq-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fastauth_iq-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for fastauth_iq-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f8c5db0010983417487e3c4c53b8b0fd3f80f28833c5e699f901a5061c8cd2d0
MD5 686a91c0d6523e07b5b41f29cb457d96
BLAKE2b-256 ab54447e1ec24e68cf925dc19d11383a3b0a92132e65eae29acf35936f80b9dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page