File tree Expand file tree Collapse file tree 3 files changed +46
-1
lines changed
Expand file tree Collapse file tree 3 files changed +46
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,10 @@ WGER_SETTINGS["ALLOW_GUEST_USERS"] = True
1515WGER_SETTINGS ["ALLOW_UPLOAD_VIDEOS" ] = False
1616WGER_SETTINGS ["MIN_ACCOUNT_AGE_TO_TRUST" ] = 21 # in days
1717WGER_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
1923DATABASES = {{
2024 'default' : {{
Original file line number Diff line number Diff line change 2222from django .conf import settings
2323from django .contrib import auth
2424from django .contrib .auth import login as django_login
25+ from django .contrib .auth .models import User
2526from django .utils .deprecation import MiddlewareMixin
2627from django .utils .functional import SimpleLazyObject
2728
@@ -62,8 +63,22 @@ 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+ username = request .META .get (auth_proxy_header )
71+ if username :
72+ user_query = User .objects .filter (username = username )
73+ if user_query .exists ():
74+ user = user_query .first ()
75+ else :
76+ user = User .objects .create_user (username )
77+ user .save ()
78+
79+ django_login (request , user , backend = 'django.contrib.auth.backends.ModelBackend' )
6580 # Django didn't find a user, so create one now
66- if (
81+ elif (
6782 settings .WGER_SETTINGS ['ALLOW_GUEST_USERS' ]
6883 and request .method == 'GET'
6984 and create_user
Original file line number Diff line number Diff line change 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" ), remote_user = "testuser" )
26+ self .assertEqual (response .status_code , 200 )
You can’t perform that action at this time.
0 commit comments