Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow numbers in usernames #224

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ocflib/account/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def extract_username_from_principal(principal):
'ckuehl'
"""

REGEX = '^([a-z]{3,16})(/[a-z]*)?@OCF\\.BERKELEY\\.EDU$'
REGEX = '^([a-z0-9]{3,16})(/[a-z]*)?@OCF\\.BERKELEY\\.EDU$'
ja5087 marked this conversation as resolved.
Show resolved Hide resolved
match = re.match(REGEX, principal)

if not match:
Expand Down
7 changes: 5 additions & 2 deletions ocflib/account/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,11 @@ def validate_username(username, check_exists=False):
if not 3 <= len(username) <= 16:
raise ValueError('Username must be between 3 and 16 characters.')

if not all(c.islower() for c in username):
raise ValueError('Username must be all lowercase letters.')
if not username[0].islower():
ja5087 marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError('Username must begin with a lowercase character')

if not all(c.islower() or c.isdigit() for c in username):
raise ValueError('Username must be only lowercase and numeric characters.')

if check_exists and not user_exists(username):
raise ValueError('Username does not exist.')
Expand Down
1 change: 0 additions & 1 deletion tests/account/validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class TestValidateUsername:
'Ckuehl',
'ckuehl!',
'123123',
Copy link

@PotatoParser PotatoParser Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be valid since the regex selects 3-16 digits/lowercase nums?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not valid because the validation logic comes from validate_username. You're right that the regex is lax and I don't really know how to feel about it. On the one hand Kerberos principals can be any printable ASCII so there is nothing stopping us from creating [email protected] for example, and the method's purpose is to extract that out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the goal of the regex to handle any arbitrary Kerberos principal, or only those which correspond to valid usernames? I must point out that the regex as written won't match hypothetical Kerberos principals which are longer than 16 characters, have hyphens in them, or the like.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I think making it tighter and in line with what we allow will be better than in this weird state of not doing either.

'f00f00',
])
def test_failure(self, username):
with pytest.raises(ValueError):
Expand Down