-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathfeedback.py
More file actions
156 lines (138 loc) · 5.27 KB
/
feedback.py
File metadata and controls
156 lines (138 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from zxcvbn.scoring import START_UPPER, ALL_UPPER
from gettext import gettext as gettext_
from typing import Callable, Optional
# Store reference to translation function for fallback handling
def _(s: str) -> str:
"""Translation function wrapper that falls back to identity if no translation available.
Args:
s: The string to translate
Returns:
Translated string or original string if translation not available
"""
try:
# Try to use gettext translation
trans = globals().get('_', gettext_)
return trans(s)
except:
return s
# Store reference to translation function for fallback handling
_ = lambda s: s if not hasattr(_.__globals__, '_') else _.__globals__['_'](s)
def get_feedback(score, sequence):
if len(sequence) == 0:
return {
'warning': '',
'suggestions': [
_("Use a few words, avoid common phrases."),
_("No need for symbols, digits, or uppercase letters.")
]
}
if score > 2:
return {
'warning': '',
'suggestions': [],
}
longest_match = sequence[0]
for match in sequence[1:]:
if len(match['token']) > len(longest_match['token']):
longest_match = match
feedback = get_match_feedback(longest_match, len(sequence) == 1)
extra_feedback = _('Add another word or two. Uncommon words are better.')
if feedback:
feedback['suggestions'].insert(0, extra_feedback)
if not feedback['warning']:
feedback['warning'] = ''
else:
feedback = {
'warning': '',
'suggestions': [extra_feedback]
}
return feedback
def get_match_feedback(match, is_sole_match):
if match['pattern'] == 'dictionary':
return get_dictionary_match_feedback(match, is_sole_match)
elif match['pattern'] == 'spatial':
if match['turns'] == 1:
warning = _('Straight rows of keys are easy to guess.')
else:
warning = _('Short keyboard patterns are easy to guess.')
return {
'warning': warning,
'suggestions': [
_('Use a longer keyboard pattern with more turns.')
]
}
elif match['pattern'] == 'repeat':
if len(match['base_token']) == 1:
warning = _('Repeats like "aaa" are easy to guess.')
else:
warning = _('Repeats like "abcabcabc" are only slightly harder to ' \
'guess than "abc".')
return {
'warning': warning,
'suggestions': [
_('Avoid repeated words and characters.')
]
}
elif match['pattern'] == 'sequence':
return {
'warning': _('Sequences like "abc" or "6543" are easy to guess.'),
'suggestions': [
_('Avoid sequences.')
]
}
elif match['pattern'] == 'regex':
if match['regex_name'] == 'recent_year':
return {
'warning': _("Recent years are easy to guess."),
'suggestions': [
_('Avoid recent years.'),
_('Avoid years that are associated with you.'),
]
}
elif match['pattern'] == 'date':
return {
'warning': _("Dates are often easy to guess."),
'suggestions': [
_('Avoid dates and years that are associated with you.'),
],
}
def get_dictionary_match_feedback(match, is_sole_match):
warning = ''
if match['dictionary_name'] == 'passwords':
if is_sole_match and not match.get('l33t', False) and not \
match['reversed']:
if match['rank'] <= 10:
warning = _('This is a top-10 common password.')
elif match['rank'] <= 100:
warning = _('This is a top-100 common password.')
else:
warning = _('This is a very common password.')
elif match['guesses_log10'] <= 4:
warning = _('This is similar to a commonly used password.')
elif match['dictionary_name'] == 'english_wikipedia':
if is_sole_match:
warning = _('A word by itself is easy to guess.')
elif match['dictionary_name'] in ['surnames', 'male_names',
'female_names', ]:
if is_sole_match:
warning = _('Names and surnames by themselves are easy to guess.')
else:
warning = _('Common names and surnames are easy to guess.')
else:
warning = ''
suggestions = []
word = match['token']
if START_UPPER.search(word):
suggestions.append(_("Capitalization doesn't help very much."))
elif ALL_UPPER.search(word) and word.lower() != word:
suggestions.append(_("All-uppercase is almost as easy to guess as "
"all-lowercase."))
if match['reversed'] and len(match['token']) >= 4:
suggestions.append(_("Reversed words aren't much harder to guess."))
if match.get('l33t', False):
suggestions.append(_("Predictable substitutions like '@' instead of 'a' "
"don't help very much."))
return {
'warning': warning,
'suggestions': suggestions,
}