You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2019/10/25 19:58:35 UTC

[allura] 01/01: [#8337] show more helpful messages when username is wrong format

This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch db/8337
in repository https://gitbox.apache.org/repos/asf/allura.git

commit c21e8fe2ef523eddc4112df7caade032fdb2d9f6
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Fri Oct 25 15:58:07 2019 -0400

    [#8337] show more helpful messages when username is wrong format
---
 Allura/allura/lib/plugin.py                 | 12 ++++++++++++
 Allura/allura/lib/widgets/auth_widgets.py   | 10 +++++++++-
 Allura/allura/lib/widgets/forms.py          |  8 ++------
 Allura/allura/tests/functional/test_auth.py | 10 ++++++++++
 4 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 1fde18c..00d6128 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -45,6 +45,7 @@ from tg import config, request, redirect, response, flash
 from tg import tmpl_context as c, app_globals as g
 from webob import exc, Request
 from paste.deploy.converters import asbool, asint
+from formencode import validators as fev
 
 from ming.utils import LazyProperty
 from ming.orm import state
@@ -479,6 +480,17 @@ class AuthenticationProvider(object):
 
         return False
 
+    def username_validator(self, long_message=True):
+        validator = fev.Regex(h.re_project_name)
+        if long_message:
+            validator._messages['invalid'] = (
+                'Usernames must include only small letters, numbers, and dashes.'
+                ' They must also start with a letter and be at least 3 characters'
+                ' long.')
+        else:
+            validator._messages['invalid'] = 'Usernames only include small letters, numbers, and dashes'
+        return validator
+
 
 class LocalAuthenticationProvider(AuthenticationProvider):
 
diff --git a/Allura/allura/lib/widgets/auth_widgets.py b/Allura/allura/lib/widgets/auth_widgets.py
index 3233015..47c6764 100644
--- a/Allura/allura/lib/widgets/auth_widgets.py
+++ b/Allura/allura/lib/widgets/auth_widgets.py
@@ -21,6 +21,7 @@ from ew.core import validator
 
 from tg import request, tmpl_context as c
 from formencode import Invalid
+from formencode import validators as fev
 from webob import exc
 
 from .forms import ForgeForm
@@ -69,8 +70,15 @@ class LoginForm(ForgeForm):
 
     @validator
     def validate(self, value, state=None):
+        super(LoginForm, self).validate(value, state=state)
+        auth_provider = plugin.AuthenticationProvider.get(request)
+
+        # can't use a validator attr on the username TextField, since the antispam encoded name changes and doesn't
+        # match the name used in the form submission
+        auth_provider.username_validator(long_message=False).to_python(value['username'])
+
         try:
-            plugin.AuthenticationProvider.get(request).login()
+            auth_provider.login()
         except exc.HTTPUnauthorized:
             msg = 'Invalid login'
             raise Invalid(
diff --git a/Allura/allura/lib/widgets/forms.py b/Allura/allura/lib/widgets/forms.py
index 68254d9..930b6bc 100644
--- a/Allura/allura/lib/widgets/forms.py
+++ b/Allura/allura/lib/widgets/forms.py
@@ -778,12 +778,8 @@ class RegistrationForm(ForgeForm):
         username = ew.TextField(
             name='username',
             label='Desired Username',
-            validator=fev.Regex(
-                h.re_project_name))
-        username.validator._messages['invalid'] = (
-            'Usernames must include only small letters, numbers, and dashes.'
-            ' They must also start with a letter and be at least 3 characters'
-            ' long.')
+            validator=plugin.AuthenticationProvider.get(None).username_validator(),
+        )
         fields = [
             ew.TextField(
                 name='display_name',
diff --git a/Allura/allura/tests/functional/test_auth.py b/Allura/allura/tests/functional/test_auth.py
index b8066b6..a7c6893 100644
--- a/Allura/allura/tests/functional/test_auth.py
+++ b/Allura/allura/tests/functional/test_auth.py
@@ -100,6 +100,16 @@ class TestAuth(TestController):
             _session_id=self.app.cookies['_session_id']))
         assert 'Invalid login' in str(r), r.showbrowser()
 
+    def test_login_invalid_username(self):
+        extra = {'username': '*anonymous'}
+        r = self.app.get('/auth/', extra_environ=extra)
+        f = r.forms[0]
+        encoded = self.app.antispam_field_names(f)
+        f[encoded['username']] = 'test@user.com'
+        f[encoded['password']] = 'foo'
+        r = f.submit(extra_environ={'username': '*anonymous'})
+        r.mustcontain('Usernames only include small letters, ')
+
     def test_login_diff_ips_ok(self):
         # exercises AntiSpam.validate methods
         extra = {'username': '*anonymous', 'REMOTE_ADDR': '11.22.33.44'}