Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
9d5c3060
Commit
9d5c3060
authored
Mar 01, 2013
by
Pierre Dulac
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve the `django-oauth2-provider` import block
to avoid naming collision with `oauth2` used for OAuth 1
parent
aed3c134
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
20 deletions
+15
-20
rest_framework/authentication.py
+5
-5
rest_framework/compat.py
+1
-5
rest_framework/tests/authentication.py
+9
-10
No files found.
rest_framework/authentication.py
View file @
9d5c3060
...
@@ -6,7 +6,7 @@ from django.contrib.auth import authenticate
...
@@ -6,7 +6,7 @@ from django.contrib.auth import authenticate
from
django.utils.encoding
import
DjangoUnicodeDecodeError
from
django.utils.encoding
import
DjangoUnicodeDecodeError
from
rest_framework
import
exceptions
,
HTTP_HEADER_ENCODING
from
rest_framework
import
exceptions
,
HTTP_HEADER_ENCODING
from
rest_framework.compat
import
CsrfViewMiddleware
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
from
rest_framework.authtoken.models
import
Token
import
base64
import
base64
...
@@ -190,13 +190,13 @@ class OAuth2Authentication(BaseAuthentication):
...
@@ -190,13 +190,13 @@ class OAuth2Authentication(BaseAuthentication):
"""
"""
# authenticate the client
# 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
():
if
not
oauth2_client_form
.
is_valid
():
raise
exceptions
.
AuthenticationFailed
(
"Client could not be validated"
)
raise
exceptions
.
AuthenticationFailed
(
"Client could not be validated"
)
client
=
oauth2_client_form
.
cleaned_data
.
get
(
'client'
)
client
=
oauth2_client_form
.
cleaned_data
.
get
(
'client'
)
# retrieve the `oauth2.models.OAuth2AccessToken` instance from the access_token
# retrieve the `oauth2
_provider
.models.OAuth2AccessToken` instance from the access_token
auth_backend
=
oauth2
.
backends
.
AccessTokenBackend
()
auth_backend
=
oauth2
_provider
.
backends
.
AccessTokenBackend
()
token
=
auth_backend
.
authenticate
(
access_token
,
client
)
token
=
auth_backend
.
authenticate
(
access_token
,
client
)
if
token
is
None
:
if
token
is
None
:
raise
exceptions
.
AuthenticationFailed
(
"Invalid token"
)
# does not exist or is expired
raise
exceptions
.
AuthenticationFailed
(
"Invalid token"
)
# does not exist or is expired
...
@@ -204,7 +204,7 @@ class OAuth2Authentication(BaseAuthentication):
...
@@ -204,7 +204,7 @@ class OAuth2Authentication(BaseAuthentication):
# TODO check scope
# TODO check scope
# try:
# try:
# self.validate_token(request, consumer, token)
# self.validate_token(request, consumer, token)
# except oauth2.Error, e:
# except oauth2
_provider
.Error, e:
# print "got e"
# print "got e"
# raise exceptions.AuthenticationFailed(e.message)
# raise exceptions.AuthenticationFailed(e.message)
...
...
rest_framework/compat.py
View file @
9d5c3060
...
@@ -430,10 +430,6 @@ except ImportError:
...
@@ -430,10 +430,6 @@ except ImportError:
# OAuth 2 support is optional
# OAuth 2 support is optional
try
:
try
:
import
provider
as
oauth2_provider
import
provider
.oauth2
as
oauth2_provider
except
ImportError
:
except
ImportError
:
oauth2_provider
=
None
oauth2_provider
=
None
try
:
import
provider.oauth2
as
oauth2
except
ImportError
:
oauth2
=
None
rest_framework/tests/authentication.py
View file @
9d5c3060
...
@@ -16,7 +16,6 @@ from rest_framework.authentication import (
...
@@ -16,7 +16,6 @@ from rest_framework.authentication import (
OAuth2Authentication
OAuth2Authentication
)
)
from
rest_framework.compat
import
patterns
,
url
,
include
from
rest_framework.compat
import
patterns
,
url
,
include
from
rest_framework.compat
import
oauth2
from
rest_framework.compat
import
oauth2_provider
from
rest_framework.compat
import
oauth2_provider
from
rest_framework.tests.utils
import
RequestFactory
from
rest_framework.tests.utils
import
RequestFactory
from
rest_framework.views
import
APIView
from
rest_framework.views
import
APIView
...
@@ -248,7 +247,7 @@ class OAuth2Tests(TestCase):
...
@@ -248,7 +247,7 @@ class OAuth2Tests(TestCase):
self
.
ACCESS_TOKEN
=
"access_token"
self
.
ACCESS_TOKEN
=
"access_token"
self
.
REFRESH_TOKEN
=
"refresh_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_id
=
self
.
CLIENT_ID
,
client_secret
=
self
.
CLIENT_SECRET
,
client_secret
=
self
.
CLIENT_SECRET
,
redirect_uri
=
''
,
redirect_uri
=
''
,
...
@@ -257,12 +256,12 @@ class OAuth2Tests(TestCase):
...
@@ -257,12 +256,12 @@ class OAuth2Tests(TestCase):
user
=
None
,
user
=
None
,
)
)
self
.
access_token
=
oauth2
.
models
.
AccessToken
.
objects
.
create
(
self
.
access_token
=
oauth2
_provider
.
models
.
AccessToken
.
objects
.
create
(
token
=
self
.
ACCESS_TOKEN
,
token
=
self
.
ACCESS_TOKEN
,
client
=
self
.
oauth2_client
,
client
=
self
.
oauth2_client
,
user
=
self
.
user
,
user
=
self
.
user
,
)
)
self
.
refresh_token
=
oauth2
.
models
.
RefreshToken
.
objects
.
create
(
self
.
refresh_token
=
oauth2
_provider
.
models
.
RefreshToken
.
objects
.
create
(
user
=
self
.
user
,
user
=
self
.
user
,
access_token
=
self
.
access_token
,
access_token
=
self
.
access_token
,
client
=
self
.
oauth2_client
client
=
self
.
oauth2_client
...
@@ -274,7 +273,7 @@ class OAuth2Tests(TestCase):
...
@@ -274,7 +273,7 @@ class OAuth2Tests(TestCase):
def
_client_credentials_params
(
self
):
def
_client_credentials_params
(
self
):
return
{
'client_id'
:
self
.
CLIENT_ID
,
'client_secret'
:
self
.
CLIENT_SECRET
}
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
):
def
test_get_form_with_wrong_client_data_failing_auth
(
self
):
"""Ensure GETing form over OAuth with incorrect client credentials fails"""
"""Ensure GETing form over OAuth with incorrect client credentials fails"""
auth
=
self
.
_create_authorization_header
()
auth
=
self
.
_create_authorization_header
()
...
@@ -283,7 +282,7 @@ class OAuth2Tests(TestCase):
...
@@ -283,7 +282,7 @@ class OAuth2Tests(TestCase):
response
=
self
.
csrf_client
.
get
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
response
=
self
.
csrf_client
.
get
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
self
.
assertEqual
(
response
.
status_code
,
401
)
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
):
def
test_get_form_passing_auth
(
self
):
"""Ensure GETing form over OAuth with correct client credentials succeed"""
"""Ensure GETing form over OAuth with correct client credentials succeed"""
auth
=
self
.
_create_authorization_header
()
auth
=
self
.
_create_authorization_header
()
...
@@ -291,7 +290,7 @@ class OAuth2Tests(TestCase):
...
@@ -291,7 +290,7 @@ class OAuth2Tests(TestCase):
response
=
self
.
csrf_client
.
get
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
response
=
self
.
csrf_client
.
get
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
self
.
assertEqual
(
response
.
status_code
,
200
)
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
):
def
test_post_form_passing_auth
(
self
):
"""Ensure POSTing form over OAuth with correct credentials passes and does not require CSRF"""
"""Ensure POSTing form over OAuth with correct credentials passes and does not require CSRF"""
auth
=
self
.
_create_authorization_header
()
auth
=
self
.
_create_authorization_header
()
...
@@ -299,7 +298,7 @@ class OAuth2Tests(TestCase):
...
@@ -299,7 +298,7 @@ class OAuth2Tests(TestCase):
response
=
self
.
csrf_client
.
post
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
response
=
self
.
csrf_client
.
post
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
self
.
assertEqual
(
response
.
status_code
,
200
)
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
):
def
test_post_form_token_removed_failing_auth
(
self
):
"""Ensure POSTing when there is no OAuth access token in db fails"""
"""Ensure POSTing when there is no OAuth access token in db fails"""
self
.
access_token
.
delete
()
self
.
access_token
.
delete
()
...
@@ -308,7 +307,7 @@ class OAuth2Tests(TestCase):
...
@@ -308,7 +307,7 @@ class OAuth2Tests(TestCase):
response
=
self
.
csrf_client
.
post
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
response
=
self
.
csrf_client
.
post
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
self
.
assertIn
(
response
.
status_code
,
(
status
.
HTTP_401_UNAUTHORIZED
,
status
.
HTTP_403_FORBIDDEN
))
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
):
def
test_post_form_with_refresh_token_failing_auth
(
self
):
"""Ensure POSTing with refresh token instead of access token fails"""
"""Ensure POSTing with refresh token instead of access token fails"""
auth
=
self
.
_create_authorization_header
(
token
=
self
.
refresh_token
.
token
)
auth
=
self
.
_create_authorization_header
(
token
=
self
.
refresh_token
.
token
)
...
@@ -316,7 +315,7 @@ class OAuth2Tests(TestCase):
...
@@ -316,7 +315,7 @@ class OAuth2Tests(TestCase):
response
=
self
.
csrf_client
.
post
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
response
=
self
.
csrf_client
.
post
(
'/oauth2-test/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
self
.
assertIn
(
response
.
status_code
,
(
status
.
HTTP_401_UNAUTHORIZED
,
status
.
HTTP_403_FORBIDDEN
))
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
):
def
test_post_form_with_expired_access_token_failing_auth
(
self
):
"""Ensure POSTing with expired access token fails with an 'Invalid token' error"""
"""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
self
.
access_token
.
expires
=
datetime
.
datetime
.
now
()
-
datetime
.
timedelta
(
seconds
=
10
)
# 10 seconds late
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment