|
30 | 30 | from db import models |
31 | 31 |
|
32 | 32 | # From Auth0 password settings |
33 | | -ALLOWED_SPECIAL_CHARS = "!@#$%^&*" |
| 33 | +PASSWORD_MIN_LENGTH = 8 |
| 34 | +# Auth0's max password length is 72, it "allows" more characters but silently ignores |
| 35 | +# them beyond 72. We try to explicitly set a max of 72 |
| 36 | +PASSWORD_MAX_LENGTH = 72 |
| 37 | +# List of special characters from: |
| 38 | +# https://owasp.org/www-community/password-special-characters |
| 39 | +ALLOWED_SPECIAL_CHARS = r""" !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
| 40 | +# Define the regex for special characters explicitly (escaping some of them is tricky) |
| 41 | +SPECIAL_CHARS_PATTERN = r'[ !"#$%&\'()*+,\-./:;<=>?@[\\\]^_`{|}~]' |
34 | 42 | VALID_PASSWORD_REGEX = re.compile( |
35 | | - f"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[{ALLOWED_SPECIAL_CHARS}]).{{8,}}$" |
| 43 | + f"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?{SPECIAL_CHARS_PATTERN}).{{8,}}$" |
36 | 44 | ) |
37 | 45 | PASSWORD_FORMAT_MESSAGE = ( |
38 | 46 | "Password must contain at least one uppercase letter, one lowercase letter, one number, " |
@@ -79,9 +87,11 @@ def _check(v: str) -> str: |
79 | 87 | "max_length": "Username must be 128 characters or less.", |
80 | 88 | "pattern": "Username must only contain lowercase letters, numbers, hyphens and underscores." |
81 | 89 | }) |
82 | | -BiocommonsPassword = ValidatedString(min_length=8, max_length=72, pattern=VALID_PASSWORD_REGEX, messages={ |
83 | | - "min_length": "Password must be at least 8 characters.", |
84 | | - "max_length": "Password must be 72 characters or less.", |
| 90 | +BiocommonsPassword = ValidatedString(min_length=PASSWORD_MIN_LENGTH, |
| 91 | + max_length=PASSWORD_MAX_LENGTH, |
| 92 | + pattern=VALID_PASSWORD_REGEX, messages={ |
| 93 | + "min_length": f"Password must be at least {PASSWORD_MIN_LENGTH} characters.", |
| 94 | + "max_length": f"Password must be {PASSWORD_MAX_LENGTH} characters or less.", |
85 | 95 | "pattern": PASSWORD_FORMAT_MESSAGE |
86 | 96 | }) |
87 | 97 | BiocommonsFullName = ValidatedString(min_length=1, max_length=300, |
|
0 commit comments