Merge LoggerPasswordValidator with PasswordValidator

This commit is contained in:
Alexandre Aubin 2018-10-25 18:36:55 +00:00
parent 3c5ce491c5
commit 55256c1973

View file

@ -63,6 +63,36 @@ class PasswordValidator(object):
# Fallback to default value if we can't fetch settings for some reason # Fallback to default value if we can't fetch settings for some reason
self.validation_strength = 2 if profile == 'admin' else 1 self.validation_strength = 2 if profile == 'admin' else 1
def validate(self, password):
"""
Check the validation_summary and trigger the corresponding
exception or info message
This method is meant to be used from inside YunoHost's code
(compared to validation_summary which is meant to be called
by ssowat)
"""
if self.validation_strength == -1:
return
# Note that those imports are made here and can't be put
# on top (at least not the moulinette ones)
# because the moulinette needs to be correctly initialized
# as well as modules available in python's path.
import errno
import logging
from moulinette import m18n
from moulinette.core import MoulinetteError
from moulinette.utils.log import getActionLogger
logger = logging.getLogger('yunohost.utils.password')
status, msg = validation_summary(password)
if status == "error":
raise MoulinetteError(1, m18n.n(msg))
elif status == "warning":
logger.info(m18n.n(msg))
def validation_summary(self, password): def validation_summary(self, password):
""" """
Check if a password is listed in the list of most used password Check if a password is listed in the list of most used password
@ -95,7 +125,7 @@ class PasswordValidator(object):
For instance, "PikachuDu67" is (11, 2, 7, 2, 0) For instance, "PikachuDu67" is (11, 2, 7, 2, 0)
""" """
# Indicators
length = len(password) length = len(password)
digits = 0 digits = 0
uppers = 0 uppers = 0
@ -150,28 +180,9 @@ class PasswordValidator(object):
return False return False
class LoggerPasswordValidator(PasswordValidator): # This file is also meant to be used as an executable by
# SSOwat to validate password from the portal when an user
def validate(self, password): # change its password.
"""
Validate a password and raise error or display a warning
"""
if self.validation_strength == -1:
return
import errno
import logging
from moulinette import m18n
from moulinette.core import MoulinetteError
from moulinette.utils.log import getActionLogger
logger = logging.getLogger('yunohost.utils.password')
status, msg = super(LoggerPasswordValidator, self).validation_summary(password)
if status == "error":
raise MoulinetteError(1, m18n.n(msg))
elif status == "warning":
logger.info(m18n.n(msg))
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) < 2: if len(sys.argv) < 2:
import getpass import getpass
@ -182,5 +193,3 @@ if __name__ == '__main__':
status, msg = PasswordValidator('user').validation_summary(pwd) status, msg = PasswordValidator('user').validation_summary(pwd)
print(msg) print(msg)
sys.exit(0) sys.exit(0)