docs: password_strength.py

* also add type hints
This commit is contained in:
Hussain Nagaria 2023-12-18 18:20:52 +05:30
parent 47626e96fb
commit aefe634ff7

View file

@ -1,14 +1,18 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
from zxcvbn import zxcvbn
from collections.abc import Iterable
from zxcvbn import _Result, zxcvbn
from zxcvbn.feedback import _Feedback as PasswordStrengthFeedback
from zxcvbn.matching import _Match
from zxcvbn.scoring import ALL_UPPER, START_UPPER
import frappe
from frappe import _
def test_password_strength(password, user_inputs=None):
def test_password_strength(password: str, user_inputs: Iterable[object] = None) -> _Result:
"""Wrapper around zxcvbn.password_strength"""
if len(password) > 128:
# zxcvbn takes forever when checking long, random passwords.
@ -27,8 +31,9 @@ def test_password_strength(password, user_inputs=None):
# see license for feedback code at https://github.com/sans-serif/python-zxcvbn/blob/master/LICENSE.txt
# -------------------------------------------
# Default feedback value
default_feedback = {
default_feedback: PasswordStrengthFeedback = {
"warning": "",
"suggestions": [
_("Use a few words, avoid common phrases."),
@ -37,10 +42,8 @@ default_feedback = {
}
def get_feedback(score, sequence):
"""
Returns the feedback dictionary consisting of ("warning","suggestions") for the given sequences.
"""
def get_feedback(score: int, sequence: list) -> PasswordStrengthFeedback:
"""Return the feedback dictionary consisting of ("warning","suggestions") for the given sequences."""
global default_feedback
minimum_password_score = int(
frappe.db.get_single_value("System Settings", "minimum_password_score") or 2
@ -69,10 +72,8 @@ def get_feedback(score, sequence):
return feedback
def get_match_feedback(match, is_sole_match):
"""
Returns feedback as a dictionary for a certain match
"""
def get_match_feedback(match: _Match, is_sole_match: bool) -> PasswordStrengthFeedback:
"""Return feedback as a dictionary for a certain match."""
def fun_bruteforce():
# Define a number of functions that are used in a look up dictionary
@ -142,10 +143,8 @@ def get_match_feedback(match, is_sole_match):
return pattern_fn()
def get_dictionary_match_feedback(match, is_sole_match):
"""
Returns feedback for a match that is found in a dictionary
"""
def get_dictionary_match_feedback(match: _Match, is_sole_match: bool) -> PasswordStrengthFeedback:
"""Return feedback for a match that is found in a dictionary."""
warning = ""
suggestions = []