Skip to content

Commit

Permalink
Improve the django-oauth2-provider import block
Browse files Browse the repository at this point in the history
to avoid naming collision with `oauth2` used for OAuth 1
  • Loading branch information
dulacp committed Mar 1, 2013
1 parent aed3c13 commit 9d5c306
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
10 changes: 5 additions & 5 deletions rest_framework/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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

Expand Down Expand Up @@ -190,21 +190,21 @@ def authenticate_credentials(self, request, access_token):
"""

# 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

# 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)

Expand Down
6 changes: 1 addition & 5 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,6 @@ def apply_markdown(text):

# 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
19 changes: 9 additions & 10 deletions rest_framework/tests/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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
Expand Down Expand Up @@ -248,7 +247,7 @@ def setUp(self):
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='',
Expand All @@ -257,12 +256,12 @@ def setUp(self):
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
Expand All @@ -274,7 +273,7 @@ def _create_authorization_header(self, token=None):
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()
Expand All @@ -283,23 +282,23 @@ def test_get_form_with_wrong_client_data_failing_auth(self):
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()
params = self._client_credentials_params()
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()
params = self._client_credentials_params()
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()
Expand All @@ -308,15 +307,15 @@ def test_post_form_token_removed_failing_auth(self):
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)
params = self._client_credentials_params()
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
Expand Down

0 comments on commit 9d5c306

Please sign in to comment.