mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 09:57:55 +03:00 
			
		
		
		
	Improve the django-oauth2-provider import block
				
					
				
			to avoid naming collision with `oauth2` used for OAuth 1
This commit is contained in:
		
							parent
							
								
									aed3c13471
								
							
						
					
					
						commit
						9d5c306038
					
				| 
						 | 
				
			
			@ -6,7 +6,7 @@ from django.contrib.auth import authenticate
 | 
			
		|||
from django.utils.encoding import DjangoUnicodeDecodeError
 | 
			
		||||
from rest_framework import exceptions, HTTP_HEADER_ENCODING
 | 
			
		||||
from rest_framework.compat import CsrfViewMiddleware
 | 
			
		||||
from rest_framework.compat import oauth2_provider, oauth2
 | 
			
		||||
from rest_framework.compat import oauth2_provider
 | 
			
		||||
from rest_framework.authtoken.models import Token
 | 
			
		||||
import base64
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -190,13 +190,13 @@ class OAuth2Authentication(BaseAuthentication):
 | 
			
		|||
        """
 | 
			
		||||
 | 
			
		||||
        # authenticate the client
 | 
			
		||||
        oauth2_client_form = oauth2.forms.ClientAuthForm(request.REQUEST)
 | 
			
		||||
        oauth2_client_form = oauth2_provider.forms.ClientAuthForm(request.REQUEST)
 | 
			
		||||
        if not oauth2_client_form.is_valid():
 | 
			
		||||
            raise exceptions.AuthenticationFailed("Client could not be validated")
 | 
			
		||||
        client = oauth2_client_form.cleaned_data.get('client')
 | 
			
		||||
 | 
			
		||||
        # retrieve the `oauth2.models.OAuth2AccessToken` instance from the access_token
 | 
			
		||||
        auth_backend = oauth2.backends.AccessTokenBackend()
 | 
			
		||||
        # retrieve the `oauth2_provider.models.OAuth2AccessToken` instance from the access_token
 | 
			
		||||
        auth_backend = oauth2_provider.backends.AccessTokenBackend()
 | 
			
		||||
        token = auth_backend.authenticate(access_token, client)
 | 
			
		||||
        if token is None:
 | 
			
		||||
            raise exceptions.AuthenticationFailed("Invalid token")  # does not exist or is expired
 | 
			
		||||
| 
						 | 
				
			
			@ -204,7 +204,7 @@ class OAuth2Authentication(BaseAuthentication):
 | 
			
		|||
        # TODO check scope
 | 
			
		||||
        # try:
 | 
			
		||||
        #     self.validate_token(request, consumer, token)
 | 
			
		||||
        # except oauth2.Error, e:
 | 
			
		||||
        # except oauth2_provider.Error, e:
 | 
			
		||||
        #     print "got e"
 | 
			
		||||
        #     raise exceptions.AuthenticationFailed(e.message)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -430,10 +430,6 @@ except ImportError:
 | 
			
		|||
 | 
			
		||||
# OAuth 2 support is optional
 | 
			
		||||
try:
 | 
			
		||||
    import provider as oauth2_provider
 | 
			
		||||
    import provider.oauth2 as oauth2_provider
 | 
			
		||||
except ImportError:
 | 
			
		||||
    oauth2_provider = None
 | 
			
		||||
try:
 | 
			
		||||
    import provider.oauth2 as oauth2
 | 
			
		||||
except ImportError:
 | 
			
		||||
    oauth2 = None
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,7 +16,6 @@ from rest_framework.authentication import (
 | 
			
		|||
    OAuth2Authentication
 | 
			
		||||
)
 | 
			
		||||
from rest_framework.compat import patterns, url, include
 | 
			
		||||
from rest_framework.compat import oauth2
 | 
			
		||||
from rest_framework.compat import oauth2_provider
 | 
			
		||||
from rest_framework.tests.utils import RequestFactory
 | 
			
		||||
from rest_framework.views import APIView
 | 
			
		||||
| 
						 | 
				
			
			@ -248,7 +247,7 @@ class OAuth2Tests(TestCase):
 | 
			
		|||
        self.ACCESS_TOKEN = "access_token"
 | 
			
		||||
        self.REFRESH_TOKEN = "refresh_token"
 | 
			
		||||
 | 
			
		||||
        self.oauth2_client = oauth2.models.Client.objects.create(
 | 
			
		||||
        self.oauth2_client = oauth2_provider.models.Client.objects.create(
 | 
			
		||||
                client_id=self.CLIENT_ID, 
 | 
			
		||||
                client_secret=self.CLIENT_SECRET,
 | 
			
		||||
                redirect_uri='',
 | 
			
		||||
| 
						 | 
				
			
			@ -257,12 +256,12 @@ class OAuth2Tests(TestCase):
 | 
			
		|||
                user=None,
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        self.access_token = oauth2.models.AccessToken.objects.create(
 | 
			
		||||
        self.access_token = oauth2_provider.models.AccessToken.objects.create(
 | 
			
		||||
                token=self.ACCESS_TOKEN,
 | 
			
		||||
                client=self.oauth2_client,
 | 
			
		||||
                user=self.user,
 | 
			
		||||
            )
 | 
			
		||||
        self.refresh_token = oauth2.models.RefreshToken.objects.create(
 | 
			
		||||
        self.refresh_token = oauth2_provider.models.RefreshToken.objects.create(
 | 
			
		||||
                user=self.user,
 | 
			
		||||
                access_token=self.access_token,
 | 
			
		||||
                client=self.oauth2_client
 | 
			
		||||
| 
						 | 
				
			
			@ -274,7 +273,7 @@ class OAuth2Tests(TestCase):
 | 
			
		|||
    def _client_credentials_params(self):
 | 
			
		||||
        return {'client_id': self.CLIENT_ID, 'client_secret': self.CLIENT_SECRET}
 | 
			
		||||
 | 
			
		||||
    @unittest.skipUnless(oauth2, 'django-oauth2-provider not installed')
 | 
			
		||||
    @unittest.skipUnless(oauth2_provider, 'django-oauth2-provider not installed')
 | 
			
		||||
    def test_get_form_with_wrong_client_data_failing_auth(self):
 | 
			
		||||
        """Ensure GETing form over OAuth with incorrect client credentials fails"""
 | 
			
		||||
        auth = self._create_authorization_header()
 | 
			
		||||
| 
						 | 
				
			
			@ -283,7 +282,7 @@ class OAuth2Tests(TestCase):
 | 
			
		|||
        response = self.csrf_client.get('/oauth2-test/', params, HTTP_AUTHORIZATION=auth)
 | 
			
		||||
        self.assertEqual(response.status_code, 401)
 | 
			
		||||
 | 
			
		||||
    @unittest.skipUnless(oauth2, 'django-oauth2-provider not installed')
 | 
			
		||||
    @unittest.skipUnless(oauth2_provider, 'django-oauth2-provider not installed')
 | 
			
		||||
    def test_get_form_passing_auth(self):
 | 
			
		||||
        """Ensure GETing form over OAuth with correct client credentials succeed"""
 | 
			
		||||
        auth = self._create_authorization_header()
 | 
			
		||||
| 
						 | 
				
			
			@ -291,7 +290,7 @@ class OAuth2Tests(TestCase):
 | 
			
		|||
        response = self.csrf_client.get('/oauth2-test/', params, HTTP_AUTHORIZATION=auth)
 | 
			
		||||
        self.assertEqual(response.status_code, 200)
 | 
			
		||||
 | 
			
		||||
    @unittest.skipUnless(oauth2, 'django-oauth2-provider not installed')
 | 
			
		||||
    @unittest.skipUnless(oauth2_provider, 'django-oauth2-provider not installed')
 | 
			
		||||
    def test_post_form_passing_auth(self):
 | 
			
		||||
        """Ensure POSTing form over OAuth with correct credentials passes and does not require CSRF"""
 | 
			
		||||
        auth = self._create_authorization_header()
 | 
			
		||||
| 
						 | 
				
			
			@ -299,7 +298,7 @@ class OAuth2Tests(TestCase):
 | 
			
		|||
        response = self.csrf_client.post('/oauth2-test/', params, HTTP_AUTHORIZATION=auth)
 | 
			
		||||
        self.assertEqual(response.status_code, 200)
 | 
			
		||||
 | 
			
		||||
    @unittest.skipUnless(oauth2, 'django-oauth2-provider not installed')
 | 
			
		||||
    @unittest.skipUnless(oauth2_provider, 'django-oauth2-provider not installed')
 | 
			
		||||
    def test_post_form_token_removed_failing_auth(self):
 | 
			
		||||
        """Ensure POSTing when there is no OAuth access token in db fails"""
 | 
			
		||||
        self.access_token.delete()
 | 
			
		||||
| 
						 | 
				
			
			@ -308,7 +307,7 @@ class OAuth2Tests(TestCase):
 | 
			
		|||
        response = self.csrf_client.post('/oauth2-test/', params, HTTP_AUTHORIZATION=auth)
 | 
			
		||||
        self.assertIn(response.status_code, (status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN))
 | 
			
		||||
 | 
			
		||||
    @unittest.skipUnless(oauth2, 'django-oauth2-provider not installed')
 | 
			
		||||
    @unittest.skipUnless(oauth2_provider, 'django-oauth2-provider not installed')
 | 
			
		||||
    def test_post_form_with_refresh_token_failing_auth(self):
 | 
			
		||||
        """Ensure POSTing with refresh token instead of access token fails"""
 | 
			
		||||
        auth = self._create_authorization_header(token=self.refresh_token.token)
 | 
			
		||||
| 
						 | 
				
			
			@ -316,7 +315,7 @@ class OAuth2Tests(TestCase):
 | 
			
		|||
        response = self.csrf_client.post('/oauth2-test/', params, HTTP_AUTHORIZATION=auth)
 | 
			
		||||
        self.assertIn(response.status_code, (status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN))
 | 
			
		||||
 | 
			
		||||
    @unittest.skipUnless(oauth2, 'django-oauth2-provider not installed')
 | 
			
		||||
    @unittest.skipUnless(oauth2_provider, 'django-oauth2-provider not installed')
 | 
			
		||||
    def test_post_form_with_expired_access_token_failing_auth(self):
 | 
			
		||||
        """Ensure POSTing with expired access token fails with an 'Invalid token' error"""
 | 
			
		||||
        self.access_token.expires = datetime.datetime.now() - datetime.timedelta(seconds=10)  # 10 seconds late
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user