From 536b46e5271d9f7457e25d9c558eab2e8e74a903 Mon Sep 17 00:00:00 2001 From: ljf Date: Tue, 28 Aug 2018 21:34:33 +0200 Subject: [PATCH] [enh] Support advice with standalone password.py --- src/yunohost/utils/password.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/yunohost/utils/password.py b/src/yunohost/utils/password.py index 1e6bcf813..22dbb1810 100644 --- a/src/yunohost/utils/password.py +++ b/src/yunohost/utils/password.py @@ -53,14 +53,18 @@ class PasswordValidator(object): Validate a password and raise error or display a warning """ if self.validation_strength <= 0: - return + return ("success", "") self.strength = self.compute(password, ACTIVATE_ONLINE_PWNED_LIST) if self.strength < self.validation_strength: if self.listed: - return "password_listed_" + str(self.validation_strength) + return ("error", "password_listed_" + str(self.validation_strength)) else: - return "password_too_simple_" + str(self.validation_strength) + return ("error", "password_too_simple_" + str(self.validation_strength)) + + if self.strength < 3: + return ("warning", 'password_advice') + return ("success", "") def compute(self, password, online=False): # Indicators @@ -173,19 +177,21 @@ class LoggerPasswordValidator(ProfilePasswordValidator): logger = logging.getLogger('yunohost.utils.password') - error = super(LoggerPasswordValidator, self).validate(password) - if error is not None: - raise MoulinetteError(1, m18n.n(error)) - - if self.strength < 3: - logger.info(m18n.n('password_advice')) + status, msg = super(LoggerPasswordValidator, self).validate(password) + if status == "error": + raise MoulinetteError(1, m18n.n(msg)) + elif status == "warning": + logger.info(m18n.n(msg)) if __name__ == '__main__': if len(sys.argv) < 2: print("usage: password.py PASSWORD") - result = ProfilePasswordValidator('user').validate(sys.argv[1]) - if result is not None: - sys.exit(result) + status, msg = ProfilePasswordValidator('user').validate(sys.argv[1]) + if status == "error": + sys.exit(msg) + elif status == "warning": + print(msg) + sys.exit(0)