ldapcherry/ldapcherry/ppolicy/simple.py

41 lines
1.3 KiB
Python
Raw Normal View History

2015-07-01 08:58:23 +02:00
# -*- coding: utf-8 -*-
# vim:set expandtab tabstop=4 shiftwidth=4:
#
# The MIT License (MIT)
# LdapCherry
# Copyright (c) 2014 Carpentier Pierre-Francois
2015-07-01 23:00:42 +02:00
import ldapcherry.ppolicy
import re
2015-07-10 21:01:39 +02:00
2015-07-01 23:00:42 +02:00
class PPolicy(ldapcherry.ppolicy.PPolicy):
def __init__(self, config, logger):
self.config = config
self.min_length = self.get_param('min_length')
self.min_upper = self.get_param('min_upper')
self.min_digit = self.get_param('min_digit')
2015-07-01 23:00:42 +02:00
def check(self, password):
if len(password) < self.min_length:
return {'match': False, 'reason': 'Password too short'}
2015-07-01 23:00:42 +02:00
if len(re.findall(r'[A-Z]', password)) < self.min_upper:
2015-07-10 21:01:39 +02:00
return {
'match': False,
'reason': 'Not enough upper case characters'
2015-07-10 21:01:39 +02:00
}
2015-07-01 23:00:42 +02:00
if len(re.findall(r'[0-9]', password)) < self.min_digit:
return {'match': False, 'reason': 'Not enough digits'}
2015-07-01 23:00:42 +02:00
return {'match': True, 'reason': 'password ok'}
def info(self):
return \
2017-01-31 20:59:49 +01:00
"* Minimum length: %(len)d\n" \
"* Minimum number of uppercase characters: %(upper)d\n" \
"* Minimum number of digits: %(digit)d" % {
2015-07-10 21:01:39 +02:00
'upper': self.min_upper,
'len': self.min_length,
'digit': self.min_digit
}