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
e0682e92
Commit
e0682e92
authored
Mar 05, 2014
by
Eric Buehl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
don't implicitly import provider.oauth2
parent
45ae5081
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
13 additions
and
23 deletions
+13
-23
rest_framework/authentication.py
+2
-2
rest_framework/compat.py
+2
-11
rest_framework/permissions.py
+3
-4
rest_framework/tests/test_authentication.py
+6
-6
No files found.
rest_framework/authentication.py
View file @
e0682e92
...
@@ -326,11 +326,11 @@ class OAuth2Authentication(BaseAuthentication):
...
@@ -326,11 +326,11 @@ class OAuth2Authentication(BaseAuthentication):
"""
"""
try
:
try
:
token
=
oauth2_provider
.
models
.
AccessToken
.
objects
.
select_related
(
'user'
)
token
=
oauth2_provider
.
oauth2
.
models
.
AccessToken
.
objects
.
select_related
(
'user'
)
# provider_now switches to timezone aware datetime when
# provider_now switches to timezone aware datetime when
# the oauth2_provider version supports to it.
# the oauth2_provider version supports to it.
token
=
token
.
get
(
token
=
access_token
,
expires__gt
=
provider_now
())
token
=
token
.
get
(
token
=
access_token
,
expires__gt
=
provider_now
())
except
oauth2_provider
.
models
.
AccessToken
.
DoesNotExist
:
except
oauth2_provider
.
oauth2
.
models
.
AccessToken
.
DoesNotExist
:
raise
exceptions
.
AuthenticationFailed
(
'Invalid token'
)
raise
exceptions
.
AuthenticationFailed
(
'Invalid token'
)
user
=
token
.
user
user
=
token
.
user
...
...
rest_framework/compat.py
View file @
e0682e92
...
@@ -550,13 +550,8 @@ except (ImportError, ImproperlyConfigured):
...
@@ -550,13 +550,8 @@ except (ImportError, ImproperlyConfigured):
# OAuth 2 support is optional
# OAuth 2 support is optional
try
:
try
:
import
provider.oauth2
as
oauth2_provider
import
provider
as
oauth2_provider
from
provider.oauth2
import
models
as
oauth2_provider_models
if
oauth2_provider
.
__version__
in
(
'0.2.3'
,
'0.2.4'
):
from
provider.oauth2
import
forms
as
oauth2_provider_forms
from
provider
import
scope
as
oauth2_provider_scope
from
provider
import
constants
as
oauth2_constants
from
provider
import
__version__
as
provider_version
if
provider_version
in
(
'0.2.3'
,
'0.2.4'
):
# 0.2.3 and 0.2.4 are supported version that do not support
# 0.2.3 and 0.2.4 are supported version that do not support
# timezone aware datetimes
# timezone aware datetimes
import
datetime
import
datetime
...
@@ -566,10 +561,6 @@ try:
...
@@ -566,10 +561,6 @@ try:
from
django.utils.timezone
import
now
as
provider_now
from
django.utils.timezone
import
now
as
provider_now
except
ImportError
:
except
ImportError
:
oauth2_provider
=
None
oauth2_provider
=
None
oauth2_provider_models
=
None
oauth2_provider_forms
=
None
oauth2_provider_scope
=
None
oauth2_constants
=
None
provider_now
=
None
provider_now
=
None
# Handle lazy strings
# Handle lazy strings
...
...
rest_framework/permissions.py
View file @
e0682e92
...
@@ -8,8 +8,7 @@ import warnings
...
@@ -8,8 +8,7 @@ import warnings
SAFE_METHODS
=
[
'GET'
,
'HEAD'
,
'OPTIONS'
]
SAFE_METHODS
=
[
'GET'
,
'HEAD'
,
'OPTIONS'
]
from
django.http
import
Http404
from
django.http
import
Http404
from
rest_framework.compat
import
(
get_model_name
,
oauth2_provider_scope
,
from
rest_framework.compat
import
(
get_model_name
,
oauth2_provider
)
oauth2_constants
)
class
BasePermission
(
object
):
class
BasePermission
(
object
):
...
@@ -219,8 +218,8 @@ class TokenHasReadWriteScope(BasePermission):
...
@@ -219,8 +218,8 @@ class TokenHasReadWriteScope(BasePermission):
if
hasattr
(
token
,
'resource'
):
# OAuth 1
if
hasattr
(
token
,
'resource'
):
# OAuth 1
return
read_only
or
not
request
.
auth
.
resource
.
is_readonly
return
read_only
or
not
request
.
auth
.
resource
.
is_readonly
elif
hasattr
(
token
,
'scope'
):
# OAuth 2
elif
hasattr
(
token
,
'scope'
):
# OAuth 2
required
=
oauth2_
constants
.
READ
if
read_only
else
oauth2_
constants
.
WRITE
required
=
oauth2_
provider
.
constants
.
READ
if
read_only
else
oauth2_provider
.
constants
.
WRITE
return
oauth2_provider
_
scope
.
check
(
required
,
request
.
auth
.
scope
)
return
oauth2_provider
.
scope
.
check
(
required
,
request
.
auth
.
scope
)
assert
False
,
(
'TokenHasReadWriteScope requires either the'
assert
False
,
(
'TokenHasReadWriteScope requires either the'
'`OAuthAuthentication` or `OAuth2Authentication` authentication '
'`OAuthAuthentication` or `OAuth2Authentication` authentication '
...
...
rest_framework/tests/test_authentication.py
View file @
e0682e92
...
@@ -19,7 +19,7 @@ from rest_framework.authentication import (
...
@@ -19,7 +19,7 @@ from rest_framework.authentication import (
)
)
from
rest_framework.authtoken.models
import
Token
from
rest_framework.authtoken.models
import
Token
from
rest_framework.compat
import
patterns
,
url
,
include
from
rest_framework.compat
import
patterns
,
url
,
include
from
rest_framework.compat
import
oauth2_provider
,
oauth2_provider_models
,
oauth2_provider_scope
from
rest_framework.compat
import
oauth2_provider
from
rest_framework.compat
import
oauth
,
oauth_provider
from
rest_framework.compat
import
oauth
,
oauth_provider
from
rest_framework.test
import
APIRequestFactory
,
APIClient
from
rest_framework.test
import
APIRequestFactory
,
APIClient
from
rest_framework.views
import
APIView
from
rest_framework.views
import
APIView
...
@@ -488,7 +488,7 @@ class OAuth2Tests(TestCase):
...
@@ -488,7 +488,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_provider
_
models
.
Client
.
objects
.
create
(
self
.
oauth2_client
=
oauth2_provider
.
oauth2
.
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
=
''
,
...
@@ -497,12 +497,12 @@ class OAuth2Tests(TestCase):
...
@@ -497,12 +497,12 @@ class OAuth2Tests(TestCase):
user
=
None
,
user
=
None
,
)
)
self
.
access_token
=
oauth2_provider
_
models
.
AccessToken
.
objects
.
create
(
self
.
access_token
=
oauth2_provider
.
oauth2
.
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_provider
_
models
.
RefreshToken
.
objects
.
create
(
self
.
refresh_token
=
oauth2_provider
.
oauth2
.
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
...
@@ -581,7 +581,7 @@ class OAuth2Tests(TestCase):
...
@@ -581,7 +581,7 @@ class OAuth2Tests(TestCase):
def
test_post_form_with_invalid_scope_failing_auth
(
self
):
def
test_post_form_with_invalid_scope_failing_auth
(
self
):
"""Ensure POSTing with a readonly scope instead of a write scope fails"""
"""Ensure POSTing with a readonly scope instead of a write scope fails"""
read_only_access_token
=
self
.
access_token
read_only_access_token
=
self
.
access_token
read_only_access_token
.
scope
=
oauth2_provider
_
scope
.
SCOPE_NAME_DICT
[
'read'
]
read_only_access_token
.
scope
=
oauth2_provider
.
scope
.
SCOPE_NAME_DICT
[
'read'
]
read_only_access_token
.
save
()
read_only_access_token
.
save
()
auth
=
self
.
_create_authorization_header
(
token
=
read_only_access_token
.
token
)
auth
=
self
.
_create_authorization_header
(
token
=
read_only_access_token
.
token
)
response
=
self
.
csrf_client
.
get
(
'/oauth2-with-scope-test/'
,
HTTP_AUTHORIZATION
=
auth
)
response
=
self
.
csrf_client
.
get
(
'/oauth2-with-scope-test/'
,
HTTP_AUTHORIZATION
=
auth
)
...
@@ -593,7 +593,7 @@ class OAuth2Tests(TestCase):
...
@@ -593,7 +593,7 @@ class OAuth2Tests(TestCase):
def
test_post_form_with_valid_scope_passing_auth
(
self
):
def
test_post_form_with_valid_scope_passing_auth
(
self
):
"""Ensure POSTing with a write scope succeed"""
"""Ensure POSTing with a write scope succeed"""
read_write_access_token
=
self
.
access_token
read_write_access_token
=
self
.
access_token
read_write_access_token
.
scope
=
oauth2_provider
_
scope
.
SCOPE_NAME_DICT
[
'write'
]
read_write_access_token
.
scope
=
oauth2_provider
.
scope
.
SCOPE_NAME_DICT
[
'write'
]
read_write_access_token
.
save
()
read_write_access_token
.
save
()
auth
=
self
.
_create_authorization_header
(
token
=
read_write_access_token
.
token
)
auth
=
self
.
_create_authorization_header
(
token
=
read_write_access_token
.
token
)
response
=
self
.
csrf_client
.
post
(
'/oauth2-with-scope-test/'
,
HTTP_AUTHORIZATION
=
auth
)
response
=
self
.
csrf_client
.
post
(
'/oauth2-with-scope-test/'
,
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