Shared CSRF, security headers, logging, observability, flash messages, role-cleanup, and Auth client for Coffee House FastAPI apps
Project description
coffeehouse-common
Shared Python package for Coffee House ecosystem FastAPI apps:
coffeehouse_common.csrf—CSRFMiddlewaresubclass (form body + header + safe body replay)coffeehouse_common.security_middleware—SecurityHeadersMiddleware(raw ASGI)coffeehouse_common.logging_config—RequestIdMiddleware(raw ASGI),RequestIdFilter,configure_logging,get_request_idcoffeehouse_common.observability—init_sentry(app_slug)Sentry SDK bootstrap; readsSENTRY_DSNfrom env (no-op when unset), tags events withapp_slug, drops 4xx HTTP/CSRF noise, keeps 5xx
v0.3.0+ implements security headers and request ID middleware as raw ASGI (not BaseHTTPMiddleware) so streaming responses are not fully buffered. v0.3.1 sets X-Frame-Options: SAMEORIGIN (same-origin iframes allowed; third-party framing blocked). Logs include a per-request correlation id when configure_logging() + RequestIdMiddleware are used.
Install
From GitHub (pinned tag):
pip install "coffeehouse-common @ git+https://github.com/rubencfh/coffeehouse-common.git@v0.9.0"
Local editable install:
pip install -e ../coffeehouse-common
Development
Set up the repo for local development + testing:
git clone https://github.com/rubencfh/coffeehouse-common.git
cd coffeehouse-common
pip install -e ".[testing]"
pip install -r requirements-dev.txt
pre-commit install
Run the full test suite (with coverage, fails under 70%):
pytest
Run the pre-commit hooks against all files (format, lint, secret scan, syntax check):
pre-commit run --all-files
Individual checks:
ruff format --check . # formatting
ruff check . # linting
mypy coffeehouse_common # type-checking (lenient baseline)
bandit -c pyproject.toml -r coffeehouse_common coffeehouse_testing # SAST
pip-audit -r requirements-dev.txt # SCA
CI runs the same checks on every push / PR via .github/workflows/ci.yml.
See coffeehouse-ecosystem/docs/pipeline.md for the broader pipeline.
Shared test fixtures
The coffeehouse_testing subpackage ships with this repo and provides shared
pytest fixtures that consumer apps import in their own tests/conftest.py:
from coffeehouse_testing.jwt_factory import jwt_factory
from coffeehouse_testing.client import authed_client
from coffeehouse_testing.db import postgres_container, clean_db # noqa: F401
from coffeehouse_testing.auth_stub import make_auth_stub, build_roles_manifest
All heavy dependencies (PyJWT, testcontainers, starlette) are loaded lazily — import the modules you need in tests and the import errors only fire if the underlying package is not installed.
Usage
from coffeehouse_common.csrf import CSRFMiddleware
from coffeehouse_common.logging_config import RequestIdMiddleware, configure_logging
from coffeehouse_common.observability import init_sentry
from coffeehouse_common.security_middleware import SecurityHeadersMiddleware
configure_logging() # once at startup; attaches RequestIdFilter for %(request_id)s
init_sentry(app_slug="auth") # no-op if SENTRY_DSN is unset
init_sentry reads SENTRY_DSN, RAILWAY_ENVIRONMENT_NAME (or ENVIRONMENT),
and RAILWAY_GIT_COMMIT_SHA from the environment. Errors-only sample rate by
default (traces_sample_rate=0.0). 401/403/404/405 responses, 4xx
HTTPExceptions, and CSRFErrors are dropped client-side so they don't burn
the Sentry quota. 5xx HTTPExceptions and any other exception class are kept.
Pass extra_ignored_status_codes= / extra_ignored_exceptions= to extend the
filter per app.
Explicit dependency: itsdangerous (CSRF token signing).
See CONVENTIONS.md in the coffeehouse-ecosystem repo for middleware order and CSRF patterns.
First-time publish (maintainers)
- Create an empty repo on GitHub:
rubencfh/coffeehouse-common - From this directory:
git remote add origin https://github.com/rubencfh/coffeehouse-common.git
git push -u origin master
git push origin v0.9.0
Until the repo and tag exist, app Docker builds that pip install from GitHub will fail. For local dev, use editable install (above).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file coffeehouse_common-0.15.1.tar.gz.
File metadata
- Download URL: coffeehouse_common-0.15.1.tar.gz
- Upload date:
- Size: 50.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be0860baed442b3d4a2ea9f785064fab7547370023105e79cca824cedd21be86
|
|
| MD5 |
386b40a75e93cf2c532b6a5524233874
|
|
| BLAKE2b-256 |
a0706a4224ddb0837fda73896632b6ccfc60b61f1c44becc2f4175cd72fec423
|
Provenance
The following attestation bundles were made for coffeehouse_common-0.15.1.tar.gz:
Publisher:
release.yml on coffeehouse-tools/coffeehouse-common
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
coffeehouse_common-0.15.1.tar.gz -
Subject digest:
be0860baed442b3d4a2ea9f785064fab7547370023105e79cca824cedd21be86 - Sigstore transparency entry: 1632828370
- Sigstore integration time:
-
Permalink:
coffeehouse-tools/coffeehouse-common@737178a0e056e1dfa30169a1faa179433e53f0df -
Branch / Tag:
refs/tags/v0.15.1 - Owner: https://github.com/coffeehouse-tools
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@737178a0e056e1dfa30169a1faa179433e53f0df -
Trigger Event:
push
-
Statement type:
File details
Details for the file coffeehouse_common-0.15.1-py3-none-any.whl.
File metadata
- Download URL: coffeehouse_common-0.15.1-py3-none-any.whl
- Upload date:
- Size: 36.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4eb983cf2d5a1ed5062ab0165f0334ba4d286c9f8a0713aee751ceea1610c636
|
|
| MD5 |
3a5a87395f0d3d8f39e598a00a8ce2e8
|
|
| BLAKE2b-256 |
bb32bc4c847af3ba74cc81bb6fde72587d1dfe4e52f71937bb72b5b4842c29a1
|
Provenance
The following attestation bundles were made for coffeehouse_common-0.15.1-py3-none-any.whl:
Publisher:
release.yml on coffeehouse-tools/coffeehouse-common
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
coffeehouse_common-0.15.1-py3-none-any.whl -
Subject digest:
4eb983cf2d5a1ed5062ab0165f0334ba4d286c9f8a0713aee751ceea1610c636 - Sigstore transparency entry: 1632828378
- Sigstore integration time:
-
Permalink:
coffeehouse-tools/coffeehouse-common@737178a0e056e1dfa30169a1faa179433e53f0df -
Branch / Tag:
refs/tags/v0.15.1 - Owner: https://github.com/coffeehouse-tools
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@737178a0e056e1dfa30169a1faa179433e53f0df -
Trigger Event:
push
-
Statement type: