如何在 Flask-Admin 上验证密码哈希

How to verify password hash on Flask-Admin

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

如何验证散列..? 任何类似的方法,例如 bycript 上的 check_password_hash,或 werkzeug.security..?

中的 check_password_hash

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

这是我的代码片段:

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 没有一个起作用,它总是 returns 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

它 return 正确:

>>> 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 时,它总是 returns 错误值:

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

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')

我找到了这个 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