【问题标题】:flask-security encrypt_password('mypassword') varies every time when i reload the page每次我重新加载页面时,flask-security encrypt_password('mypassword') 都会变化
【发布时间】:2014-06-20 21:22:06
【问题描述】:

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

但我可以从烧瓶安全内置登录视图登录

这是演示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()

【问题讨论】:

标签: python flask-security


【解决方案1】:

encrypt_password() 生成新值的事实是设计使然。 verify_password() 失败的事实并非如此。这是一个已经被举报的bug in Flask-Security

当您使用登录视图时,使用了不同的方法,verify_and_update_password(),不会遇到同样的问题。

该修复还不是新版本的一部分。您可以通过应用change from 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()

【讨论】:

  • 是的。 pypi 版本还是有这个bug。我只是从上面的投票请求中修复它。它有效,谢谢。而且我希望pypi版本可以尽快应用这个修复,因为它可能会混淆像我这样的初学者
  • 非常感谢伙计,在花了很多时间之后,我终于找到了这个最好的答案,再次......非常感谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 2018-05-08
  • 1970-01-01
  • 2015-08-28
  • 2019-05-16
  • 2012-03-09
  • 2018-07-04
相关资源
最近更新 更多