我正在尝试对 Flask 应用程序实施JWT授权。但是,当我尝试登录时,它返回一个"500 Internal server error"
带有“TypeError:哈希必须是 unicode 或字节,而不是 builtin_function_or_method”。我会在这里错过什么。
这是我的models.py课程
from passlib.hash import pbkdf2_sha256
# Create classes to store data.
class User:
""" This class provides a way to store user data. """
users = [
{
'id': 1,
'username': u'mwinel',
'email': u'mwinel@example.com',
'password': u'code618'
},
{
'id': 2,
'username': u'lucy',
'email': u'lucy@example.com',
'password': u'123456'
}
]
def __init__(self, id, username, email, password):
""" Initialize objects. """
self.id = user_id,
self.username = username,
self.email = email,
self.password = password
@staticmethod
# Generate a hashed string to be
# stored by our class model.
def generate_hash(password):
hash = pbkdf2_sha256.encrypt(password, rounds = 20000, salt_size = 16)
return hash
@staticmethod
# Check a given password.
def verify_hash(password, hash):
return pbkdf2_sha256.verify(password, hash)
这是我的登录方法
from flask_restful import Resource, reqparse
from app.models import User
class UserLogin(Resource):
# Call the method to login a user.
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('username', required = True)
parser.add_argument('password', required = True)
data = parser.parse_args()
username = data['username']
password = data['password']
for user in User.users:
if password == User.verify_hash(data['password'], hash):
return {
'message': 'Logged in as {}'.format(data['username']),
}, 200
return {
'message': 'Something went wrong'
}, 500
这是错误
TypeError: hash must be unicode or bytes, not builtin_function_or_method