Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
authz code flow: support public client and token cache
Browse files Browse the repository at this point in the history
  • Loading branch information
yugangw-msft committed Apr 23, 2018
1 parent f56d841 commit c7b8870
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 9 additions & 2 deletions adal/token_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,19 @@ def get_token_with_client_credentials(self, client_secret):
def get_token_with_authorization_code(self, authorization_code, client_secret):

self._log.info("Getting token with auth code.")

try:
token = self._find_token_from_cache()
if token:
return token
except AdalError:
self._log.exception('Attempt to look for token in cache resulted in Error')
oauth_parameters = self._create_oauth_parameters(OAUTH2_GRANT_TYPE.AUTHORIZATION_CODE)
oauth_parameters[OAUTH2_PARAMETERS.CODE] = authorization_code
oauth_parameters[OAUTH2_PARAMETERS.CLIENT_SECRET] = client_secret

return self._oauth_get_token(oauth_parameters)
token = self._oauth_get_token(oauth_parameters)
self._cache_driver.add(token)
return token

def _get_token_with_refresh_token(self, refresh_token, resource, client_secret):

Expand Down
15 changes: 14 additions & 1 deletion tests/test_authorization_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,30 @@ def setUp(self):
@httpretty.activate
def test_happy_path(self):
response = util.create_response()

self.setup_expected_auth_code_token_request_response(200, response['wireResponse'])

context = adal.AuthenticationContext(cp['authUrl'])

# action
token_response = context.acquire_token_with_authorization_code(self.authorization_code, self.redirect_uri, response['resource'], cp['clientId'], cp['clientSecret'])

# assert

# the caching layer adds a few extra fileds, let us pop them out for easier comparisoon
for k in ['_clientId', '_authority', 'resource']:
token_response.pop(k)
self.assertTrue(util.is_match_token_response(response['decodedResponse'], token_response), 'The response did not match what was expected')

# verify a request was made on the wire
req = httpretty.last_request()
util.match_standard_request_headers(req)

# verify the same entry was cached
cached_items = context.cache.read_items()
self.assertTrue(len(cached_items) == 1)
_, cached_entry = cached_items[0]
self.assertEqual(cached_entry, token_response)

def test_failed_http_request(self):
with self.assertRaises(Exception):
adal._acquire_token_with_authorization_code(
Expand Down

0 comments on commit c7b8870

Please sign in to comment.