# coding: utf-8
from django.http import HttpResponse
from django.http import JsonResponse
from django.views.generic import View
from rest_framework.views import APIView
from rest_framework.response import Response
from django.contrib.auth import authenticate
from django.contrib.auth.models import User as UserDjango
from bcrypt import hashpw, gensalt, checkpw
from categories.models import User
import datetime
class Register(APIView):
def post(self, request):
now = datetime.datetime.now
password = request.data['password'].encode('utf-8')
username = request.data['username'].encode('utf-8')
email = request.data['email'].encode('utf-8').lower()
if( User.objects.filter(email = email).count() == 1) :
return Response( { 'success': False, 'message': 'Email registered previously' })
else:
hashed = hashpw(password, gensalt() )
user = User(email=email, username=username, password=hashed, active='1', verified='0')
user.save()
return Response( { 'success': True, 'message': 'Succesful registration' })
class Login(APIView):
def post(self, request):
if(request.data['password'] == None or request.data['email'] == None):
return Response( { 'success': false, 'message': 'Username and password needed' } )
password = request.data['password'].encode('utf-8')
email = request.data['email'].encode('utf-8').lower()
user = User.objects.filter(email = email)
if user.count() == 0 :
return Response( { 'success': False, 'message': 'Email not registered' } )
但是,在进行测试时,出现下一个错误:
Internal Server Error: /user/login Traceback (most recent call last):
File "/usr/lib64/python3.6/site-packages/django/core/handlers/exception.py",line 35, in inner
response = get_response(request)
File "/usr/lib64/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/lib64/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib64/python3.6/site-packages/django/views/decorators/csrf.py",
line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/lib64/python3.6/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/usr/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/usr/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/ben/peace/user/views.py", line 50, in post
if( checkpw(password, passdb ) ):
File "/usr/lib64/python3.6/site-packages/bcrypt/__init__.py", line 107, in checkpw
ret = hashpw(password, hashed_password)
File "/usr/lib64/python3.6/site-packages/bcrypt/__init__.py", line 86, in hashpw
raise ValueError("Invalid salt") ValueError: Invalid salt
当我尝试调试它时,我得到了下一个要测试的参数:
数据库值:'b'$2b$12$E0pbZeraqxjv3z2ZgVtz8.G7P03ANuhDvYq2/q037GtR3lAUd1Uxm''
数据库值以代码b"b'$2b$12$E0pbZeraqxjv3z2ZgVtz8.G7P03ANuhDvYq2/q037GtR3lAUd1Uxm'"
我不确定为什么要在哈希之后复制b
。
有人知道我在做什么错以及如何解决?