Skip to main content

coderfastapi

Project description

Coder FastAPI

Security and utility library for building FastAPI applications with authentication, authorization, and pagination out of the box.

Features

  • SecureRouter — Drop-in APIRouter replacement that enforces authentication and ACL-based authorization on every route
  • Authentication policies — Pluggable strategies for JWT (Bearer token) and session-based authentication
  • Authorization policies — Pyramid-style ACL authorization with Allow/Deny rules and composable principals
  • JWT providers — Extensible provider system for token creation and request augmentation (expiration, user data, recovery mode)
  • Session management — Abstract session manager backed by Redis with configurable TTL and secure token generation
  • Pagination — Cursor-based pagination decorator that auto-generates RFC 5988 Link headers
  • Cloud logging — Google Cloud Logging integration with Cloud Trace context propagation middleware
  • Validation schemas — Pydantic models for query parameters, pagination cursors, and aggregation filters

Installation

pip install coderfastapi

Quick Start

Secure Router with JWT Authentication

from coderfastapi.lib.router import SecureRouter
from coderfastapi.lib.security.acl import ACLProvider
from coderfastapi.lib.security.policies.authentication.jwt import JWTAuthenticationPolicy
from coderfastapi.lib.security.policies.authorization.user import UserAuthorizationPolicy
from fastapi import FastAPI

app = FastAPI()
authentication_policy = JWTAuthenticationPolicy(secret_key="your-secret-key")
authorization_policy = UserAuthorizationPolicy(acl_provider=ACLProvider())
router = SecureRouter(authentication_policy, authorization_policy)

@router.get("/users", permission="public")
async def list_users(request):
    ...

app.include_router(router)

Session Management

from datetime import timedelta

from codercore.lib.redis import connection
from coderfastapi.lib.security import UserSessionManager

cache_connection = connection(host="localhost")
session_manager = UserSessionManager(
    cache_connection=cache_connection,
    session_ttl=timedelta(hours=24),
)

session = await session_manager.create_session(user_id=str(user.id))
retrieved = await session_manager.get_session_by_id(session.id)

Paginated Endpoints

from coderfastapi.lib.decorators import paginate
from coderfastapi.lib.validation.schemas.query import QueryParameters

@router.get("/items")
@paginate("id")
async def list_items(params: QueryParameters = Depends()):
    ...  # return list of items. Link headers are generated automatically

Requirements

  • Python 3.12+
  • Redis 7+ (required by session management only)

All other features — JWT authentication, authorization, pagination, cloud logging — work without Redis.

Documentation

Build and serve the API reference locally:

pip install -e ".[docs]"
mkdocs serve

Then visit http://127.0.0.1:8000. To build static HTML:

mkdocs build

Development

pip install -e ".[test,dev]"

Running Tests

Tests run against a real Redis instance via Docker Compose:

docker compose -f test-compose.yml up --build

Linting

This project uses pre-commit for linting:

pre-commit install
pre-commit run --all-files

License

Apache-2.0

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

coderfastapi-6.1.1.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

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

coderfastapi-6.1.1-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file coderfastapi-6.1.1.tar.gz.

File metadata

  • Download URL: coderfastapi-6.1.1.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for coderfastapi-6.1.1.tar.gz
Algorithm Hash digest
SHA256 19b635ca78f8ba4961ff7007aff9a0828e9f5508e58b5c8129debf7046c0a761
MD5 859455c5cd58d14348f19e76d63f263b
BLAKE2b-256 639185b11d99a85ce2b37ae8150c8ea802f04b6ddaf1c7b7f381771a248c39c5

See more details on using hashes here.

File details

Details for the file coderfastapi-6.1.1-py3-none-any.whl.

File metadata

  • Download URL: coderfastapi-6.1.1-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for coderfastapi-6.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 50e29d6ff582100885cdffec7448995097b7576c7f7b2237de9a9300b2952acf
MD5 f8d9cccc38f30422c8460cc5f71e15a2
BLAKE2b-256 23eb0efd6d58ab0a456908fc0ea42179e9021d4c60d7584de2892fda0e4d59aa

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