From eb47dc8e752964a72ffa61333179a811771318ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Voron?= Date: Tue, 21 Sep 2021 08:28:25 +0200 Subject: [PATCH] Pass user_manager instance in get_login_response/get_logout_response --- fastapi_users/authentication/base.py | 14 ++++++++++++-- fastapi_users/authentication/cookie.py | 14 ++++++++++++-- fastapi_users/authentication/jwt.py | 7 ++++++- fastapi_users/router/auth.py | 12 +++++++++--- fastapi_users/router/oauth.py | 2 +- tests/conftest.py | 8 ++++++-- tests/test_authentication_base.py | 8 ++++---- tests/test_authentication_cookie.py | 16 ++++++++++++---- tests/test_authentication_jwt.py | 10 ++++++---- 9 files changed, 68 insertions(+), 23 deletions(-) diff --git a/fastapi_users/authentication/base.py b/fastapi_users/authentication/base.py index 91d3c5db..38c0e9fc 100644 --- a/fastapi_users/authentication/base.py +++ b/fastapi_users/authentication/base.py @@ -34,8 +34,18 @@ class BaseAuthentication(Generic[T, models.UC, models.UD]): ) -> Optional[models.UD]: raise NotImplementedError() - async def get_login_response(self, user: models.UD, response: Response) -> Any: + async def get_login_response( + self, + user: models.UD, + response: Response, + user_manager: BaseUserManager[models.UC, models.UD], + ) -> Any: raise NotImplementedError() - async def get_logout_response(self, user: models.UD, response: Response) -> Any: + async def get_logout_response( + self, + user: models.UD, + response: Response, + user_manager: BaseUserManager[models.UC, models.UD], + ) -> Any: raise NotImplementedError() diff --git a/fastapi_users/authentication/cookie.py b/fastapi_users/authentication/cookie.py index 430307d7..9e316171 100644 --- a/fastapi_users/authentication/cookie.py +++ b/fastapi_users/authentication/cookie.py @@ -91,7 +91,12 @@ class CookieAuthentication( except UserNotExists: return None - async def get_login_response(self, user: models.UD, response: Response) -> Any: + async def get_login_response( + self, + user: models.UD, + response: Response, + user_manager: BaseUserManager[models.UC, models.UD], + ) -> Any: token = await self._generate_token(user) response.set_cookie( self.cookie_name, @@ -108,7 +113,12 @@ class CookieAuthentication( # so that FastAPI can terminate it properly return None - async def get_logout_response(self, user: models.UD, response: Response) -> Any: + async def get_logout_response( + self, + user: models.UD, + response: Response, + user_manager: BaseUserManager[models.UC, models.UD], + ) -> Any: response.delete_cookie( self.cookie_name, path=self.cookie_path, domain=self.cookie_domain ) diff --git a/fastapi_users/authentication/jwt.py b/fastapi_users/authentication/jwt.py index 6d7fd444..7bc6988a 100644 --- a/fastapi_users/authentication/jwt.py +++ b/fastapi_users/authentication/jwt.py @@ -67,7 +67,12 @@ class JWTAuthentication( except UserNotExists: return None - async def get_login_response(self, user: models.UD, response: Response) -> Any: + async def get_login_response( + self, + user: models.UD, + response: Response, + user_manager: BaseUserManager[models.UC, models.UD], + ) -> Any: token = await self._generate_token(user) return {"access_token": token, "token_type": "bearer"} diff --git a/fastapi_users/router/auth.py b/fastapi_users/router/auth.py index 7879ef0e..5b50f506 100644 --- a/fastapi_users/router/auth.py +++ b/fastapi_users/router/auth.py @@ -37,12 +37,18 @@ def get_auth_router( status_code=status.HTTP_400_BAD_REQUEST, detail=ErrorCode.LOGIN_USER_NOT_VERIFIED, ) - return await backend.get_login_response(user, response) + return await backend.get_login_response(user, response, user_manager) if backend.logout: @router.post("/logout") - async def logout(response: Response, user=Depends(get_current_user)): - return await backend.get_logout_response(user, response) + async def logout( + response: Response, + user=Depends(get_current_user), + user_manager: BaseUserManager[models.UC, models.UD] = Depends( + get_user_manager + ), + ): + return await backend.get_logout_response(user, response, user_manager) return router diff --git a/fastapi_users/router/oauth.py b/fastapi_users/router/oauth.py index 2c29dcdf..38b7fa39 100644 --- a/fastapi_users/router/oauth.py +++ b/fastapi_users/router/oauth.py @@ -112,6 +112,6 @@ def get_oauth_router( # Authenticate for backend in authenticator.backends: if backend.name == state_data["authentication_backend"]: - return await backend.get_login_response(user, response) + return await backend.get_login_response(user, response, user_manager) return router diff --git a/tests/conftest.py b/tests/conftest.py index c5edb47d..f7da369b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -452,10 +452,14 @@ class MockAuthentication(BaseAuthentication[str, UserCreate, UserDB]): return None return None - async def get_login_response(self, user: UserDB, response: Response): + async def get_login_response( + self, user: UserDB, response: Response, user_manager: BaseUserManager + ): return {"token": user.id} - async def get_logout_response(self, user: UserDB, response: Response): + async def get_logout_response( + self, user: UserDB, response: Response, user_manager: BaseUserManager + ): return None diff --git a/tests/test_authentication_base.py b/tests/test_authentication_base.py index 0ca6746c..fdb0649e 100644 --- a/tests/test_authentication_base.py +++ b/tests/test_authentication_base.py @@ -19,13 +19,13 @@ class TestAuthenticate: @pytest.mark.authentication @pytest.mark.asyncio -async def test_get_login_response(base_authentication, user): +async def test_get_login_response(base_authentication, user, user_manager): with pytest.raises(NotImplementedError): - await base_authentication.get_login_response(user, Response()) + await base_authentication.get_login_response(user, Response(), user_manager) @pytest.mark.authentication @pytest.mark.asyncio -async def test_get_logout_response(base_authentication, user): +async def test_get_logout_response(base_authentication, user, user_manager): with pytest.raises(NotImplementedError): - await base_authentication.get_logout_response(user, Response()) + await base_authentication.get_logout_response(user, Response(), user_manager) diff --git a/tests/test_authentication_cookie.py b/tests/test_authentication_cookie.py index 1fbbba95..b084a010 100644 --- a/tests/test_authentication_cookie.py +++ b/tests/test_authentication_cookie.py @@ -98,7 +98,9 @@ class TestAuthenticate: @pytest.mark.authentication @pytest.mark.asyncio -async def test_get_login_response(user, cookie_authentication: CookieAuthentication): +async def test_get_login_response( + user, cookie_authentication: CookieAuthentication, user_manager +): secret = cookie_authentication.secret path = cookie_authentication.cookie_path domain = cookie_authentication.cookie_domain @@ -106,7 +108,9 @@ async def test_get_login_response(user, cookie_authentication: CookieAuthenticat httponly = cookie_authentication.cookie_httponly response = Response() - login_response = await cookie_authentication.get_login_response(user, response) + login_response = await cookie_authentication.get_login_response( + user, response, user_manager + ) # We shouldn't return directly the response # so that FastAPI can terminate it properly @@ -148,9 +152,13 @@ async def test_get_login_response(user, cookie_authentication: CookieAuthenticat @pytest.mark.authentication @pytest.mark.asyncio -async def test_get_logout_response(user, cookie_authentication: CookieAuthentication): +async def test_get_logout_response( + user, cookie_authentication: CookieAuthentication, user_manager +): response = Response() - logout_response = await cookie_authentication.get_logout_response(user, response) + logout_response = await cookie_authentication.get_logout_response( + user, response, user_manager + ) # We shouldn't return directly the response # so that FastAPI can terminate it properly diff --git a/tests/test_authentication_jwt.py b/tests/test_authentication_jwt.py index 971a79c4..89422d9e 100644 --- a/tests/test_authentication_jwt.py +++ b/tests/test_authentication_jwt.py @@ -72,8 +72,10 @@ class TestAuthenticate: @pytest.mark.authentication @pytest.mark.asyncio -async def test_get_login_response(jwt_authentication, user): - login_response = await jwt_authentication.get_login_response(user, Response()) +async def test_get_login_response(jwt_authentication, user, user_manager): + login_response = await jwt_authentication.get_login_response( + user, Response(), user_manager + ) assert "access_token" in login_response assert login_response["token_type"] == "bearer" @@ -87,6 +89,6 @@ async def test_get_login_response(jwt_authentication, user): @pytest.mark.authentication @pytest.mark.asyncio -async def test_get_logout_response(jwt_authentication, user): +async def test_get_logout_response(jwt_authentication, user, user_manager): with pytest.raises(NotImplementedError): - await jwt_authentication.get_logout_response(user, Response()) + await jwt_authentication.get_logout_response(user, Response(), user_manager)