59 lines
2.7 KiB
Python
59 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
###############################################################################
|
|
# ScribeEngine - Open Source Blog Software #
|
|
# --------------------------------------------------------------------------- #
|
|
# Copyright (c) 2010 Raoul Snyman #
|
|
# --------------------------------------------------------------------------- #
|
|
# This program is free software; you can redistribute it and/or modify it #
|
|
# under the terms of the GNU General Public License as published by the Free #
|
|
# Software Foundation; version 2 of the License. #
|
|
# #
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
|
# more details. #
|
|
# #
|
|
# You should have received a copy of the GNU General Public License along #
|
|
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
###############################################################################
|
|
|
|
"""
|
|
Server-side validators.
|
|
"""
|
|
import logging
|
|
import re
|
|
|
|
from formencode.api import FancyValidator, Invalid
|
|
from formencode.validators import UnicodeString, Int, Email, FieldsMatch
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
class Password(FancyValidator):
|
|
"""
|
|
This validator checks for a decently secure password. The password has to
|
|
contain a minimum of 6 characters, at least 1 number.
|
|
"""
|
|
regex = re.compile(r'^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[-.!@#%&]).{6,}$')
|
|
|
|
messages = {
|
|
u'insecure': u'Your password must be longer than 6 characters and '
|
|
u'must have at least 1 capital letter, 1 number and one '
|
|
u'of the following characters: - . ~ @ # %% &'
|
|
}
|
|
|
|
def _to_python(self, value, state):
|
|
# _to_python gets run before validate_python. Here we
|
|
# strip whitespace off the password, because leading and
|
|
# trailing whitespace in a password is too elite.
|
|
return value.strip()
|
|
|
|
def validate_python(self, value, state):
|
|
if len(value) < self.min:
|
|
raise Invalid(self.message(u'insecure', state), value, state)
|
|
if not self.regex.match(value):
|
|
raise Invalid(self.message(u'insecure', state), value, state)
|
|
|