Skip to content

Commit

Permalink
Update username regex and also use email when username is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankApiyo committed Sep 18, 2024
1 parent 14a4dd8 commit 5a46348
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ OPENID_CONNECT_VIEWSET_CONFIG = {
# that's used to validate all field inputs retrieved for the particular key
"FIELD_VALIDATION_REGEX": {
"username": {
"regex": "(?!^\d+$)^.+$",
"regex": "^(?!\d+$).{4,}$",
"help_text": "Username should only contain alpha numeric characters",
}
},
Expand Down
2 changes: 1 addition & 1 deletion oidc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"JWT_ALGORITHM": "HS256",
"FIELD_VALIDATION_REGEX": {
"username": {
"regex": "(?!^\d+$)^.+$", # noqa
"regex": "^(?!\d+$).{4,}$", # noqa
"help_text": "Username should only contain alpha numeric characters",
}
},
Expand Down
24 changes: 15 additions & 9 deletions oidc/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
oidc Viewsets module
"""

import importlib
import re
from typing import Optional, Tuple
Expand Down Expand Up @@ -226,9 +227,15 @@ def _clean_user_data(self, user_data) -> Tuple[dict, Optional[list]]:
user_data["first_name"] = user_data["last_name"]
missing_fields.remove("first_name")

# use email as username if username is missing
# use email as username if username is missing or username is invalid
if self.use_email_as_username:
if "username" in missing_fields and "email" in user_data:
username_regex = re.compile(
self.field_validation_regex["username"].get("regex")
)
if (
"username" in missing_fields
or not username_regex.search(user_data["username"])
) and "email" in user_data:
username = user_data["email"].split("@")[0]
if (
self.user_model.objects.filter(username__iexact=username).count()
Expand All @@ -245,13 +252,12 @@ def _clean_user_data(self, user_data) -> Tuple[dict, Optional[list]]:
)

# Validate retrieved username matches regex
if "username" in self.field_validation_regex:
regex = re.compile(
self.field_validation_regex["username"].get("regex")
)
if regex.search(username):
user_data["username"] = username
missing_fields.remove("username")
if (
"username" in self.field_validation_regex
and username_regex.search(username)
):
user_data["username"] = username
missing_fields.remove("username")

return user_data, missing_fields

Expand Down
3 changes: 2 additions & 1 deletion tests/test_viewsets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for the OpenID Client
"""

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.test.utils import override_settings
Expand Down Expand Up @@ -46,7 +47,7 @@
"JWT_SECRET_KEY": "abc",
"FIELD_VALIDATION_REGEX": {
"username": {
"regex": "^[\w\d]*$",
"regex": "^(?!\d+$).{4,}$",
"help_text": "Username should only contain word characters & numbers i.e datatester23",
},
},
Expand Down

0 comments on commit 5a46348

Please sign in to comment.