token modify part4
Showing
3 changed files
with
40 additions
and
18 deletions
| ... | @@ -33,6 +33,29 @@ class OAuth2AuthenticationWithUser(OAuth2Authentication): | ... | @@ -33,6 +33,29 @@ class OAuth2AuthenticationWithUser(OAuth2Authentication): | 
| 33 | 33 | ||
| 34 | class MyJSONWebTokenAuthentication(JSONWebTokenAuthentication): | 34 | class MyJSONWebTokenAuthentication(JSONWebTokenAuthentication): | 
| 35 | 35 | ||
| 36 | def authenticate_credentials(self, username): | ||
| 37 | """ | ||
| 38 | Returns an active user that matches the payload's user id and email. | ||
| 39 | """ | ||
| 40 | User = get_user_model() | ||
| 41 | # username = jwt_get_username_from_payload(payload) | ||
| 42 | |||
| 43 | if not username: | ||
| 44 | msg = _('Invalid payload.') | ||
| 45 | raise exceptions.AuthenticationFailed(msg) | ||
| 46 | |||
| 47 | try: | ||
| 48 | user = User.objects.get_by_natural_key(username) | ||
| 49 | except User.DoesNotExist: | ||
| 50 | msg = _('Invalid signature.') | ||
| 51 | raise exceptions.AuthenticationFailed(msg) | ||
| 52 | |||
| 53 | if not user.is_active: | ||
| 54 | msg = _('User account is disabled.') | ||
| 55 | raise exceptions.AuthenticationFailed(msg) | ||
| 56 | |||
| 57 | return user | ||
| 58 | |||
| 36 | def authenticate(self, request): | 59 | def authenticate(self, request): | 
| 37 | """ | 60 | """ | 
| 38 | Returns a two-tuple of `User` and token if a valid signature has been | 61 | Returns a two-tuple of `User` and token if a valid signature has been | 
| ... | @@ -43,25 +66,24 @@ class MyJSONWebTokenAuthentication(JSONWebTokenAuthentication): | ... | @@ -43,25 +66,24 @@ class MyJSONWebTokenAuthentication(JSONWebTokenAuthentication): | 
| 43 | return None | 66 | return None | 
| 44 | 67 | ||
| 45 | jwt_str = str(jwt_value, 'UTF-8')[-10:] | 68 | jwt_str = str(jwt_value, 'UTF-8')[-10:] | 
| 46 | is_expired = rh.get_token(jwt_str) | 69 | username = rh.get_token(jwt_str) | 
| 47 | if isinstance(is_expired, str): | 70 | if isinstance(username, str): | 
| 48 | rh.set_token(jwt_str) | 71 | rh.set_token(jwt_str, username) | 
| 49 | else: | 72 | else: | 
| 50 | msg = _('Signature has expired.') | 73 | msg = _('Signature has expired.') | 
| 51 | raise exceptions.AuthenticationFailed(msg) | 74 | raise exceptions.AuthenticationFailed(msg) | 
| 52 | 75 | ||
| 53 | try: | 76 | # try: | 
| 54 | payload = jwt_decode_handler(jwt_value) | 77 | # payload = jwt_decode_handler(jwt_value) | 
| 55 | except jwt.ExpiredSignature: | 78 | # except jwt.ExpiredSignature: | 
| 56 | # msg = _('Signature has expired.') | 79 | # msg = _('Signature has expired.') | 
| 57 | # raise exceptions.AuthenticationFailed(msg) | 80 | # raise exceptions.AuthenticationFailed(msg) | 
| 58 | pass | 81 | # except jwt.DecodeError: | 
| 59 | except jwt.DecodeError: | 82 | # msg = _('Error decoding signature.') | 
| 60 | msg = _('Error decoding signature.') | 83 | # raise exceptions.AuthenticationFailed(msg) | 
| 61 | raise exceptions.AuthenticationFailed(msg) | 84 | # except jwt.InvalidTokenError: | 
| 62 | except jwt.InvalidTokenError: | 85 | # raise exceptions.AuthenticationFailed() | 
| 63 | raise exceptions.AuthenticationFailed() | ||
| 64 | 86 | ||
| 65 | user = self.authenticate_credentials(payload) | 87 | user = self.authenticate_credentials(username) | 
| 66 | 88 | ||
| 67 | return (user, jwt_value) | 89 | return (user, jwt_value) | ... | ... | 
| ... | @@ -51,7 +51,7 @@ class LoginView(ObtainJSONWebToken, GenericView): | ... | @@ -51,7 +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 | rh.set_token(res.data.get('token')[-10:], user.username) | 
| 55 | return response.ok(data=data) | 55 | return response.ok(data=data) | 
| 56 | 56 | ||
| 57 | 57 | ... | ... | 
| ... | @@ -88,8 +88,8 @@ class RedisHandler: | ... | @@ -88,8 +88,8 @@ class RedisHandler: | 
| 88 | def get_token_key(self, token_str): | 88 | def get_token_key(self, token_str): | 
| 89 | return '{0}:token:{1}'.format(self.prefix, token_str) | 89 | return '{0}:token:{1}'.format(self.prefix, token_str) | 
| 90 | 90 | ||
| 91 | def set_token(self, token_str, expires=1800): | 91 | def set_token(self, token_str, username, expires=1800): | 
| 92 | return self.redis.set(self.get_token_key(token_str), 'token', expires) | 92 | return self.redis.set(self.get_token_key(token_str), username, expires) | 
| 93 | 93 | ||
| 94 | def get_token(self, token_str): | 94 | def get_token(self, token_str): | 
| 95 | return self.redis.get(self.get_token_key(token_str)) | 95 | return self.redis.get(self.get_token_key(token_str)) | ... | ... | 
- 
Please register or sign in to post a comment