Revamp Transport so they always build a full Response object (#1049)

* Revamp Transport so they always build a full Response object

* Fix linting

* Add private methods to set cookies on CookieTransport

* Change on_after_login login_return parameter to response
This commit is contained in:
François Voron
2023-04-27 09:32:49 +02:00
committed by GitHub
parent 9a2515f56c
commit 8fd097cbc8
12 changed files with 65 additions and 71 deletions

View File

@ -1,6 +1,6 @@
from typing import Any, Generic
from typing import Generic
from fastapi import Response
from fastapi import Response, status
from fastapi_users import models
from fastapi_users.authentication.strategy import (
@ -40,27 +40,22 @@ class AuthenticationBackend(Generic[models.UP, models.ID]):
self.get_strategy = get_strategy
async def login(
self,
strategy: Strategy[models.UP, models.ID],
user: models.UP,
response: Response,
) -> Any:
self, strategy: Strategy[models.UP, models.ID], user: models.UP
) -> Response:
token = await strategy.write_token(user)
return await self.transport.get_login_response(token, response)
return await self.transport.get_login_response(token)
async def logout(
self,
strategy: Strategy[models.UP, models.ID],
user: models.UP,
token: str,
response: Response,
) -> Any:
self, strategy: Strategy[models.UP, models.ID], user: models.UP, token: str
) -> Response:
try:
await strategy.destroy_token(token, user)
except StrategyDestroyNotSupportedError:
pass
try:
await self.transport.get_logout_response(response)
response = await self.transport.get_logout_response()
except TransportLogoutNotSupportedError:
return None
response = Response(status_code=status.HTTP_204_NO_CONTENT)
return response