import re
def match_condition(rule, s):
if s == None:
return False
elif rule[0] == '/' and rule[-1] == '/':
return bool(re.search(rule[1:-1], s))
elif rule[0] == '/' and rule[-2:] == '/i':
return bool(re.search(rule[1:-2], s, re.I))
return rule == s
def match_rule(rule, values):
for key, value in rule.items():
if key != 'should' and not match_condition(value, values.get(key)):
return False
return True
def match_rules(rules, **kwargs):
for rule in rules:
if match_rule(rule, kwargs):
return rule.get('should') == 'allow'
return False
if __name__ == '__main__':
conditions = [
{"password": "/honey|gotcha/i", "should": "deny"},
{"username": "/honey|gotcha/i", "should": "deny"},
{"username": "/^[a-z][\\w-]+$/", "password": "/^[ -~]{4,15}$/", "should": "allow"}
]
print(match_rules(conditions, username='honey', password='l'))
print(match_rules(conditions, username='someuser', password='somepassword'))
To embed this program on your website, copy the following code and paste it into your website's HTML: