Simple integration of Cross-Site Request Forgery (XSRF) Protection by using either Cookies or Context combined with Headers
Project description
FastAPI CSRF Protect
Features
FastAPI extension that provides Cross-Site Request Forgery (XSRF) Protection support (easy to use and lightweight).
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
Usage
With Context and Headers
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'
@CsrfProtect.load_config
def get_csrf_config():
return CsrfSettings()
@app.get('/form')
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
})
return response
@app.post('/posts', response_class=JSONResponse)
def create_post(request: Request, csrf_protect:CsrfProtect = Depends()):
'''
Creates a new Post
'''
csrf_token = csrf_protect.get_csrf_from_headers(request.headers)
csrf_protect.validate_csrf(csrf_token)
# 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
}
)
With Cookies
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'
@CsrfProtect.load_config
def get_csrf_config():
return CsrfSettings()
@app.get('/form')
def form(request: Request, csrf_protect:CsrfProtect = Depends()):
'''
Returns form template.
'''
response = templates.TemplateResponse('form.html', { 'request': request })
csrf_protect.set_csrf_cookie(response)
return response
@app.post('/posts', response_class=JSONResponse)
def create_post(request: Request, csrf_protect:CsrfProtect = Depends()):
'''
Creates a new Post
'''
csrf_protect.validate_csrf_in_cookies(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 })
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
Close
Hashes for fastapi-csrf-protect-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b1935cdc27a48013ffbb25246af677a6ffdab44043b48104c6efa924e5eac37 |
|
MD5 | 64dc82805ea83d0afa57760cc18a11d5 |
|
BLAKE2b-256 | e8104be80d3de5e84b5b7ce7d3324c6762cb3c2c74e6a2904d87d15de1ed8c21 |
Close
Hashes for fastapi_csrf_protect-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 969bd1f122aae14f14d664d5dfc932512db69f6dadf16c634041b061cf19743e |
|
MD5 | 32146c4b8d8173acaf6e0ec7e5c8b303 |
|
BLAKE2b-256 | b021921b5cc253f9529c95fbadc529592d4e48f780baf3ba81e6f5011417d3d6 |