f2a7c4b4 by 周伟奇

token modify part2

1 parent 4c8dca19
......@@ -6,6 +6,7 @@ from rest_framework import exceptions
from rest_framework_jwt.settings import api_settings
import jwt
from django.utils.translation import ugettext as _
from common.redis_cache import redis_handler as rh
jwt_decode_handler = api_settings.JWT_DECODE_HANDLER
......@@ -41,19 +42,20 @@ class MyJSONWebTokenAuthentication(JSONWebTokenAuthentication):
if jwt_value is None:
return None
print('jwt_value: {0}'.format(jwt_value))
# try:
# payload = jwt_decode_handler(jwt_value)
# except jwt.ExpiredSignature:
# msg = _('Signature has expired.')
# raise exceptions.AuthenticationFailed(msg)
jwt_str = str(jwt_value)[-10:]
is_expired = rh.get_token(jwt_str)
if isinstance(is_expired, str):
rh.set_token(jwt_str)
else:
msg = _('Signature has expired.')
raise exceptions.AuthenticationFailed(msg)
try:
payload = jwt_decode_handler(jwt_value)
except jwt.ExpiredSignature:
msg = _('Signature has expired.')
raise exceptions.AuthenticationFailed(msg)
# msg = _('Signature has expired.')
# raise exceptions.AuthenticationFailed(msg)
pass
except jwt.DecodeError:
msg = _('Error decoding signature.')
raise exceptions.AuthenticationFailed(msg)
......
......@@ -9,8 +9,8 @@ from settings import conf
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.contrib.auth import login as auth_login
from django.conf import settings
from django.shortcuts import resolve_url, redirect
# from django.conf import settings
# from django.shortcuts import resolve_url, redirect
# Create your views here.
......@@ -51,6 +51,7 @@ class LoginView(ObtainJSONWebToken, GenericView):
'user_name': user.username,
'token': res.data.get('token'),
}
rh.set_token(res.data.get('token')[-10:])
return response.ok(data=data)
......
......@@ -85,3 +85,12 @@ class RedisHandler:
if isinstance(expires, int):
self.redis.expire(key, expires)
def get_token_key(self, token_str):
return '{0}:token:{1}'.format(self.prefix, token_str)
def set_token(self, token_str, expires=1800):
return self.redis.set(self.get_token_key(token_str), 'token', expires)
def get_token(self, token_str):
return self.redis.get(self.get_token_key(token_str))
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!