Catch "unknown password hashing algoritm errors"
authorMagnus Hagander <magnus@hagander.net>
Sat, 26 Dec 2015 17:39:02 +0000 (18:39 +0100)
committerMagnus Hagander <magnus@hagander.net>
Sat, 26 Dec 2015 17:51:00 +0000 (18:51 +0100)
These errors typically happens when someone tries to log in before they
have completed the process by clicking the link in the received email,
so give them an error message showing this.

pgweb/account/forms.py
pgweb/account/views.py

index eb1be83e258ec10c8ffdd15b979bc94c130369e2..117ee59916697f25267b8e5a72dba5a98e2e401d 100644 (file)
@@ -1,4 +1,5 @@
 from django import forms
+from django.contrib.auth.forms import AuthenticationForm
 
 import re
 
@@ -8,6 +9,25 @@ from pgweb.contributors.models import Contributor
 
 from recaptcha import ReCaptchaField
 
+import logging
+log = logging.getLogger(__name__)
+
+# Override some error handling only in the default authentication form
+class PgwebAuthenticationForm(AuthenticationForm):
+       def clean(self):
+               try:
+                       return super(PgwebAuthenticationForm, self).clean()
+               except ValueError, e:
+                       if e.message.startswith('Unknown password hashing algorithm'):
+                               # This is *probably* a user trying to log in with an account that has not
+                               # been set up properly yet. It could be an actually unsupported hashing
+                               # algorithm, but we'll deal with that when we get there.
+                               self._errors["__all__"] = self.error_class(["This account appears not to be properly initialized. Make sure you complete the signup process with the instructions in the email received before trying to use the account."])
+                               log.warning("User {0} tried to log in with invalid hash, probably because signup was completed.".format(self.cleaned_data['username']))
+                               return self.cleaned_data
+                       raise e
+
+
 class SignupForm(forms.Form):
        username = forms.CharField(max_length=30)
        first_name = forms.CharField(max_length=30)
index a6b7df06d5a853c2c627cb5fd7d6ad415f683534..40122425163dae2255840f72d5128bf16e614bb4 100644 (file)
@@ -31,6 +31,7 @@ from pgweb.downloads.models import Product
 from pgweb.profserv.models import ProfessionalService
 
 from models import CommunityAuthSite, EmailChangeToken
+from forms import PgwebAuthenticationForm
 from forms import SignupForm, UserForm, UserProfileForm, ContributorForm
 from forms import ChangeEmailForm
 
@@ -199,7 +200,8 @@ def orglist(request):
 
 @ssl_required
 def login(request):
-       return authviews.login(request, template_name='account/login.html')
+       return authviews.login(request, template_name='account/login.html',
+                                                  authentication_form=PgwebAuthenticationForm)
 
 @ssl_required
 def logout(request):