/
durovcode
/
backend
Обзор
Документация
Войти
/
durovcode
/
backend
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
master
app/api/csrf/main.py
57 строк
1 KB
quyxishi
implemented account profile photo feature
21 сен 2024, 14:52
21 сен 2024, 14:52
00784c0
Код
Авторство
О чём код?
# from typing import TypeVar # from fastapi import Request # from fastapi.responses import JSONResponse # from fastapi_csrf_protect import CsrfProtect # from pydantic import BaseModel # from app.env import CSRF_SECRET_KEY # _T = TypeVar('_T') # class CsrfSettings(BaseModel): # secret_key: str = CSRF_SECRET_KEY # cookie_key: str = 'csrf-token' # header_name: str = 'csrf-token' # cookie_samesite: str = 'strict' # # cookie_secure: bool = True todo! # httponly: bool = True # @CsrfProtect.load_config # def csrf_load_config() -> CsrfSettings: # return CsrfSettings() # def action_validate_csrf_decorator(f: _T) -> _T: # async def _wrapper(*args) -> ...: # csrf = CsrfProtect() # request: Request = args[1] # await csrf.validate_csrf(request=request) # response: _T = await f(*args) # csrf.unset_csrf_cookie(response=response) # return response # return _wrapper # class CsrfAction: # def __init__(self) -> None: # pass # async def __call__(self) -> JSONResponse: # csrf = CsrfProtect() # csrf_token, csrf_signed = csrf.generate_csrf_tokens() # response = JSONResponse( # status_code=200, content={csrf._header_name: csrf_token} # ) # csrf.set_csrf_cookie(csrf_signed_token=csrf_signed, response=response) # return response