修复python3.6.13+django2.2+djangorestframework 3.12.4
使用djangorestframework_simplejwt-4.4.0-py3时,产生的两个bug
bug01:
Internal Server Error: /login/
Traceback (most recent call last):
File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\views.py", line 27, in post
serializer.is_valid(raise_exception=True)
File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\serializers.py", line 220, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\serializers.py", line 422, in run_validation
value = self.validate(value)
File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\serializers.py", line 78, in validate
data['refresh'] = str(refresh)
File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\tokens.py", line 82, in __str__
return token_backend.encode(self.payload)
File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\backends.py", line 43, in encode
return token.decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'
解决方式:
rest_framework_simplejwt文件夹下backends.py文件里
class TokenBackend:
def encode(self, payload):
"""
Returns an encoded token for the given payload dictionary.
"""
jwt_payload = payload.copy()
if self.audience is not None:
jwt_payload['aud'] = self.audience
if self.issuer is not None:
jwt_payload['iss'] = self.issuer
token = jwt.encode(jwt_payload, self.signing_key, algorithm=self.algorithm)
return token # .decode('utf-8') 修改的地方
bug02:
Internal Server Error: /user_center/list/
Traceback (most recent call last):
File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\views.py", line 497, in dispatch
self.initial(request, *args, **kwargs)
File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\views.py", line 414, in initial
self.perform_authentication(request)
File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\views.py", line 324, in perform_authentication
request.user
File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\request.py", line 227, in user
self._authenticate()
File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\request.py", line 380, in _authenticate
user_auth_tuple = authenticator.authenticate(self)
File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\authentication.py", line 36, in authenticate
validated_token = self.get_validated_token(raw_token)
File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\authentication.py", line 90, in get_validated_token
return AuthToken(raw_token)
File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\tokens.py", line 42, in __init__
self.payload = token_backend.decode(token, verify=verify)
File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\backends.py", line 56, in decode
options={'verify_aud': self.audience is not None})
TypeError: decode() got an unexpected keyword argument 'verify'
解决方式:
rest_framework_simplejwt文件夹下backends.py文件里
class TokenBackend:
def decode(self, token, verify=True):
"""
Performs a validation of the given token and returns its payload
dictionary.
Raises a `TokenBackendError` if the token is malformed, if its
signature check fails, or if its 'exp' claim indicates it has expired.
"""
try:
return jwt.decode(token.decode('utf-8'), self.verifying_key, algorithms=[self.algorithm], # verify=verify, 修改的地方
audience=self.audience, issuer=self.issuer,
options={'verify_aud': self.audience is not None})
except InvalidTokenError:
raise TokenBackendError(_('Token is invalid or expired'))