token modify part2
Showing
3 changed files
with
23 additions
and
11 deletions
... | @@ -6,6 +6,7 @@ from rest_framework import exceptions | ... | @@ -6,6 +6,7 @@ from rest_framework import exceptions |
6 | from rest_framework_jwt.settings import api_settings | 6 | from rest_framework_jwt.settings import api_settings |
7 | import jwt | 7 | import jwt |
8 | from django.utils.translation import ugettext as _ | 8 | from django.utils.translation import ugettext as _ |
9 | from common.redis_cache import redis_handler as rh | ||
9 | 10 | ||
10 | jwt_decode_handler = api_settings.JWT_DECODE_HANDLER | 11 | jwt_decode_handler = api_settings.JWT_DECODE_HANDLER |
11 | 12 | ||
... | @@ -41,19 +42,20 @@ class MyJSONWebTokenAuthentication(JSONWebTokenAuthentication): | ... | @@ -41,19 +42,20 @@ class MyJSONWebTokenAuthentication(JSONWebTokenAuthentication): |
41 | if jwt_value is None: | 42 | if jwt_value is None: |
42 | return None | 43 | return None |
43 | 44 | ||
44 | print('jwt_value: {0}'.format(jwt_value)) | 45 | jwt_str = str(jwt_value)[-10:] |
45 | 46 | is_expired = rh.get_token(jwt_str) | |
46 | # try: | 47 | if isinstance(is_expired, str): |
47 | # payload = jwt_decode_handler(jwt_value) | 48 | rh.set_token(jwt_str) |
48 | # except jwt.ExpiredSignature: | 49 | else: |
49 | # msg = _('Signature has expired.') | 50 | msg = _('Signature has expired.') |
50 | # raise exceptions.AuthenticationFailed(msg) | 51 | raise exceptions.AuthenticationFailed(msg) |
51 | 52 | ||
52 | try: | 53 | try: |
53 | payload = jwt_decode_handler(jwt_value) | 54 | payload = jwt_decode_handler(jwt_value) |
54 | except jwt.ExpiredSignature: | 55 | except jwt.ExpiredSignature: |
55 | msg = _('Signature has expired.') | 56 | # msg = _('Signature has expired.') |
56 | raise exceptions.AuthenticationFailed(msg) | 57 | # raise exceptions.AuthenticationFailed(msg) |
58 | pass | ||
57 | except jwt.DecodeError: | 59 | except jwt.DecodeError: |
58 | msg = _('Error decoding signature.') | 60 | msg = _('Error decoding signature.') |
59 | raise exceptions.AuthenticationFailed(msg) | 61 | raise exceptions.AuthenticationFailed(msg) | ... | ... |
... | @@ -9,8 +9,8 @@ from settings import conf | ... | @@ -9,8 +9,8 @@ from settings import conf |
9 | from django.urls import reverse | 9 | from django.urls import reverse |
10 | from django.http import HttpResponseRedirect | 10 | from django.http import HttpResponseRedirect |
11 | from django.contrib.auth import login as auth_login | 11 | from django.contrib.auth import login as auth_login |
12 | from django.conf import settings | 12 | # from django.conf import settings |
13 | from django.shortcuts import resolve_url, redirect | 13 | # from django.shortcuts import resolve_url, redirect |
14 | 14 | ||
15 | # Create your views here. | 15 | # Create your views here. |
16 | 16 | ||
... | @@ -51,6 +51,7 @@ class LoginView(ObtainJSONWebToken, GenericView): | ... | @@ -51,6 +51,7 @@ class LoginView(ObtainJSONWebToken, GenericView): |
51 | 'user_name': user.username, | 51 | 'user_name': user.username, |
52 | 'token': res.data.get('token'), | 52 | 'token': res.data.get('token'), |
53 | } | 53 | } |
54 | rh.set_token(res.data.get('token')[-10:]) | ||
54 | return response.ok(data=data) | 55 | return response.ok(data=data) |
55 | 56 | ||
56 | 57 | ... | ... |
... | @@ -85,3 +85,12 @@ class RedisHandler: | ... | @@ -85,3 +85,12 @@ class RedisHandler: |
85 | if isinstance(expires, int): | 85 | if isinstance(expires, int): |
86 | self.redis.expire(key, expires) | 86 | self.redis.expire(key, expires) |
87 | 87 | ||
88 | def get_token_key(self, token_str): | ||
89 | return '{0}:token:{1}'.format(self.prefix, token_str) | ||
90 | |||
91 | def set_token(self, token_str, expires=1800): | ||
92 | return self.redis.set(self.get_token_key(token_str), 'token', expires) | ||
93 | |||
94 | def get_token(self, token_str): | ||
95 | return self.redis.get(self.get_token_key(token_str)) | ||
96 | ... | ... |
-
Please register or sign in to post a comment