flask-security encrypt_password('mypassword') 每次刷新页面结果都不同

0 投票
1 回答
8114 浏览
提问于 2025-04-18 05:19

我已经设置了 SECURITY_PASSWORD_SALT 并使用了 "bcrypt"。但是每次我刷新页面时, print encrypt_password('mypassword') 都会打印出不同的值,所以我无法通过 verify_password(form.password.data, user.password) 来验证用户输入的密码。

不过,我可以通过 flask-security 自带的登录视图成功登录。

下面是展示 encrypt_password 奇怪行为的代码:

from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, \
    UserMixin, RoleMixin, login_required
from flask.ext.security.utils import encrypt_password, verify_password

# Create app
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'

app.config['SECURITY_PASSWORD_HASH'] = 'sha512_crypt'
app.config['SECURITY_PASSWORD_SALT'] = 'fhasdgihwntlgy8f'

# Create database connection object
db = SQLAlchemy(app)

# Define models
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)

# Create a user to test with
@app.before_first_request
def create_user():
    db.create_all()
    user_datastore.create_user(email='matt@nobien.net', password='password')
    db.session.commit()

# Views
@app.route('/')
#@login_required
def home():
    password = encrypt_password('mypassword')
    print verify_password('mypassword', password)
    return password
#    return render_template('index.html')

if __name__ == '__main__':
    app.run()

1 个回答

2

函数 encrypt_password() 生成一个新值是故意设计的。而 verify_password() 失败则不是,这个问题已经在 Flask-Security 中被报告为一个bug

当你使用登录视图时,会用到一个不同的方法 verify_and_update_password(),这个方法没有遇到同样的问题。

这个问题的修复还没有包含在新的版本中。你可以通过应用PR #223 的更改来自己解决这个问题;这个更改会把 flask_security/utils.py 文件中的 verify_password() 函数替换为:

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)

例如,在验证密码之前,先用 HMAC+SHA512 对密码进行哈希处理,就像原来的 encrypt_password() 所做的那样,而不是像当前发布的版本那样直接使用 encrypt_password()

撰写回答