Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Really fix #1163

Roland Geiderecf5f4e8102e
diff --git a/wger/core/api/views.py b/wger/core/api/views.py
index c947a1990..db142ff0a 100644
--- a/wger/core/api/views.py
+++ b/wger/core/api/views.py
@@ -223,7 +223,8 @@ class UserAPILoginView(viewsets.ViewSet):
username = serializer.data["username"]
# Try to retrieve the user
- form = UserLoginForm(data=serializer.data)
+ form = UserLoginForm(data=serializer.data, authenticate_on_clean=False)
+
if not form.is_valid():
logger.info(f"Tried logging via API with unknown user: '{username}'")
return Response(
@@ -231,6 +232,7 @@ class UserAPILoginView(viewsets.ViewSet):
status=status.HTTP_401_UNAUTHORIZED,
)
+ form.authenticate(request)
token = create_token(form.get_user())
return Response(
data={
diff --git a/wger/core/forms.py b/wger/core/forms.py
index 105b7bf4b..cb8ce1784 100644
--- a/wger/core/forms.py
+++ b/wger/core/forms.py
@@ -58,9 +58,13 @@ class UserLoginForm(AuthenticationForm):
Form for logins
"""
- def __init__(self, *args, **kwargs):
+ authenticate_on_clean = True
+
+ def __init__(self, authenticate_on_clean=True, *args, **kwargs):
super(UserLoginForm, self).__init__(*args, **kwargs)
+ self.authenticate_on_clean = authenticate_on_clean
+
self.helper = FormHelper()
self.helper.add_input(Submit('submit', _('Login'), css_class='btn-success btn-block'))
self.helper.form_class = 'wger-form'
@@ -74,26 +78,31 @@ class UserLoginForm(AuthenticationForm):
def clean(self):
"""
- Note this clean method is the same as the one from django/contrib/auth/forms.py,
- but we pass the request as an explicit request parameter. Otherwise, django
- axes won't work
+ Note: this clean method needs to be able to toggle authenticating directly
+ or not. This is needed because django axes expects an explicit request
+ parameter and otherwise the login endpoint won't work
See https://github.com/wger-project/wger/issues/1163
"""
+ if self.authenticate_on_clean:
+ self.authenticate(self.request)
+ return self.cleaned_data
+
+ def authenticate(self, request):
username = self.cleaned_data.get("username")
password = self.cleaned_data.get("password")
- if username is not None and password:
+ if username and password:
self.user_cache = authenticate(
- request=self.request, username=username, password=password
+ request=request,
+ username=username,
+ password=password,
)
if self.user_cache is None:
raise self.get_invalid_login_error()
else:
self.confirm_login_allowed(self.user_cache)
- return self.cleaned_data
-
class UserPreferencesForm(forms.ModelForm):
first_name = forms.CharField(label=_('First name'), required=False)