Skip to content

Commit 7a7a494

Browse files
committed
adds support for proxy auth header
Useful if you have authentication in front of wger, and want to use that instead of wgers authentication/signup methods.
1 parent 30871d6 commit 7a7a494

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

wger/settings.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ WGER_SETTINGS["ALLOW_GUEST_USERS"] = True
1515
WGER_SETTINGS["ALLOW_UPLOAD_VIDEOS"] = False
1616
WGER_SETTINGS["MIN_ACCOUNT_AGE_TO_TRUST"] = 21 # in days
1717
WGER_SETTINGS["EXERCISE_CACHE_TTL"] = 3600 # in seconds
18+
# can be used if there is authentication in front of wger, e.g.
19+
# if authelia is used to authenticate the users. Users will be
20+
# created with this username.
21+
# WGER_SETTINGS["AUTH_PROXY_HEADER"] = "Remote-User"
1822

1923
DATABASES = {{
2024
'default': {{

wger/utils/middleware.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from django.conf import settings
2323
from django.contrib import auth
2424
from django.contrib.auth import login as django_login
25+
from django.contrib.auth.models import User
2526
from django.utils.deprecation import MiddlewareMixin
2627
from django.utils.functional import SimpleLazyObject
2728

@@ -62,8 +63,25 @@ def get_user(request):
6263
if not request.session.get('has_demo_data'):
6364
request.session['has_demo_data'] = False
6465

66+
# if auth proxy header is setup, then create the user
67+
# as authentication has already happened.
68+
auth_proxy_header = settings.WGER_SETTINGS.get("AUTH_PROXY_HEADER")
69+
if auth_proxy_header:
70+
auth_proxy_header_django = "HTTP_" + auth_proxy_header.replace("-", "_").upper()
71+
username = request.META.get(auth_proxy_header_django)
72+
logger.debug(f'using auth_proxy_header "{auth_proxy_header}" got username "{username}"')
73+
74+
if username:
75+
user_query = User.objects.filter(username=username)
76+
if user_query.exists():
77+
user = user_query.first()
78+
else:
79+
user = User.objects.create_user(username)
80+
user.save()
81+
82+
django_login(request, user, backend='django.contrib.auth.backends.ModelBackend')
6583
# Django didn't find a user, so create one now
66-
if (
84+
elif (
6785
settings.WGER_SETTINGS['ALLOW_GUEST_USERS']
6886
and request.method == 'GET'
6987
and create_user
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Django
2+
from django.urls import reverse
3+
4+
# wger
5+
from wger.core.tests.base_testcase import WgerTestCase
6+
7+
8+
class ProxyAuthHeaderTestCase(WgerTestCase):
9+
"""
10+
Tests using proxy auth for authentication
11+
"""
12+
13+
def test_basic_auth_proxy_header(self):
14+
"""
15+
Tests that the proxy auth header works for authenticating
16+
the user
17+
"""
18+
with self.settings(
19+
WGER_SETTINGS={
20+
"AUTH_PROXY_HEADER": "Remote-User",
21+
"ALLOW_REGISTRATION": False,
22+
"ALLOW_GUEST_USERS": False,
23+
}
24+
):
25+
response = self.client.get(reverse("core:dashboard"), HTTP_REMOTE_USER="testuser")
26+
self.assertEqual(response.status_code, 200)

0 commit comments

Comments
 (0)