66 lines
1.3 KiB
Python
66 lines
1.3 KiB
Python
|
|
from . import VALID, INVALID
|
|
|
|
def mac_addr(addr):
|
|
|
|
VALID_MAC_CHARS = "0123456789abcdefABCDEF"
|
|
|
|
if len(addr) != 17:
|
|
return INVALID
|
|
|
|
COLON_START_IDX = 2
|
|
COLON_STEP = 3
|
|
for idx in range(COLON_START_IDX, len(addr), COLON_STEP):
|
|
if addr[idx] != ":":
|
|
return INVALID
|
|
|
|
mac_sections = addr.split(":")
|
|
if len(mac_sections) != 6:
|
|
return INVALID
|
|
|
|
for mac_byte in mac_sections:
|
|
|
|
if len(mac_byte) != 2:
|
|
return INVALID
|
|
|
|
for char in mac_byte:
|
|
if not char in VALID_MAC_CHARS:
|
|
return INVALID
|
|
|
|
return VALID
|
|
|
|
|
|
|
|
def password_strength(password):
|
|
|
|
PW_MIN_LEN = 8
|
|
|
|
if not isinstance(password, str):
|
|
return "Not a string"
|
|
|
|
if len(password) < PW_MIN_LEN:
|
|
return f"Password too short. {PW_MIN_LEN} character minimum"
|
|
|
|
|
|
|
|
def text_numeric_only(text, extra_chars=""):
|
|
WHITELIST_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
for char in text:
|
|
if not (char in WHITELIST_CHARS or char in extra_chars):
|
|
return INVALID
|
|
|
|
return VALID
|
|
|
|
|
|
|
|
def username(username):
|
|
return text_numeric_only(username, extra_chars=" _-")
|
|
|
|
|
|
def email(email):
|
|
return text_numeric_only(email, extra_chars=" _-@+.")
|
|
|
|
|
|
|