Skip to content Skip to sidebar Skip to footer

Storing And Validating Encrypted Password For Login In Pyramid

I am trying to validate an encrypted password for login purposes in Pyramid. So that if the user and password match then the system will authorize the user. At the moment I am find

Solution 1:

Please modify your code, add the excellent passlib library, and use secure password storage using bcrypt as the hashing algorithm.

In your projects setup.py add the following as requirements:

  • bcrypt
  • passlib

And then use the following code snippet for your model:

from passlib.hashimport bcrypt

classUser(Base):
    __tablename__ = 'users'id = Column(Integer, primary_key=True)
    username = Column(String(15), nullable=False, unique=True)
    email = Column(String(300))
    password = Column(String(300), nullable=False)

    def__init__(self, username, password, email):
        self.username = username
        self.password = bcrypt.encrypt(password)
        self.email = email

    defvalidate_password(self, password):
        return bcrypt.verify(password, self.password)

    def__repr__(self):
        return"<User(username ='%s', password='%s', email='%s')>" % (self.username, self.password, self.email)

Solution 2:

WARNING INSECURE CODE FOLLOWS

The code below is NOT a SECURE and SAFE way to store/verify user passwords. Please use a library that provides secure password storage, such as passlib which is specifically designed to securely store passwords.


You hash the user's password in your User.__init__ using self.password = hashlib.sha224(password).hexdigest(). Just use a similar method to validate it:

classUser(Base):# Your existing code unchangeddefvalidate_password(self, password):
        returnself.password == hashlib.sha224(password).hexdigest()

And use it in your view:

user = api.retrieve_user(login)
if user isnotNoneand user.validate_password(password):
    # You logic on success

Post a Comment for "Storing And Validating Encrypted Password For Login In Pyramid"