Stateless implementation of Cross-Site Request Forgery (XSRF) Protection by using Double Submit Cookie mitigation pattern
Project description
FastAPI CSRF Protect
Features
FastAPI extension that provides stateless Cross-Site Request Forgery (XSRF) Protection support.
Aimed to be easy to use and lightweight, we adopt Double Submit Cookie mitigation pattern.
If you were familiar with flask-wtf
library this extension suitable for you.
This extension inspired by fastapi-jwt-auth
😀
- Storing
fastapi-csrf-token
in cookies or serve it in template's context
Installation
The easiest way to start working with this extension with pip
pip install fastapi-csrf-protect
# or
poetry add fastapi-csrf-protect
Getting Started
The following examples show you how to integrate this extension to a FastAPI App
Example Login Form
from fastapi import FastAPI, Request, Depends
from fastapi.responses import JSONResponse
from fastapi.templating import Jinja2Templates
from fastapi_csrf_protect import CsrfProtect
from fastapi_csrf_protect.exceptions import CsrfProtectError
from pydantic import BaseModel
app = FastAPI()
templates = Jinja2Templates(directory="templates")
class CsrfSettings(BaseModel):
secret_key: str = "asecrettoeverybody"
cookie_samesite: str = "none"
@CsrfProtect.load_config
def get_csrf_config():
return CsrfSettings()
@app.get("/login")
def form(request: Request, csrf_protect: CsrfProtect = Depends()):
"""
Returns form template.
"""
csrf_token = csrf_protect.generate_csrf()
response = templates.TemplateResponse(
"form.html", {"request": request, "csrf_token": csrf_token}
)
csrf_protect.set_csrf_cookie(csrf_token, response)
return response
@app.post("/login", response_class=JSONResponse)
def create_post(request: Request, csrf_protect: CsrfProtect = Depends()):
"""
Creates a new Post
"""
csrf_protect.validate_csrf(request)
# Do stuff
@app.exception_handler(CsrfProtectError)
def csrf_protect_exception_handler(request: Request, exc: CsrfProtectError):
return JSONResponse(status_code=exc.status_code, content={"detail": exc.message})
Contributions
To contribute to the project, fork the repository and clone to your local device and install preferred testing dependency pytest Alternatively, run the following command on your terminal to do so:
pip install -U poetry
poetry install
Testing can be done by the following command post-installation:
poetry install --with test
pytest
Run Examples
To run the provided examples, first you must install extra dependencies uvicorn and jinja2 Alternatively, run the following command on your terminal to do so
poetry install --with examples
Running the example utilizing Context and Headers
uvicorn examples.login:app
License
This project is licensed under the terms of the MIT license.
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
File details
Details for the file fastapi-csrf-protect-0.3.0.tar.gz
.
File metadata
- Download URL: fastapi-csrf-protect-0.3.0.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.9.13 Darwin/22.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96734df60ad852ffd452eac03630c8e84b8bf8e207460f0b01987d2867ac656e |
|
MD5 | 822fff2670f125c2cfbe27687f961f5e |
|
BLAKE2b-256 | 42c6859b0ea3eda30bba23da8758f8d67b4afa67f5ff3c1911e635175022169b |
File details
Details for the file fastapi_csrf_protect-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_csrf_protect-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.9.13 Darwin/22.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bc758b16b80632a3476420a2d9e395e2474aacdde15f1faa5700ca6c93b35b6 |
|
MD5 | 10987fce7a879505247251996cf40c7e |
|
BLAKE2b-256 | 4f9df1b22d863d9a19c99082ebcfbc02549ed3b5d73deb80dee302715a47afbf |