|
13 | 13 | """
|
14 | 14 |
|
15 | 15 | ### Standard packages ###
|
16 |
| -from typing import Tuple |
| 16 | +from typing import Any, Callable, ClassVar, Optional, Sequence, Tuple |
| 17 | + |
| 18 | +### Third-party packages ### |
| 19 | +from starlette.datastructures import Headers |
| 20 | +from starlette.requests import Request |
| 21 | +from starlette.responses import Response |
17 | 22 |
|
18 | 23 | ### Local modules ###
|
19 |
| -from fastapi_csrf_protect.core import CsrfProtect |
| 24 | +from fastapi_csrf_protect.core import CsrfProtect as CsrfProtectImpl |
| 25 | + |
| 26 | +class CsrfProtect: |
| 27 | + impl: ClassVar[CsrfProtectImpl] = CsrfProtectImpl() |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def load_config(cls, settings: Callable[..., Sequence[Tuple[str, Any]]]) -> None: |
| 31 | + cls.impl.load_config(settings) |
| 32 | + |
| 33 | + def generate_csrf_tokens(self, secret_key: Optional[str] = None) -> Tuple[str, str]: |
| 34 | + return self.impl.generate_csrf_tokens(secret_key) |
| 35 | + |
| 36 | + def get_csrf_from_body(self, data: bytes) -> str: |
| 37 | + return self.impl.get_csrf_from_body(data) |
| 38 | + |
| 39 | + def get_csrf_from_headers(self, headers: Headers) -> str: |
| 40 | + return self.impl.get_csrf_from_headers(headers) |
| 41 | + |
| 42 | + def set_csrf_cookie(self, csrf_signed_token: str, response: Response) -> None: |
| 43 | + self.impl.set_csrf_cookie(csrf_signed_token, response) |
| 44 | + |
| 45 | + def unset_csrf_cookie(self, response: Response) -> None: |
| 46 | + self.impl.unset_csrf_cookie(response) |
| 47 | + |
| 48 | + async def validate_csrf( |
| 49 | + self, |
| 50 | + request: Request, |
| 51 | + cookie_key: Optional[str] = None, |
| 52 | + secret_key: Optional[str] = None, |
| 53 | + time_limit: Optional[int] = None, |
| 54 | + ) -> None: |
| 55 | + await self.impl.validate_csrf(request, cookie_key, secret_key, time_limit) |
| 56 | + |
20 | 57 |
|
21 | 58 | __all__: Tuple[str, ...] = ("CsrfProtect",)
|
22 | 59 | __name__ = "fastapi-csrf-protect"
|
|
0 commit comments