forked from caffeinehit/django-oauth2-provider
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from skruger/improved_scopes
Improve Oauth middleware
- Loading branch information
Showing
17 changed files
with
234 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
dist: xenial | ||
sudo: false | ||
language: python | ||
python: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "2.1" | ||
__version__ = "2.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from django.utils.decorators import classonlymethod | ||
from django.http.response import JsonResponse | ||
|
||
|
||
class OAuthRegisteredScopes(object): | ||
scopes = set() | ||
|
||
|
||
class OAuthRequiredMixin(object): | ||
accepted_oauth_scopes = [] | ||
|
||
@classonlymethod | ||
def as_view(cls, *args, **kwargs): | ||
for scope in cls.accepted_oauth_scopes: | ||
OAuthRegisteredScopes.scopes.add(scope) | ||
|
||
return super(OAuthRequiredMixin, cls).as_view() | ||
|
||
def dispatch(self, request, *args, **kwargs): | ||
scopes = list() | ||
if hasattr(request, 'oauth2_token'): | ||
scopes = set(request.oauth2_token.scope.all().values_list('name', flat=True)) | ||
|
||
if request.user.is_authenticated and scopes.intersection(self.accepted_oauth_scopes): | ||
return super(OAuthRequiredMixin, self).dispatch(request, *args, **kwargs) | ||
|
||
return JsonResponse( | ||
{ | ||
'error': 'bad_access_token', | ||
'accepted_scopes': sorted(self.accepted_oauth_scopes), | ||
'token_scopes': sorted(scopes) | ||
}, | ||
status=401 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import json | ||
from six.moves.urllib_parse import urlparse | ||
|
||
from django.shortcuts import reverse | ||
from django.http import QueryDict | ||
|
||
from provider.oauth2.models import Scope | ||
from provider.oauth2.mixins import OAuthRegisteredScopes | ||
from provider.oauth2.tests.test_views import BaseOAuth2TestCase | ||
|
||
|
||
class MiddlewareTestCase(BaseOAuth2TestCase): | ||
fixtures = ['test_oauth2.json'] | ||
|
||
def setUp(self): | ||
if not Scope.objects.filter(name='read').exists(): | ||
Scope.objects.create(name='read') | ||
|
||
def _login_authorize_get_token(self): | ||
required_props = ['access_token', 'token_type'] | ||
|
||
self.login() | ||
self._login_and_authorize() | ||
|
||
response = self.client.get(self.redirect_url()) | ||
query = QueryDict(urlparse(response['Location']).query) | ||
code = query['code'] | ||
|
||
response = self.client.post(self.access_token_url(), { | ||
'grant_type': 'authorization_code', | ||
'client_id': self.get_client().client_id, | ||
'client_secret': self.get_client().client_secret, | ||
'code': code}) | ||
|
||
self.assertEqual(200, response.status_code, response.content) | ||
|
||
token = json.loads(response.content) | ||
|
||
for prop in required_props: | ||
self.assertIn(prop, token, "Access token response missing " | ||
"required property: %s" % prop) | ||
|
||
return token | ||
|
||
def test_mixin_scopes(self): | ||
self.assertIn('read', OAuthRegisteredScopes.scopes) | ||
|
||
def test_no_token(self): | ||
# user_url = self.live_server_url + reverse('tests:user', args=[self.get_user().pk]) | ||
# result = requests.get(user_url) | ||
|
||
user_url = reverse('tests:user', args=[self.get_user().pk]) | ||
result = self.client.get(user_url) | ||
|
||
self.assertEqual(result.status_code, 401) | ||
|
||
def test_token_access(self): | ||
self.login() | ||
token_info = self._login_authorize_get_token() | ||
token = token_info['access_token'] | ||
|
||
# Create a new client to ensure a clean session | ||
oauth_client = self.client_class() | ||
|
||
user_url = reverse('tests:user', args=[self.get_user().pk]) | ||
result = oauth_client.get(user_url, {'access_token': token}) | ||
|
||
self.assertEqual(result.status_code, 200) | ||
result_json = result.json() | ||
self.assertEqual(result_json.get('id'), self.get_user().pk) | ||
|
||
def test_unauthorized_scope(self): | ||
self.login() | ||
token_info = self._login_authorize_get_token() | ||
token = token_info['access_token'] | ||
|
||
badscope_url = reverse('tests:badscope') | ||
|
||
oauth_client = self.client_class() | ||
|
||
result = oauth_client.get(badscope_url, {'access_token': token}) | ||
|
||
self.assertEqual(result.status_code, 401) | ||
result_json = result.json() | ||
# self.assertEqual(result_json.get('id'), self.get_user().pk) | ||
|
||
def test_no_stored_session(self): | ||
self.login() | ||
token_info = self._login_authorize_get_token() | ||
token = token_info['access_token'] | ||
|
||
oauth_client = self.client_class() | ||
|
||
user_url = reverse('tests:user', args=[self.get_user().pk]) | ||
result = oauth_client.get(user_url, {'access_token': token}) | ||
|
||
self.assertNotIn('sessionid', result.cookies) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.conf.urls import url | ||
from django.http.response import JsonResponse | ||
from django.views.generic import View | ||
from django.contrib.auth.mixins import LoginRequiredMixin | ||
from django.contrib.auth.models import User | ||
from django.shortcuts import get_object_or_404 | ||
|
||
from provider.oauth2.mixins import OAuthRequiredMixin | ||
|
||
app_name = 'tests' | ||
|
||
|
||
class UserView(OAuthRequiredMixin, LoginRequiredMixin, View): | ||
accepted_oauth_scopes = ['read'] | ||
|
||
def get(self, request, *args, **kwargs): | ||
user = get_object_or_404(User, pk=self.kwargs['pk']) | ||
return JsonResponse( | ||
{ | ||
'username': user.username, | ||
'id': user.pk, | ||
} | ||
) | ||
|
||
|
||
class BadScopeView(OAuthRequiredMixin, LoginRequiredMixin, View): | ||
accepted_oauth_scopes = ['badscope'] | ||
|
||
def get(self, request, *args, **kwargs): | ||
user = self.request.user | ||
return JsonResponse( | ||
{ | ||
'username': user.username, | ||
'id': user.pk, | ||
} | ||
) | ||
|
||
|
||
urlpatterns = [ | ||
url('^badscope$', BadScopeView.as_view(), name='badscope'), | ||
url('^user/(?P<pk>\d+)$', UserView.as_view(), name='user'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.