mirror of
				https://github.com/Tivix/django-rest-auth.git
				synced 2025-10-31 07:47:33 +03:00 
			
		
		
		
	Moved jwt auth class to separate file to avoid hard dependency.
This commit is contained in:
		
							parent
							
								
									8568c8221b
								
							
						
					
					
						commit
						b2c06fa18a
					
				|  | @ -123,7 +123,7 @@ REST_FRAMEWORK = { | |||
|     'DEFAULT_AUTHENTICATION_CLASSES': ( | ||||
|         'rest_framework.authentication.SessionAuthentication', | ||||
|         'rest_framework.authentication.TokenAuthentication', | ||||
|         'dj_rest_auth.utils.JWTCookieAuthentication' | ||||
|         'dj_rest_auth.jwt_auth.JWTCookieAuthentication' | ||||
|     ), | ||||
|     'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema' | ||||
| } | ||||
|  |  | |||
							
								
								
									
										27
									
								
								dj_rest_auth/jwt_auth.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								dj_rest_auth/jwt_auth.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,27 @@ | |||
| from django.conf import settings | ||||
| from rest_framework_simplejwt.authentication import JWTAuthentication | ||||
| 
 | ||||
| 
 | ||||
| class JWTCookieAuthentication(JWTAuthentication): | ||||
|     """ | ||||
|     An authentication plugin that hopefully authenticates requests through a JSON web | ||||
|     token provided in a request cookie (and through the header as normal, with a | ||||
|     preference to the header). | ||||
|     """ | ||||
| 
 | ||||
|     def authenticate(self, request): | ||||
|         cookie_name = getattr(settings, 'JWT_AUTH_COOKIE', None) | ||||
|         header = self.get_header(request) | ||||
|         if header is None: | ||||
|             if cookie_name: | ||||
|                 raw_token = request.COOKIES.get(cookie_name) | ||||
|             else: | ||||
|                 return None | ||||
|         else: | ||||
|             raw_token = self.get_raw_token(header) | ||||
| 
 | ||||
|         if raw_token is None: | ||||
|             return None | ||||
| 
 | ||||
|         validated_token = self.get_validated_token(raw_token) | ||||
|         return self.get_user(validated_token), validated_token | ||||
|  | @ -68,7 +68,7 @@ TEMPLATES = [ | |||
| REST_FRAMEWORK = { | ||||
|     'DEFAULT_AUTHENTICATION_CLASSES': ( | ||||
|         'rest_framework.authentication.SessionAuthentication', | ||||
|         'dj_rest_auth.utils.JWTCookieAuthentication', | ||||
|         'dj_rest_auth.jwt_auth.JWTCookieAuthentication', | ||||
|     ) | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -563,7 +563,7 @@ class APIBasicTests(TestsMixin, TestCase): | |||
|     @override_settings(JWT_AUTH_COOKIE='jwt-auth') | ||||
|     @override_settings(REST_FRAMEWORK=dict( | ||||
|         DEFAULT_AUTHENTICATION_CLASSES=[ | ||||
|             'dj_rest_auth.utils.JWTCookieAuthentication' | ||||
|             'dj_rest_auth.jwt_auth.JWTCookieAuthentication' | ||||
|         ] | ||||
|     )) | ||||
|     @override_settings(REST_SESSION_LOGIN=False) | ||||
|  | @ -624,7 +624,7 @@ class APIBasicTests(TestsMixin, TestCase): | |||
|     @override_settings(JWT_AUTH_COOKIE=None) | ||||
|     @override_settings(REST_FRAMEWORK=dict( | ||||
|         DEFAULT_AUTHENTICATION_CLASSES=[ | ||||
|             'dj_rest_auth.utils.JWTCookieAuthentication' | ||||
|             'dj_rest_auth.jwt_auth.JWTCookieAuthentication' | ||||
|         ] | ||||
|     )) | ||||
|     @override_settings(REST_SESSION_LOGIN=False) | ||||
|  | @ -649,7 +649,7 @@ class APIBasicTests(TestsMixin, TestCase): | |||
|     @override_settings(JWT_AUTH_COOKIE='jwt-auth') | ||||
|     @override_settings(REST_FRAMEWORK=dict( | ||||
|         DEFAULT_AUTHENTICATION_CLASSES=[ | ||||
|             'dj_rest_auth.utils.JWTCookieAuthentication' | ||||
|             'dj_rest_auth.jwt_auth.JWTCookieAuthentication' | ||||
|         ] | ||||
|     )) | ||||
|     @override_settings(REST_SESSION_LOGIN=False) | ||||
|  |  | |||
|  | @ -1,5 +1,7 @@ | |||
| from importlib import import_module | ||||
| 
 | ||||
| from django.conf import settings | ||||
| 
 | ||||
| 
 | ||||
| def import_callable(path_or_callable): | ||||
|     if hasattr(path_or_callable, '__call__'): | ||||
|  | @ -15,38 +17,14 @@ def default_create_token(token_model, user, serializer): | |||
|     return token | ||||
| 
 | ||||
| 
 | ||||
| try: | ||||
|     from django.conf import settings | ||||
|     from rest_framework_simplejwt.authentication import JWTAuthentication | ||||
| def jwt_encode(user): | ||||
|     from rest_framework_simplejwt.serializers import TokenObtainPairSerializer | ||||
|     TOPS = import_callable(getattr(settings, 'JWT_TOKEN_CLAIMS_SERIALIZER', TokenObtainPairSerializer)) | ||||
|     refresh = TOPS.get_token(user) | ||||
|     return refresh.access_token, refresh | ||||
| 
 | ||||
|     def jwt_encode(user): | ||||
|         TOPS = import_callable(getattr(settings, 'JWT_TOKEN_CLAIMS_SERIALIZER', TokenObtainPairSerializer)) | ||||
|         refresh = TOPS.get_token(user) | ||||
|         return refresh.access_token, refresh | ||||
| 
 | ||||
|     class JWTCookieAuthentication(JWTAuthentication): | ||||
|         """ | ||||
|         An authentication plugin that hopefully authenticates requests through a JSON web | ||||
|         token provided in a request cookie (and through the header as normal, with a | ||||
|         preference to the header). | ||||
|         """ | ||||
|         def authenticate(self, request): | ||||
|             cookie_name = getattr(settings, 'JWT_AUTH_COOKIE', None) | ||||
|             header = self.get_header(request) | ||||
|             if header is None: | ||||
|                 if cookie_name: | ||||
|                     raw_token = request.COOKIES.get(cookie_name) | ||||
|                 else: | ||||
|                     return None | ||||
|             else: | ||||
|                 raw_token = self.get_raw_token(header) | ||||
| 
 | ||||
|             if raw_token is None: | ||||
|                 return None | ||||
| 
 | ||||
|             validated_token = self.get_validated_token(raw_token) | ||||
|             return self.get_user(validated_token), validated_token | ||||
| 
 | ||||
| try: | ||||
|     from .jwt_auth import JWTCookieAuthentication | ||||
| except ImportError: | ||||
|     raise ImportError("rest-framework-simplejwt needs to be installed") | ||||
|     pass | ||||
|  |  | |||
|  | @ -35,9 +35,9 @@ Installation | |||
| 
 | ||||
| .. code-block:: python | ||||
| 
 | ||||
|     python manage.py migrate  | ||||
|      | ||||
|      | ||||
|     python manage.py migrate | ||||
| 
 | ||||
| 
 | ||||
| You're good to go now! | ||||
| 
 | ||||
| 
 | ||||
|  | @ -59,7 +59,7 @@ Registration (optional) | |||
|         'allauth.account', | ||||
|         'dj_rest_auth.registration', | ||||
|     ) | ||||
|      | ||||
| 
 | ||||
|     SITE_ID = 1 | ||||
| 
 | ||||
| 3. Add dj_rest_auth.registration urls: | ||||
|  | @ -76,7 +76,7 @@ Registration (optional) | |||
| Social Authentication (optional) | ||||
| -------------------------------- | ||||
| 
 | ||||
| Using ``django-allauth``, ``dj-rest-auth`` provides helpful class for creating social media authentication view.  | ||||
| Using ``django-allauth``, ``dj-rest-auth`` provides helpful class for creating social media authentication view. | ||||
| 
 | ||||
| .. note:: Points 1 and 2 are related to ``django-allauth`` configuration, so if you have already configured social authentication, then please go to step 3. See ``django-allauth`` documentation for more details. | ||||
| 
 | ||||
|  | @ -223,7 +223,7 @@ In urls.py: | |||
| You can also use the following views to check all social accounts attached to the current authenticated user and disconnect selected social accounts: | ||||
| 
 | ||||
| .. code-block:: python | ||||
|      | ||||
| 
 | ||||
|     from dj_rest_auth.registration.views import ( | ||||
|         SocialAccountListView, SocialAccountDisconnectView | ||||
|     ) | ||||
|  | @ -259,7 +259,7 @@ By default ``dj-rest-auth`` uses Django's Token-based authentication. If you wan | |||
|         ... | ||||
|         'DEFAULT_AUTHENTICATION_CLASSES': ( | ||||
|             ... | ||||
|             'dj_rest_auth.utils.JWTCookieAuthentication', | ||||
|             'dj_rest_auth.jwt_auth.JWTCookieAuthentication', | ||||
|         ) | ||||
|         ... | ||||
|     } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user