Source code for server.views_api.validations
import re
from ..models import roles, User
[docs]
def password_format_is_valid(password):
pattern = r"^[a-zA-Z\d.,:;!?'\"_\-+*/^=~()\[\]{}<>@#$%&|]*$"
return bool(re.fullmatch(pattern, password))
[docs]
def password_is_secure(password):
pattern = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$"
return bool(re.fullmatch(pattern, password))
[docs]
def username_is_valid_format(username):
pattern = r"^[a-z_][a-z0-9_-]*[$]?$"
return bool(re.match(pattern, username))
[docs]
def email_address_is_valid(email):
pattern = r"^[\w\.-]+@([\w-]+\.)+[\w-]{2,4}$"
return bool(re.fullmatch(pattern, email)) or (email == "")
[docs]
def get_metadata_errors(username, email, role):
"""Yield errors in metadata format."""
if not username:
yield "username", "Username is required"
elif username_is_too_short(username):
yield "username", "Must be at least 4 characters long"
elif username_is_too_long(username):
yield "username", "Username is too long"
elif not username_is_valid_format(username):
yield "username", "Username format is invalid, is it lowercase?"
if not email_address_is_valid(email):
yield "email", "Not a valid e-mail"
if not role:
yield "role", "Role is required"
elif not role_is_valid(role):
yield "role", "Invalid role"
[docs]
def get_password_errors(password):
"""Yield errors in password format."""
if not password:
yield "password", "Password is required"
elif not password_is_secure(password):
yield "password", "Min. 8 characters including lower case, upper case and digits"
elif not password_format_is_valid(password):
yield "password", "Some symbols are not allowed"
[docs]
def pretty_name_is_valid(name):
pattern = r"[\w\s'\[\]]+"
return all([bool(re.fullmatch(pattern, name)), len(name) <= 30])
[docs]
def ipv4_is_valid(ip):
pattern = r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$"
return bool(re.fullmatch(pattern, ip))
[docs]
def get_device_metadata_errors(name, ipv4):
"""Yield errors in metadata format for devices."""
if not name_is_valid(name):
yield "name", "Invalid format"
if not ipv4_is_valid(ipv4):
yield "ipv4", "Invalid format"
[docs]
def get_session_metadata_errors(name, begin, end):
"""Yield errors in metadata format for sessions."""
if not pretty_name_is_valid(name):
yield "name", "Invalid characters or too long"
if not isinstance(begin, int):
yield "begin", "Invalid data type (not int)"
if not isinstance(end, int):
yield "end", "Invalid data type (not int)"
if isinstance(begin, int) and isinstance(end, int):
if begin >= end:
yield "begin", "Invalid time interval"
yield "end", "Invalid time interval"
if begin < 0:
yield "begin", "Negative value"
if end < 0:
yield "end", "Negative value"