Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit c6b191e

Browse files
Clinton Blackburnclintonb
authored andcommitted
Allowing any scopes to be used for the client credentials grant
ECOM-4196
1 parent d67abe7 commit c6b191e

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

provider/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.2'
1+
__version__ = '1.0.3'

provider/oauth2/forms.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.utils.encoding import smart_unicode
44
from django.utils.translation import ugettext as _
55

6-
from provider import constants, scope
6+
from provider import scope
77
from provider.constants import RESPONSE_TYPE_CHOICES, SCOPES
88
from provider.forms import OAuthForm, OAuthValidationError
99
from provider.oauth2.models import Client, Grant, RefreshToken
@@ -340,10 +340,18 @@ def clean(self):
340340

341341
class ClientCredentialsGrantForm(ScopeMixin, OAuthForm):
342342
""" Validate a client credentials grant request. """
343+
scope = forms.CharField(required=False)
343344

344-
def clean(self):
345-
cleaned_data = super(ClientCredentialsGrantForm, self).clean()
346-
# We do not fully support scopes for this grant type; however, a scope is required
347-
# in order to create an access token. Default to read-only access.
348-
cleaned_data['scope'] = constants.READ
349-
return cleaned_data
345+
def clean_scope(self):
346+
if not self.cleaned_data.get('scope'):
347+
# NOTE (CCB): This is a horrible hack, like much of our OAuth work. The scopes are declared in
348+
# edx-oauth2-provider. (See edx_oauth2_provider/constants.py.) However, we need to provide a default scope
349+
# that (a) gives the token basic read access and (b) allows access to the user info endpoint. This value
350+
# represents the following scopes: openid (1), profile (2), email (4), permissions (32). At present, this is
351+
# all scopes except course_staff and course_instructor. These scopes are normally associated with actual
352+
# users, whereas the client credentials grant will primarily be used by service users.
353+
#
354+
# In the future, we should limit the allowable scopes either at a global or per-client level.
355+
return 39
356+
357+
return super(ClientCredentialsGrantForm, self).clean_scope()

provider/oauth2/tests.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def setUp(self):
597597
super(ClientCredentialsAccessTokenTests, self).setUp()
598598
AccessToken.objects.all().delete()
599599

600-
def request_access_token(self, client_id=None, client_secret=None):
600+
def request_access_token(self, client_id=None, client_secret=None, scope=None):
601601
""" Issues an access token request using the client credentials grant.
602602
603603
Arguments:
@@ -614,6 +614,11 @@ def request_access_token(self, client_id=None, client_secret=None):
614614
'client_secret': client_secret or client.client_secret,
615615
}
616616

617+
if scope:
618+
data.update({
619+
'scope': scope,
620+
})
621+
617622
return self.client.post(self.access_token_url(), data)
618623

619624
def assert_valid_access_token_response(self, access_token, response):
@@ -634,9 +639,10 @@ def assert_valid_access_token_response(self, access_token, response):
634639
def get_latest_access_token(self):
635640
return AccessToken.objects.filter(client=self.get_client()).order_by('-id')[0]
636641

637-
def test_authorize_success(self):
642+
@ddt.data(None, 'read')
643+
def test_authorize_success(self, scope):
638644
""" Verify the endpoint successfully issues an access token using the client credentials grant. """
639-
response = self.request_access_token()
645+
response = self.request_access_token(scope=scope)
640646
self.assertEqual(200, response.status_code, response.content)
641647

642648
access_token = self.get_latest_access_token()

0 commit comments

Comments
 (0)