|
28 | 28 | from phonenumber_field.modelfields import PhoneNumberField
|
29 | 29 | from private_storage.fields import PrivateFileField
|
30 | 30 |
|
31 |
| -def sanitize_mac_address(mac): |
32 |
| - """ |
33 |
| - Sanitize a MAC address string to the colon-separated lowercase format. |
34 |
| - If the input is not a valid MAC address, return it unchanged. |
35 |
| - |
36 |
| - Handles various MAC address formats: |
37 |
| - - 00:1A:2B:3C:4D:5E -> 00:1a:2b:3c:4d:5e |
38 |
| - - 00-1A-2B-3C-4D-5E -> 00:1a:2b:3c:4d:5e |
39 |
| - - 001A.2B3C.4D5E -> 00:1a:2b:3c:4d:5e |
40 |
| - - 001A2B3C4D5E -> 00:1a:2b:3c:4d:5e |
41 |
| - """ |
42 |
| - # Return empty string or non-string input as is |
43 |
| - if not mac or not isinstance(mac, str): |
44 |
| - return mac |
45 |
| - |
46 |
| - # Check if it's an IP address (IPv4) - preserve unchanged |
47 |
| - if re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', mac): |
48 |
| - return mac |
49 |
| - |
50 |
| - # Try to extract MAC address from the string |
51 |
| - # Look for MAC address patterns, including those with additional text |
52 |
| - mac_patterns = [ |
53 |
| - r'([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}', # Standard MAC with : or - |
54 |
| - r'([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}', # Cisco format |
55 |
| - r'[0-9A-Fa-f]{12}' # No separators |
56 |
| - ] |
57 |
| - |
58 |
| - for pattern in mac_patterns: |
59 |
| - match = re.search(pattern, mac) |
60 |
| - if match: |
61 |
| - mac_candidate = match.group(0) |
62 |
| - try: |
63 |
| - # Try to parse as EUI to validate |
64 |
| - eui = EUI(mac_candidate) |
65 |
| - # Return sanitized MAC address in colon-separated lowercase format |
66 |
| - return ':'.join(['%02x' % x for x in eui.words]).lower() |
67 |
| - except (AddrFormatError, ValueError, TypeError): |
68 |
| - continue |
69 |
| - |
70 |
| - # If no valid MAC found, return original string unchanged |
71 |
| - return mac |
72 |
| - |
73 |
| -import swapper |
74 | 31 | from openwisp_radius.registration import (
|
75 | 32 | REGISTRATION_METHOD_CHOICES,
|
76 | 33 | get_registration_choices,
|
@@ -104,7 +61,6 @@ def sanitize_mac_address(mac):
|
104 | 61 | prefix_generate_users,
|
105 | 62 | validate_csvfile,
|
106 | 63 | )
|
107 |
| -from ..mac_utils import sanitize_mac_address |
108 | 64 | from .validators import ipv6_network_validator, password_reset_url_validator
|
109 | 65 |
|
110 | 66 | logger = logging.getLogger(__name__)
|
@@ -220,6 +176,49 @@ def sanitize_mac_address(mac):
|
220 | 176 | OPTIONAL_SETTINGS = app_settings.OPTIONAL_REGISTRATION_FIELDS
|
221 | 177 |
|
222 | 178 |
|
| 179 | +def sanitize_mac_address(mac): |
| 180 | + """ |
| 181 | + Sanitize a MAC address string to the colon-separated lowercase format. |
| 182 | + If the input is not a valid MAC address, return it unchanged. |
| 183 | +
|
| 184 | + Handles various MAC address formats: |
| 185 | + - 00:1A:2B:3C:4D:5E -> 00:1a:2b:3c:4d:5e |
| 186 | + - 00-1A-2B-3C-4D-5E -> 00:1a:2b:3c:4d:5e |
| 187 | + - 001A.2B3C.4D5E -> 00:1a:2b:3c:4d:5e |
| 188 | + - 001A2B3C4D5E -> 00:1a:2b:3c:4d:5e |
| 189 | + """ |
| 190 | + # Return empty string or non-string input as is |
| 191 | + if not mac or not isinstance(mac, str): |
| 192 | + return mac |
| 193 | + |
| 194 | + # Check if it's an IP address (IPv4) - preserve unchanged |
| 195 | + if re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", mac): |
| 196 | + return mac |
| 197 | + |
| 198 | + # Try to extract MAC address from the string |
| 199 | + # Look for MAC address patterns, including those with additional text |
| 200 | + mac_patterns = [ |
| 201 | + r"([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}", # Standard MAC with : or - |
| 202 | + r"([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}", # Cisco format |
| 203 | + r"[0-9A-Fa-f]{12}", # No separators |
| 204 | + ] |
| 205 | + |
| 206 | + for pattern in mac_patterns: |
| 207 | + match = re.search(pattern, mac) |
| 208 | + if match: |
| 209 | + mac_candidate = match.group(0) |
| 210 | + try: |
| 211 | + # Try to parse as EUI to validate |
| 212 | + eui = EUI(mac_candidate) |
| 213 | + # Return sanitized MAC address in colon-separated lowercase format |
| 214 | + return ":".join(["%02x" % x for x in eui.words]).lower() |
| 215 | + except (AddrFormatError, ValueError, TypeError): |
| 216 | + continue |
| 217 | + |
| 218 | + # If no valid MAC found, return original string unchanged |
| 219 | + return mac |
| 220 | + |
| 221 | + |
223 | 222 | class AutoUsernameMixin(object):
|
224 | 223 | def clean(self):
|
225 | 224 | """
|
|
0 commit comments