Skip to content

Commit

Permalink
try new approach with only ascii lowercase + numbers valid for new users
Browse files Browse the repository at this point in the history
  • Loading branch information
dsschult committed Jan 5, 2024
1 parent fa01f60 commit 2136916
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
14 changes: 12 additions & 2 deletions tests/test_api_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,19 @@ async def test_username_select(server, reg_token_client):
'fo=o', # invalid char
'fo o', # space
'f\'oo', # quote
'f-oo', # dash
'f.oo', # dot
'f_oo', # underscore
'Foo', # uppercase
'foO', # uppercase
]
# put is less strict
invalid_usernames_put = [
'foò', # unicode
'fo=o', # invalid char
'fo o', # space
'f\'oo', # quote
]
# put is fine with lengths, so ignore those errors
invalid_usernames_put = invalid_usernames[2:]

@pytest.mark.parametrize('username', invalid_usernames_put)
@pytest.mark.asyncio
Expand Down
2 changes: 1 addition & 1 deletion user_mgmt/static/routes/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export default {
<textinput name="Last Name" inputName="last_name" v-model.trim="lastName"
required=true :valid="validLastName" :allValid="valid"></textinput>
<textinput name="Username" inputName="username" v-model.trim="username"
required=true :valid="validUsername" :allValid="valid" helptext="Note: must be between 5-16 characters"></textinput>
required=true :valid="validUsername" :allValid="valid" helptext="Note: must be between 5-16 characters, ascii lowercase and numbers"></textinput>
<textinput name="External Email Address" inputName="email" v-model.trim="email"
required=true :valid="validEmail" :allValid="valid"></textinput>
<div v-if="errMessage" class="error_box" v-html="errMessage"></div>
Expand Down
19 changes: 8 additions & 11 deletions user_mgmt/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Handle user profile updates.
"""
import itertools
import re
import os
import logging
import os
import string

from tornado.web import HTTPError
from rest_tools.server import catch_error, authenticated
Expand Down Expand Up @@ -74,7 +74,7 @@ class Username(MyHandler):
@staticmethod
def _gen_username(first_name, last_name, number):
"""Make ascii username from first and last name."""
ret = unidecode.unidecode(first_name[0] + last_name).replace("'", '').replace(' ', '').lower()
ret = unidecode.unidecode(first_name[0] + last_name).replace("'", '').replace(' ', '').replace('.','').lower()
if len(ret) < 5:
ret = f'{ret:0<5s}'
if len(ret) > 8:
Expand All @@ -89,20 +89,17 @@ def _username_valid(username):
Check if a username is valid.
Valid:
* ascii string between 4-16 chars
* letters, numbers, -, ., _
* ascii string between 5-15 chars
* lowercase letters, numbers
Invalid:
* unicode
* quotes
* spaces
* punctuation
* special chars
* BAD_WORDS filter
"""
ascii_username = unidecode.unidecode(username).replace("'", '').replace(' ', '').lower()
if ascii_username != username:
return False
if not re.fullmatch(r'[\w\-\._]+', username):
valid_chars = string.ascii_lowercase + string.digits
if any(c not in valid_chars for c in username):
return False
if len(username) < 5:
return False
Expand Down

0 comments on commit 2136916

Please sign in to comment.