py-auth-fastapi
FastAPI integration for py-auth-core.
PyAuthFastAPI is an APIRouter. You pass it a configured PyAuth instance; it registers the HTTP routes, sets and clears cookies, and turns py-auth errors into FastAPI HTTPExceptions. It does not talk to the database — that stays on your adapter.
Requires Python 3.10+.
Install
pip install py-auth-fastapi
You also need py-auth-core and a storage adapter such as py-auth-sqlalchemy.
Quick start
from fastapi import FastAPI, Depends
from py_auth import PyAuth
from py_auth_fastapi import PyAuthFastAPI
auth = PyAuth(adapter=adapter, providers=[...])
app = FastAPI()
auth_router = PyAuthFastAPI(auth)
app.include_router(auth_router)
@app.get("/protected")
async def protected(session=Depends(auth_router.get_current_session())):
return {"user_id": session["user_id"]}
Defaults: prefix /auth, tags ["Authentication"]. Extra keyword arguments are forwarded to APIRouter. Cookie names and flags come from auth.cookies (configured on PyAuth, not here).
PyAuthFastAPI(auth, prefix="/api/auth", tags=["Auth"])
Routes
| Method | Path | CSRF | What it does |
|---|---|---|---|
GET |
/auth/csrf |
No | Returns a CSRF token. Sets the CSRF cookie if it is missing. |
POST |
/auth/signin/credentials |
Yes | JSON body → signin_with_credentials. Sets the session cookie. |
POST |
/auth/signout |
Yes | Deletes the session, then clears session and CSRF cookies. |
GET |
/auth/session |
No | Returns id, user_id, and expires for the current session. |
GET |
/auth/signin/google |
No | Redirects to Google. Stores state, PKCE verifier, and nonce in cookies. Requires GoogleProvider. |
GET |
/auth/google/callback |
No | Finishes Google sign-in, clears OAuth cookies, sets the session cookie. |
Mutating routes that require CSRF compare the CSRF cookie with the x-csrf-token header (double-submit). Fetch GET /auth/csrf first, then send the same value in that header.
On failure, the response looks like:
{
"detail": {
"success": false,
"message": "...",
"error": { "status_code": 401, "code": "InvalidSession", "details": {} }
}
}
Protecting your own routes
get_current_session() is a FastAPI dependency factory. It reads the session cookie, calls verify_session, and injects the session dict (id, user_id, expires, …). Invalid or missing sessions become HTTP errors.
Use add_csrf_verification=True on endpoints that change state:
@app.post("/profile")
async def update_profile(
session=Depends(auth_router.get_current_session(add_csrf_verification=True)),
):
...
verify_csrf() is available on its own if you only need the CSRF check.
License
MIT
Metadata
Release files for py-auth-fastapi 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py_auth_fastapi-0.0.2.tar.gz | 7.3 kB | Without provenance | Details for py_auth_fastapi-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py_auth_fastapi-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for py_auth_fastapi-0.0.2-py3-none-any.whl |
Total release size: 14.2 kB
Release files / py_auth_fastapi-0.0.2.tar.gz
| Download URL | py_auth_fastapi-0.0.2.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c93a2949bd61d9b4d46156c288ea6431f0b841a69b9eb92b658d2a272ffdae86
|
|
BLAKE2b-256 checksum How to use checksums |
2ca828c922e9c7e19f7a4c5c694967640bfa9742036233c5b48f4c729ba62773
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.6
|
Release files / py_auth_fastapi-0.0.2-py3-none-any.whl
| Download URL | py_auth_fastapi-0.0.2-py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8a2749e3b54b91874f74b2ac8f92fba4d8fa28cf3c99b5bb25b669196ecf6129
|
|
BLAKE2b-256 checksum How to use checksums |
d4088497c807ebfcc3e2257200cf2f39af88ad97cbefd38523bc331f37cc4be8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.6
|
Security for py-auth-fastapi 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.