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
a87c55a9
Commit
a87c55a9
authored
Dec 13, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Compat fixes for django-oauth-plus versions 2.0-2.2.1
parent
fac6d1a3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
13 deletions
+29
-13
rest_framework/authentication.py
+4
-3
rest_framework/compat.py
+15
-0
rest_framework/tests/test_authentication.py
+10
-10
No files found.
rest_framework/authentication.py
View file @
a87c55a9
...
@@ -9,7 +9,7 @@ from django.core.exceptions import ImproperlyConfigured
...
@@ -9,7 +9,7 @@ from django.core.exceptions import ImproperlyConfigured
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
oauth
,
oauth_provider
,
oauth_provider_store
from
rest_framework.compat
import
oauth
,
oauth_provider
,
oauth_provider_store
from
rest_framework.compat
import
oauth2_provider
,
provider_now
from
rest_framework.compat
import
oauth2_provider
,
provider_now
,
check_nonce
from
rest_framework.authtoken.models
import
Token
from
rest_framework.authtoken.models
import
Token
...
@@ -281,8 +281,9 @@ class OAuthAuthentication(BaseAuthentication):
...
@@ -281,8 +281,9 @@ class OAuthAuthentication(BaseAuthentication):
"""
"""
Checks nonce of request, and return True if valid.
Checks nonce of request, and return True if valid.
"""
"""
return
oauth_provider_store
.
check_nonce
(
request
,
oauth_request
,
oauth_nonce
=
oauth_request
[
'oauth_nonce'
]
oauth_request
[
'oauth_nonce'
],
oauth_request
[
'oauth_timestamp'
])
oauth_timestamp
=
oauth_request
[
'oauth_timestamp'
]
return
check_nonce
(
request
,
oauth_request
,
oauth_nonce
,
oauth_timestamp
)
class
OAuth2Authentication
(
BaseAuthentication
):
class
OAuth2Authentication
(
BaseAuthentication
):
...
...
rest_framework/compat.py
View file @
a87c55a9
...
@@ -7,6 +7,7 @@ versions of django/python, and compatibility wrappers around optional packages.
...
@@ -7,6 +7,7 @@ versions of django/python, and compatibility wrappers around optional packages.
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
django
import
django
import
inspect
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.exceptions
import
ImproperlyConfigured
from
django.conf
import
settings
from
django.conf
import
settings
...
@@ -536,9 +537,23 @@ except ImportError:
...
@@ -536,9 +537,23 @@ except ImportError:
try
:
try
:
import
oauth_provider
import
oauth_provider
from
oauth_provider.store
import
store
as
oauth_provider_store
from
oauth_provider.store
import
store
as
oauth_provider_store
# check_nonce's calling signature in django-oauth-plus changes sometime
# between versions 2.0 and 2.2.1
def
check_nonce
(
request
,
oauth_request
,
oauth_nonce
,
oauth_timestamp
):
check_nonce_args
=
inspect
.
getargspec
(
oauth_provider_store
.
check_nonce
)
.
args
if
'timestamp'
in
check_nonce_args
:
return
oauth_provider_store
.
check_nonce
(
request
,
oauth_request
,
oauth_nonce
,
oauth_timestamp
)
return
oauth_provider_store
.
check_nonce
(
request
,
oauth_request
,
oauth_nonce
)
except
(
ImportError
,
ImproperlyConfigured
):
except
(
ImportError
,
ImproperlyConfigured
):
oauth_provider
=
None
oauth_provider
=
None
oauth_provider_store
=
None
oauth_provider_store
=
None
check_nonce
=
None
# OAuth 2 support is optional
# OAuth 2 support is optional
try
:
try
:
...
...
rest_framework/tests/test_authentication.py
View file @
a87c55a9
...
@@ -249,7 +249,7 @@ class OAuthTests(TestCase):
...
@@ -249,7 +249,7 @@ class OAuthTests(TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
# these imports are here because oauth is optional and hiding them in try..except block or compat
# these imports are here because oauth is optional and hiding them in try..except block or compat
# could obscure problems if something breaks
# could obscure problems if something breaks
from
oauth_provider.models
import
Consumer
,
Resourc
e
from
oauth_provider.models
import
Consumer
,
Scop
e
from
oauth_provider.models
import
Token
as
OAuthToken
from
oauth_provider.models
import
Token
as
OAuthToken
from
oauth_provider
import
consts
from
oauth_provider
import
consts
...
@@ -269,8 +269,8 @@ class OAuthTests(TestCase):
...
@@ -269,8 +269,8 @@ class OAuthTests(TestCase):
self
.
consumer
=
Consumer
.
objects
.
create
(
key
=
self
.
CONSUMER_KEY
,
secret
=
self
.
CONSUMER_SECRET
,
self
.
consumer
=
Consumer
.
objects
.
create
(
key
=
self
.
CONSUMER_KEY
,
secret
=
self
.
CONSUMER_SECRET
,
name
=
'example'
,
user
=
self
.
user
,
status
=
self
.
consts
.
ACCEPTED
)
name
=
'example'
,
user
=
self
.
user
,
status
=
self
.
consts
.
ACCEPTED
)
self
.
resource
=
Resourc
e
.
objects
.
create
(
name
=
"resource name"
,
url
=
"api/"
)
self
.
scope
=
Scop
e
.
objects
.
create
(
name
=
"resource name"
,
url
=
"api/"
)
self
.
token
=
OAuthToken
.
objects
.
create
(
user
=
self
.
user
,
consumer
=
self
.
consumer
,
resource
=
self
.
resourc
e
,
self
.
token
=
OAuthToken
.
objects
.
create
(
user
=
self
.
user
,
consumer
=
self
.
consumer
,
scope
=
self
.
scop
e
,
token_type
=
OAuthToken
.
ACCESS
,
key
=
self
.
TOKEN_KEY
,
secret
=
self
.
TOKEN_SECRET
,
is_approved
=
True
token_type
=
OAuthToken
.
ACCESS
,
key
=
self
.
TOKEN_KEY
,
secret
=
self
.
TOKEN_SECRET
,
is_approved
=
True
)
)
...
@@ -398,10 +398,10 @@ class OAuthTests(TestCase):
...
@@ -398,10 +398,10 @@ class OAuthTests(TestCase):
@unittest.skipUnless
(
oauth_provider
,
'django-oauth-plus not installed'
)
@unittest.skipUnless
(
oauth_provider
,
'django-oauth-plus not installed'
)
@unittest.skipUnless
(
oauth
,
'oauth2 not installed'
)
@unittest.skipUnless
(
oauth
,
'oauth2 not installed'
)
def
test_get_form_with_readonly_resource_passing_auth
(
self
):
def
test_get_form_with_readonly_resource_passing_auth
(
self
):
"""Ensure POSTing with a readonly
resourc
e instead of a write scope fails"""
"""Ensure POSTing with a readonly
scop
e instead of a write scope fails"""
read_only_access_token
=
self
.
token
read_only_access_token
=
self
.
token
read_only_access_token
.
resourc
e
.
is_readonly
=
True
read_only_access_token
.
scop
e
.
is_readonly
=
True
read_only_access_token
.
resourc
e
.
save
()
read_only_access_token
.
scop
e
.
save
()
params
=
self
.
_create_authorization_url_parameters
()
params
=
self
.
_create_authorization_url_parameters
()
response
=
self
.
csrf_client
.
get
(
'/oauth-with-scope/'
,
params
)
response
=
self
.
csrf_client
.
get
(
'/oauth-with-scope/'
,
params
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
response
.
status_code
,
200
)
...
@@ -411,8 +411,8 @@ class OAuthTests(TestCase):
...
@@ -411,8 +411,8 @@ class OAuthTests(TestCase):
def
test_post_form_with_readonly_resource_failing_auth
(
self
):
def
test_post_form_with_readonly_resource_failing_auth
(
self
):
"""Ensure POSTing with a readonly resource instead of a write scope fails"""
"""Ensure POSTing with a readonly resource instead of a write scope fails"""
read_only_access_token
=
self
.
token
read_only_access_token
=
self
.
token
read_only_access_token
.
resourc
e
.
is_readonly
=
True
read_only_access_token
.
scop
e
.
is_readonly
=
True
read_only_access_token
.
resourc
e
.
save
()
read_only_access_token
.
scop
e
.
save
()
params
=
self
.
_create_authorization_url_parameters
()
params
=
self
.
_create_authorization_url_parameters
()
response
=
self
.
csrf_client
.
post
(
'/oauth-with-scope/'
,
params
)
response
=
self
.
csrf_client
.
post
(
'/oauth-with-scope/'
,
params
)
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
))
...
@@ -422,8 +422,8 @@ class OAuthTests(TestCase):
...
@@ -422,8 +422,8 @@ class OAuthTests(TestCase):
def
test_post_form_with_write_resource_passing_auth
(
self
):
def
test_post_form_with_write_resource_passing_auth
(
self
):
"""Ensure POSTing with a write resource succeed"""
"""Ensure POSTing with a write resource succeed"""
read_write_access_token
=
self
.
token
read_write_access_token
=
self
.
token
read_write_access_token
.
resourc
e
.
is_readonly
=
False
read_write_access_token
.
scop
e
.
is_readonly
=
False
read_write_access_token
.
resourc
e
.
save
()
read_write_access_token
.
scop
e
.
save
()
params
=
self
.
_create_authorization_url_parameters
()
params
=
self
.
_create_authorization_url_parameters
()
auth
=
self
.
_create_authorization_header
()
auth
=
self
.
_create_authorization_header
()
response
=
self
.
csrf_client
.
post
(
'/oauth-with-scope/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
response
=
self
.
csrf_client
.
post
(
'/oauth-with-scope/'
,
params
,
HTTP_AUTHORIZATION
=
auth
)
...
...
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