HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Change password after being validated

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
aftervalidatedbeingpasswordchange

Problem

I'm doing Python programming challenges and have come across one where the object of the challenge was to create a script to change passwords. The script must validate that the password contains, one upper, one lowercase, and one number. After everything has been validated the script should save the password to a text file. I added special characters and some basic encryption to the program to make it a little more interesting.

Source:

```
# -- encoding: UTF-8 --

import re
import getpass
from random import randint

class PasswordValidator(object):
""" A password validator, used to validate a password for
specific characters, numbers, and special characters """

validated = False

def __init__(self, password):
self.password = password

def validate_length(self): # Check the length, gotta be over 8
if len(self.password) > 8:
return True
else:
return False

def validate_upper_case(self): # Check for upper case characters
return re.search("[A-Z]", self.password) is not None

def validate_lower_case(self): # Check for lower case characters
return re.search("[a-z]", self.password) is not None

def validate_integers(self): # Check for integers
return re.search("[0-9]", self.password) is not None

def validate_spec_chars(self): # Check for special characters
return re.search("[!$&*]", self.password) is not None

def validate_all(self):
""" Validate everything. """
length = self.validate_length()
upper = self.validate_upper_case()
lower = self.validate_lower_case()
digits = self.validate_integers()
specs = self.validate_spec_chars()

if specs is False:
return "Failed special characters validation. Allowed special chars: (! $ & *)"
elif digits is False:
return "Failed integer validation. Allowed integers: (0 - 9)"
elif lower is False:
ret

Solution

It would be more intuitive if your validate_all method was just called validate.

In quite a few places you are doing something like this in validate_length:

def validate_length(self):  # Check the length, gotta be over 8
    if len(self.password) > 8:
        return True
    else:
        return False


Just return the right value right away:

def validate_length(self):  # Check the length, gotta be over 8
    return len(self.password) > 8


It would also make more sense if the Validator.validate method just returned True or False instead of setting a class variable and returning a string detailing the status:

def validate(self, password):
    return all(validator(password) for validator in [self.validate_length, self.validate_upper_case, ...])


I would also make failing a testcase raise an exception and catch it in validate:

class ValidatorError(Exception):
    pass
...

class PasswordValidator(object):
    def __init__(self):
        self.validators = [self.validate_length, self.validate_upper_case, ...]

    ...

    def validate_length(self, password):  
        """Check the length, gotta be over 8"""
        if len(password) > 8:
            return True
        raise ValidatorError("Failed length validation. Allowed length: (> 8)")

    def validate(self, password):
        try:
            return all(validator(password) for validator in self.validators)
        except ValidatorError as e:
            print e
            return False

...
if __name__ == '__main__':
    pass_verify = obtain_password()
    val = PasswordValidator()
    if not val.validate(pass_verify):
        print "Password not validated."
    else:
        print second_authentication(pass_verify)


Also your encrypt function seems to be one-way. There is no way to validate an entered password in the future (to see whether the entered password is equal to the password set by this program).

Code Snippets

def validate_length(self):  # Check the length, gotta be over 8
    if len(self.password) > 8:
        return True
    else:
        return False
def validate_length(self):  # Check the length, gotta be over 8
    return len(self.password) > 8
def validate(self, password):
    return all(validator(password) for validator in [self.validate_length, self.validate_upper_case, ...])
class ValidatorError(Exception):
    pass
...

class PasswordValidator(object):
    def __init__(self):
        self.validators = [self.validate_length, self.validate_upper_case, ...]

    ...

    def validate_length(self, password):  
        """Check the length, gotta be over 8"""
        if len(password) > 8:
            return True
        raise ValidatorError("Failed length validation. Allowed length: (> 8)")

    def validate(self, password):
        try:
            return all(validator(password) for validator in self.validators)
        except ValidatorError as e:
            print e
            return False

...
if __name__ == '__main__':
    pass_verify = obtain_password()
    val = PasswordValidator()
    if not val.validate(pass_verify):
        print "Password not validated."
    else:
        print second_authentication(pass_verify)

Context

StackExchange Code Review Q#140058, answer score: 7

Revisions (0)

No revisions yet.