【问题标题】:How to verify password hash on Flask-Admin [duplicate]如何在 Flask-Admin 上验证密码哈希 [重复]
【发布时间】:2019-11-29 17:47:49
【问题描述】:

当我们用Flask-Admin注册一个新用户时,它会自动生成一个密码哈希。

如何验证哈希..? 任何类似的方法,如 bycript 上的 check_password_hash,或来自 werkzeug.security..?

check_password_hash

我从 Flask-Security 尝试了这个 verify_password,但似乎不起作用。

这是我的代码的 sn-p:

config.py

SECURITY_PASSWORD_HASH = "pbkdf2_sha256"
SECURITY_PASSWORD_SALT = "ATGUOHAELKiubahiughaerGOJAEGj"

这是我的 models.py

from flask_security import UserMixin
from werkzeug.security import check_password_hash

class User(db.Model, UserMixin):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password = db.Column(db.String(128))

    def check_password(self, password):
        # return verify_password(self.password, password)   # from Flask-Security
        # return verify_and_update_password(self.password, password) # from Flask-Security
        return check_password(self.password, password) # from werkzeug.security

当我尝试验证用户首次注册时从 Flask-Admin 自动生成的密码哈希时,chek_password 没有一个工作,它总是像这样返回 False。

>>> u1=db.session.query(User).filter_by(email='zidanecr7kaka@gmail.com').first()
>>> u1.check_password('123456')
False

但是当我尝试像下面这样手动设置密码时:

def set_password(self, password):
    # self.password = encrypt_password(password)    # from Flask-Security
    # self.password = hash_password(password)         # from Flask-Security
    self.password = generate_password_hash(password) # from werkzeug.security

返回真:

>>> u1=db.session.query(User).filter_by(email='zidanecr7kaka@gmail.com').first()
>>> u1.set_password('123456')
>>> u1.check_password('123456')
True

但是当我尝试从用户首次注册时从 Flask Admin 自动生成的密码哈希中 check_password 时,它总是返回 False 值:

所以我的问题的重点是,如何使用 Flask-Admin.. 验证密码哈希?

【问题讨论】:

    标签: python flask hash flask-admin flask-security


    【解决方案1】:

    flask-admin 示例包括 this 注释代码:

    from werkzeug.security import generate_password_hash, check_password_hash
    
    ...
    
        # we're comparing the plaintext pw with the the hash from the db
        if not check_password_hash(user.password, self.password.data):
        # to compare plain text passwords use
        # if user.password != self.password.data:
            raise validators.ValidationError('Invalid password')
    

    【讨论】:

      【解决方案2】:

      我在我的案例中找到了这个best answer

      所以我制作了名为 utils.py 的新闻模块,代码如下:

      from flask_security.utils import _security, get_hmac, _pwd_context
      
      
      def verify_password(password, password_hash):
          """Returns ``True`` if the password matches the supplied hash.
      
          :param password: A plaintext password to verify
          :param password_hash: The expected hash value of the password (usually form your database)
          """
          if _security.password_hash != 'plaintext':
              password = get_hmac(password)
      
          return _pwd_context.verify(password, password_hash)
      

      然后我修改我的 models.py 如下:

      from flask_security import UserMixin
      from app.utils import verify_password
      
      
      class User(db.Model, UserMixin):
          __tablename__ = 'user'
          id = db.Column(db.Integer, primary_key=True)
          email = db.Column(db.String(120), index=True, unique=True)
          password = db.Column(db.String())
      
          def check_password(self, password):
              return verify_password(password, self.password)
      

      非常感谢有answered 的人。

      【讨论】:

        猜你喜欢
        • 2016-05-12
        • 1970-01-01
        • 1970-01-01
        • 2015-12-17
        • 2014-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多