mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Pass user_manager instance in get_login_response/get_logout_response
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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"}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user