Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 api/namex/VERSION.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.2.67'
__version__ = '1.2.68'
7 changes: 3 additions & 4 deletions api/namex/utils/auth.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import string

import requests
from flask import Request, current_app
from flask_jwt_oidc.jwt_manager import JwtManager
from jose import jwt

from namex.models import Request as RequestDAO
from namex.utils.common import normalize_phone_number


def cors_preflight(methods):
Expand Down Expand Up @@ -65,7 +64,7 @@ def full_access_to_name_request(request: Request) -> bool:
applicant = name_request.applicants[0]

if phone:
phone = phone.translate(str.maketrans('', '', string.punctuation))
phone = normalize_phone_number(phone)
if not (phone or email):
current_app.logger.debug(
'Failed no phone or email - NR: %s, NRL: %s, Email: %s, Phone: %s', nr, nrl, email, phone
Expand All @@ -74,7 +73,7 @@ def full_access_to_name_request(request: Request) -> bool:
if (
phone
and applicant.phoneNumber
and phone != applicant.phoneNumber.translate(str.maketrans('', '', string.punctuation))
and phone != normalize_phone_number(applicant.phoneNumber)
):
current_app.logger.debug(
'Failed wrong phone - NR: %s, NRL: %s, Email: %s, Phone: %s, Applicant phone %s',
Expand Down
24 changes: 24 additions & 0 deletions api/namex/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,27 @@ def convert_to_utc_max_date_time(date_str: str):

# def remove_numbers_dict(d):
# return {key: value for key, value in d.items() if not key.isdigit()}


def normalize_phone_number(phone: str) -> str:
"""
Normalize a phone number by stripping all non-digit characters.

This function takes a phone number string in various formats
(e.g., '555-555-5555', '555 555 5555', '(555) 555-5555', '+1 (555) 555-5555') and returns
a normalized string containing only digits.

Args:
phone_number (str): The phone number string to normalize.

Returns:
str: The normalized phone number containing only digits.
Example: '5555555555'.
"""
# keep only digits
digits = re.sub(r'\D', '', phone)

# handle North America: allow 10 digits, or 11 with leading "1"
if len(digits) == 11 and digits.startswith('1'):
digits = digits[1:]
return digits
16 changes: 16 additions & 0 deletions api/tests/python/unit/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@ def test_full_access_to_name_request(
req = Request(env)

assert expected == full_access_to_name_request(req)


@pytest.mark.parametrize('input_phone,expected', [
('(555) 123-4567', '5551234567'),
('+1-555-123-4567', '5551234567'),
('555.123.4567', '5551234567'),
('555 123 4567', '5551234567'),
('5551234567', '5551234567'),
('', ''),
(None, ''),
])
def test_normalize_phone_number(input_phone, expected):
"""Assure that normalized phone number contains only digits."""
from namex.utils.auth import normalize_phone_number
result = normalize_phone_number(input_phone or '')
assert result == expected
Loading