Skip to content

Commit d0393b3

Browse files
Psykopearsynasius
authored andcommitted
Added possibility to specify a default list of scopes
1 parent 6e4b0cb commit d0393b3

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

oauth2_provider/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class AllowForm(forms.Form):
55
allow = forms.BooleanField(required=False)
66
redirect_uri = forms.CharField(widget=forms.HiddenInput())
7-
scope = forms.CharField(required=False, widget=forms.HiddenInput())
7+
scope = forms.CharField(widget=forms.HiddenInput())
88
client_id = forms.CharField(widget=forms.HiddenInput())
99
state = forms.CharField(required=False, widget=forms.HiddenInput())
1010
response_type = forms.CharField(widget=forms.HiddenInput())

oauth2_provider/oauth2_validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def validate_scopes(self, client_id, scopes, client, request, *args, **kwargs):
278278
return set(scopes).issubset(set(oauth2_settings._SCOPES))
279279

280280
def get_default_scopes(self, client_id, request, *args, **kwargs):
281-
return oauth2_settings._SCOPES
281+
return oauth2_settings._DEFAULT_SCOPES or oauth2_settings._SCOPES
282282

283283
def validate_redirect_uri(self, client_id, redirect_uri, request, *args, **kwargs):
284284
return request.client.redirect_uri_allowed(redirect_uri)

oauth2_provider/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
except ImportError:
2727
from django.utils import importlib
2828

29-
3029
USER_SETTINGS = getattr(settings, 'OAUTH2_PROVIDER', None)
3130

3231
DEFAULTS = {
@@ -37,6 +36,7 @@
3736
'OAUTH2_VALIDATOR_CLASS': 'oauth2_provider.oauth2_validators.OAuth2Validator',
3837
'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.OAuthLibCore',
3938
'SCOPES': {"read": "Reading scope", "write": "Writing scope"},
39+
'DEFAULT_SCOPES': {},
4040
'READ_SCOPE': 'read',
4141
'WRITE_SCOPE': 'write',
4242
'AUTHORIZATION_CODE_EXPIRE_SECONDS': 60,
@@ -48,6 +48,7 @@
4848

4949
# Special settings that will be evaluated at runtime
5050
'_SCOPES': [],
51+
'_DEFAULT_SCOPES': [],
5152
}
5253

5354
# List of settings that cannot be empty
@@ -129,6 +130,8 @@ def __getattr__(self, attr):
129130
# Overriding special settings
130131
if attr == '_SCOPES':
131132
val = list(six.iterkeys(self.SCOPES))
133+
if attr == '_DEFAULT_SCOPES':
134+
val = list(six.iterkeys(self.DEFAULT_SCOPES))
132135

133136
self.validate_setting(attr, val)
134137

oauth2_provider/tests/test_authorization_code.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from django.test import TestCase, RequestFactory
99
from django.core.urlresolvers import reverse
10+
from django.test.utils import override_settings
1011
from django.utils import timezone
1112

1213
from ..compat import urlparse, parse_qs, urlencode, get_user_model
@@ -1003,3 +1004,34 @@ def test_resource_access_deny(self):
10031004
view = ResourceView.as_view()
10041005
response = view(request)
10051006
self.assertEqual(response.status_code, 403)
1007+
1008+
1009+
class TestDefaultScopes(BaseTest):
1010+
1011+
def test_pre_auth_deafult_scopes(self):
1012+
"""
1013+
Test response for a valid client_id with response_type: code using default scopes
1014+
"""
1015+
self.client.login(username="test_user", password="123456")
1016+
oauth2_settings._DEFAULT_SCOPES = ['read']
1017+
1018+
query_string = urlencode({
1019+
'client_id': self.application.client_id,
1020+
'response_type': 'code',
1021+
'state': 'random_state_string',
1022+
'redirect_uri': 'http://example.it',
1023+
})
1024+
url = "{url}?{qs}".format(url=reverse('oauth2_provider:authorize'), qs=query_string)
1025+
1026+
response = self.client.get(url)
1027+
self.assertEqual(response.status_code, 200)
1028+
1029+
# check form is in context and form params are valid
1030+
self.assertIn("form", response.context)
1031+
1032+
form = response.context["form"]
1033+
self.assertEqual(form['redirect_uri'].value(), "http://example.it")
1034+
self.assertEqual(form['state'].value(), "random_state_string")
1035+
self.assertEqual(form['scope'].value(), 'read')
1036+
self.assertEqual(form['client_id'].value(), self.application.client_id)
1037+
oauth2_settings._DEFAULT_SCOPES = []

0 commit comments

Comments
 (0)