mirror of https://github.com/tiangolo/fastapi.git
Update csrf.py
fix: update type annotations to pass mypy checks
This commit is contained in:
parent
ef75891358
commit
2b731085ad
|
|
@ -2,8 +2,7 @@ import functools
|
|||
import http.cookies
|
||||
import secrets
|
||||
from re import Pattern
|
||||
from typing import Any, Dict, List, Optional, Set
|
||||
|
||||
from typing import Dict, List, Optional, Set, Any, cast, Callable, Coroutine
|
||||
from itsdangerous import BadSignature
|
||||
from itsdangerous.url_safe import URLSafeSerializer
|
||||
from starlette.datastructures import URL, MutableHeaders
|
||||
|
|
@ -134,10 +133,11 @@ class CSRFMiddleware:
|
|||
async def _get_csrf_token_form(self, request: Request) -> str:
|
||||
form = await request.form()
|
||||
csrf_token = form.get(self.cookie_name, "")
|
||||
return csrf_token
|
||||
return cast(str, csrf_token)
|
||||
|
||||
def _generate_csrf_token(self) -> str:
|
||||
return self.serializer.dumps(secrets.token_urlsafe(128))
|
||||
token = self.serializer.dumps(secrets.token_urlsafe(128))
|
||||
return cast(str, token)
|
||||
|
||||
def _csrf_tokens_match(self, token1: str, token2: str) -> bool:
|
||||
try:
|
||||
|
|
@ -152,10 +152,9 @@ class CSRFMiddleware:
|
|||
content="CSRF token verification failed", status_code=403
|
||||
)
|
||||
|
||||
def _receive_with_body(self, receive, body) -> dict:
|
||||
async def inner() -> dict:
|
||||
def _receive_with_body(self, receive: Any, body: bytes) -> Callable[[], Coroutine[Any, Any, Dict[str, Any]]]:
|
||||
async def inner() -> dict :
|
||||
return {"type": "http.request", "body": body, "more_body": False}
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
|
|
@ -171,5 +170,4 @@ def csrf_token_processor(csrf_cookie_name: str, csrf_header_name: str):
|
|||
"csrf_input": csrf_input,
|
||||
"csrf_header": csrf_header,
|
||||
}
|
||||
|
||||
return processor
|
||||
|
|
|
|||
Loading…
Reference in New Issue