2020-07-12 18:53:04 +00:00
|
|
|
# Password validation
|
2020-04-14 20:31:44 +00:00
|
|
|
specials = ['[', '@', '_', '!', '#', '$', '%', '^', '&', '*', '(', ')',
|
|
|
|
'<', '>', '?', '/', '\\', '|', '}', '{', '~', ':', ']']
|
|
|
|
|
2020-06-27 14:38:51 +00:00
|
|
|
|
2020-04-14 20:31:44 +00:00
|
|
|
class PasswordValidator:
|
2020-04-16 13:33:23 +00:00
|
|
|
def __init__(self, min_length, upper, lower, number, special):
|
2020-06-21 19:29:53 +00:00
|
|
|
self.criteria = {
|
|
|
|
"characters": int(min_length),
|
|
|
|
"uppercase characters": int(upper),
|
|
|
|
"lowercase characters": int(lower),
|
|
|
|
"numbers": int(number),
|
|
|
|
"special characters": int(special),
|
|
|
|
}
|
|
|
|
|
2020-04-14 20:31:44 +00:00
|
|
|
def validate(self, password):
|
2020-06-21 19:29:53 +00:00
|
|
|
count = {
|
|
|
|
"characters": 0,
|
|
|
|
"uppercase characters": 0,
|
|
|
|
"lowercase characters": 0,
|
|
|
|
"numbers": 0,
|
|
|
|
"special characters": 0,
|
|
|
|
}
|
2020-04-14 20:31:44 +00:00
|
|
|
for c in password:
|
2020-06-21 19:29:53 +00:00
|
|
|
count["characters"] += 1
|
2020-04-14 20:31:44 +00:00
|
|
|
if c.isupper():
|
2020-06-21 19:29:53 +00:00
|
|
|
count["uppercase characters"] += 1
|
2020-04-16 13:33:23 +00:00
|
|
|
elif c.islower():
|
2020-06-21 19:29:53 +00:00
|
|
|
count["lowercase characters"] += 1
|
2020-04-14 20:31:44 +00:00
|
|
|
elif c.isnumeric():
|
2020-06-21 19:29:53 +00:00
|
|
|
count["numbers"] += 1
|
2020-04-14 20:31:44 +00:00
|
|
|
elif c in specials:
|
2020-06-21 19:29:53 +00:00
|
|
|
count["special characters"] += 1
|
2020-04-14 20:31:44 +00:00
|
|
|
for criterion in count:
|
|
|
|
if count[criterion] < self.criteria[criterion]:
|
|
|
|
count[criterion] = False
|
|
|
|
else:
|
|
|
|
count[criterion] = True
|
|
|
|
return count
|
2020-06-21 19:29:53 +00:00
|
|
|
|
2020-04-14 20:31:44 +00:00
|
|
|
def getCriteria(self):
|
|
|
|
lines = {}
|
|
|
|
for criterion in self.criteria:
|
|
|
|
min = self.criteria[criterion]
|
|
|
|
if min > 0:
|
|
|
|
text = f"Must have at least {min} "
|
|
|
|
if min == 1:
|
|
|
|
text += criterion[:-1]
|
|
|
|
else:
|
|
|
|
text += criterion
|
|
|
|
lines[criterion] = text
|
|
|
|
return lines
|