diff --git a/core/web/apiv2/auth.py b/core/web/apiv2/auth.py index 5bb1c4e27..ab9b96b45 100644 --- a/core/web/apiv2/auth.py +++ b/core/web/apiv2/auth.py @@ -289,6 +289,13 @@ async def oidc_callback(request: Request) -> RedirectResponse: detail="User account disabled. Please contact your server admin.", ) + # Also covers every login after the first, until an admin enables the user. + if not db_user.enabled: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="User account disabled. Please contact your server admin.", + ) + access_token = create_access_token( data={"sub": db_user.username, "enabled": db_user.enabled}, expires_delta=BROWSER_TOKEN_EXPIRE_DELTA, diff --git a/tests/apiv2/auth.py b/tests/apiv2/auth.py index d2db6ddb4..af35173da 100644 --- a/tests/apiv2/auth.py +++ b/tests/apiv2/auth.py @@ -437,6 +437,20 @@ def test_unverified_email(self) -> None: response.json()["detail"], ) + def test_disabled_user_gets_no_session(self) -> None: + """A first login creates a disabled user. Until an admin enables it, + every later login is refused as well, with no session cookie.""" + for attempt in ("first", "second"): + with self.subTest(login=attempt): + response = self.browser_login({"email": "bob@example.com"}) + self.assertEqual(response.status_code, 401) + self.assertNotIn("set-cookie", response.headers) + self.assertEqual( + response.json()["detail"], + "User account disabled. Please contact your server admin.", + ) + self.assertFalse(UserSensitive.find(username="bob@example.com").enabled) + def test_unverified_email_creates_no_user(self) -> None: self.browser_login({"email": "mallory@example.com", "email_verified": False}) self.assertIsNone(UserSensitive.find(username="mallory@example.com"))