Storing and validating encrypted password for login in Pyramid

前端 未结 2 1905
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 22:08

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

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 22:58

    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.hash import bcrypt
    
    class User(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
    
        def validate_password(self, password):
            return bcrypt.verify(password, self.password)
    
        def __repr__(self):
            return "" % (self.username, self.password, self.email)
    

提交回复
热议问题