Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Don't authenticate deactivated users

Roland Geiderf06d8aa15500
diff --git a/wger/core/backends.py b/wger/core/backends.py
index 05f061499..60e431b85 100644
--- a/wger/core/backends.py
+++ b/wger/core/backends.py
@@ -90,13 +90,21 @@ class AuthProxyUserBackend(BaseBackend):
logger.error(f"AuthProxy: Error fetching user '{username}': {e}")
return None
+ if not user.is_active:
+ logger.warning(f"AuthProxy: User '{username}' is inactive, refusing login.")
+ return None
+
return user
def get_user(self, user_id):
"""
Standard Django method to retrieve a user by ID.
+
+ Inactive users are refused here as well, so that deactivating an
+ account also ends its existing proxy sessions.
"""
try:
- return User.objects.get(pk=user_id)
+ user = User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
+ return user if user.is_active else None
diff --git a/wger/core/tests/test_auth_proxy_middleware.py b/wger/core/tests/test_auth_proxy_middleware.py
index b9ff06f6e..f0aee8549 100644
--- a/wger/core/tests/test_auth_proxy_middleware.py
+++ b/wger/core/tests/test_auth_proxy_middleware.py
@@ -364,3 +364,44 @@ class AuthProxyMiddlewareTests(TestCase):
self.assertNotIn('_auth_user_id', self.client.session)
self.assertContains(response, 'login', status_code=200)
+
+ @override_settings(
+ AUTH_PROXY_TRUSTED_IPS=[TRUSTED_IP],
+ AUTH_PROXY_HEADER=PROXY_HEADER_KEY,
+ WGER_SETTINGS={'ALLOW_GUEST_USERS': False},
+ )
+ def test_inactive_user_is_refused(self):
+ """
+ A deactivated account cannot log in through the proxy, even if the
+ identity provider still lets the person through
+ """
+ self.existing_user.is_active = False
+ self.existing_user.save()
+
+ response = self.client.get(
+ self.login_url,
+ REMOTE_ADDR=TRUSTED_IP,
+ follow=True,
+ **{PROXY_HEADER_KEY: USERNAME},
+ )
+
+ self.assertNotIn('_auth_user_id', self.client.session)
+ self.assertEqual(response.status_code, 200)
+
+ @override_settings(
+ AUTH_PROXY_TRUSTED_IPS=[TRUSTED_IP],
+ AUTH_PROXY_HEADER=PROXY_HEADER_KEY,
+ WGER_SETTINGS={'ALLOW_GUEST_USERS': False},
+ )
+ def test_deactivation_ends_proxy_session(self):
+ """
+ Deactivating an account ends a proxy session that already exists
+ """
+ self.client.get(self.login_url, REMOTE_ADDR=TRUSTED_IP, **{PROXY_HEADER_KEY: USERNAME})
+ self.assertEqual(int(self.client.session['_auth_user_id']), self.existing_user.pk)
+
+ self.existing_user.is_active = False
+ self.existing_user.save()
+
+ response = self.client.get(self.protected_url)
+ self.assertFalse(response.wsgi_request.user.is_authenticated)