Skip to content

Commit

Permalink
Instead of using the refresh_token grant type for automatically refre…
Browse files Browse the repository at this point in the history
…shing tokens, just do a normal client_credentials request.
  • Loading branch information
Dave St.Germain authored and davestgermain committed Nov 8, 2018
1 parent ef7100c commit 7ed2a83
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion edx_rest_api_client/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.9.1'
__version__ = '1.9.2'
7 changes: 2 additions & 5 deletions edx_rest_api_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def get_oauth_access_token(url, client_id, client_secret, token_type='jwt', gran
)

data = response.json()

try:
access_token = data['access_token']
expires_in = data['expires_in']
Expand Down Expand Up @@ -112,14 +111,12 @@ def __init__(self, base_url, client_id, client_secret, **kwargs):
def _check_auth(self):
if datetime.datetime.utcnow() > self._expiration:
url = self._base_url + self.oauth_uri
has_token = self.auth.token is not None
grant_type = 'refresh_token' if has_token else 'client_credentials'
grant_type = 'client_credentials'
self.auth.token, self._expiration = get_oauth_access_token(
url,
self._client_id,
self._client_secret,
grant_type=grant_type,
refresh_token=self.auth.token)
grant_type=grant_type)

def request(self, method, url, **kwargs): # pylint: disable=arguments-differ
"""
Expand Down
10 changes: 5 additions & 5 deletions edx_rest_api_client/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ def test_automatic_token_refresh(self):
"""
Test that the JWT token is automatically refreshed
"""
tokens = ['cred2', 'cred1']

def auth_callback(request):
resp = {'expires_in': 60}
if 'grant_type=client_credentials' in request.body:
resp['access_token'] = 'cred'
elif 'grant_type=refresh_token' in request.body and 'refresh_token=cred' in request.body:
resp['access_token'] = 'refresh'
resp['access_token'] = tokens.pop()
return (200, {}, json.dumps(resp))

responses.add_callback(
Expand All @@ -180,8 +180,8 @@ def auth_callback(request):
session = OAuthAPIClient(self.base_url, self.client_id, self.client_secret)
self._mock_auth_api(self.base_url + '/endpoint', 200, {'status': 'ok'})
response = session.post(self.base_url + '/endpoint', data={'test': 'ok'})
self.assertEqual(session.auth.token, 'cred')
self.assertEqual(session.auth.token, 'cred1')
self.assertEqual(response.json()['status'], 'ok')
with freeze_time(datetime.datetime.utcnow() + datetime.timedelta(seconds=3600)):
response = session.post(self.base_url + '/endpoint', data={'test': 'ok'})
self.assertEqual(session.auth.token, 'refresh')
self.assertEqual(session.auth.token, 'cred2')

0 comments on commit 7ed2a83

Please sign in to comment.