token modify part1
Showing
2 changed files
with
44 additions
and
1 deletions
1 | from django.contrib.auth import get_user_model | 1 | from django.contrib.auth import get_user_model |
2 | from oauth2_provider.contrib.rest_framework import OAuth2Authentication | 2 | from oauth2_provider.contrib.rest_framework import OAuth2Authentication |
3 | from oauth2_provider.oauth2_backends import get_oauthlib_core | 3 | from oauth2_provider.oauth2_backends import get_oauthlib_core |
4 | from rest_framework_jwt.authentication import JSONWebTokenAuthentication | ||
5 | from rest_framework import exceptions | ||
6 | from rest_framework_jwt.settings import api_settings | ||
7 | import jwt | ||
8 | from django.utils.translation import ugettext as _ | ||
9 | |||
10 | jwt_decode_handler = api_settings.JWT_DECODE_HANDLER | ||
4 | 11 | ||
5 | 12 | ||
6 | class OAuth2AuthenticationWithUser(OAuth2Authentication): | 13 | class OAuth2AuthenticationWithUser(OAuth2Authentication): |
... | @@ -21,3 +28,38 @@ class OAuth2AuthenticationWithUser(OAuth2Authentication): | ... | @@ -21,3 +28,38 @@ class OAuth2AuthenticationWithUser(OAuth2Authentication): |
21 | return self.user, r.access_token | 28 | return self.user, r.access_token |
22 | request.oauth2_error = getattr(r, "oauth2_error", {}) | 29 | request.oauth2_error = getattr(r, "oauth2_error", {}) |
23 | return None | 30 | return None |
31 | |||
32 | |||
33 | class MyJSONWebTokenAuthentication(JSONWebTokenAuthentication): | ||
34 | |||
35 | def authenticate(self, request): | ||
36 | """ | ||
37 | Returns a two-tuple of `User` and token if a valid signature has been | ||
38 | supplied using JWT-based authentication. Otherwise returns `None`. | ||
39 | """ | ||
40 | jwt_value = self.get_jwt_value(request) | ||
41 | if jwt_value is None: | ||
42 | return None | ||
43 | |||
44 | print('jwt_value: {0}'.format(jwt_value)) | ||
45 | |||
46 | # try: | ||
47 | # payload = jwt_decode_handler(jwt_value) | ||
48 | # except jwt.ExpiredSignature: | ||
49 | # msg = _('Signature has expired.') | ||
50 | # raise exceptions.AuthenticationFailed(msg) | ||
51 | |||
52 | try: | ||
53 | payload = jwt_decode_handler(jwt_value) | ||
54 | except jwt.ExpiredSignature: | ||
55 | msg = _('Signature has expired.') | ||
56 | raise exceptions.AuthenticationFailed(msg) | ||
57 | except jwt.DecodeError: | ||
58 | msg = _('Error decoding signature.') | ||
59 | raise exceptions.AuthenticationFailed(msg) | ||
60 | except jwt.InvalidTokenError: | ||
61 | raise exceptions.AuthenticationFailed() | ||
62 | |||
63 | user = self.authenticate_credentials(payload) | ||
64 | |||
65 | return (user, jwt_value) | ... | ... |
... | @@ -156,7 +156,8 @@ REST_FRAMEWORK = { | ... | @@ -156,7 +156,8 @@ REST_FRAMEWORK = { |
156 | ), | 156 | ), |
157 | 'DEFAULT_AUTHENTICATION_CLASSES': ( | 157 | 'DEFAULT_AUTHENTICATION_CLASSES': ( |
158 | 'rest_framework.authentication.BasicAuthentication', | 158 | 'rest_framework.authentication.BasicAuthentication', |
159 | 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', | 159 | 'apps.account.authentication.MyJSONWebTokenAuthentication', |
160 | # 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', | ||
160 | # 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', | 161 | # 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', |
161 | ), | 162 | ), |
162 | 'EXCEPTION_HANDLER': 'common.exceptions.exception_handler' | 163 | 'EXCEPTION_HANDLER': 'common.exceptions.exception_handler' | ... | ... |
-
Please register or sign in to post a comment