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

Do not attempt to refresh when RT is unavailable #87

Merged
merged 2 commits into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions adal/cache_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,13 @@ def _refresh_entry_if_necessary(self, entry, is_resource_specific):
now_plus_buffer = now + timedelta(minutes=Misc.CLOCK_BUFFER)

if is_resource_specific and now_plus_buffer > expiry_date:
self._log.info('Cached token is expired. Refreshing: %s', expiry_date)
return self._refresh_expired_entry(entry)
if TokenResponseFields.REFRESH_TOKEN in entry:
Copy link
Contributor

@yugangw-msft yugangw-msft May 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No matched else here means we return "None"? If yes, then it means the old expired entry will stay in the cache. My concern is, the subsequent token request will further push in a new valid token, but the old one might still get matched in future, rather the new token; so more and more new tokens will be added and the older ones still stay? This will end up corrupting the token cache. If I get it right, I would suggest we delete this token before return None

Copy link
Collaborator Author

@rayluo rayluo May 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will remove have removed the unused cache entry in my next commit. A side note: the current cache logic is somewhat complicated. We may refactor them someday in future, probably starting from scratch in MSAL implementation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current cache logic is somewhat complicated

Agreed, please do improve it in MSAL

self._log.info('Cached token is expired. Refreshing: %s', expiry_date)
return self._refresh_expired_entry(entry)
elif not is_resource_specific and entry.get(TokenResponseFields.IS_MRRT):
self._log.info('Acquiring new access token from MRRT token.')
return self._acquire_new_token_from_mrrt(entry)
if TokenResponseFields.REFRESH_TOKEN in entry:
self._log.info('Acquiring new access token from MRRT token.')
return self._acquire_new_token_from_mrrt(entry)
else:
return entry

Expand Down
58 changes: 58 additions & 0 deletions tests/test_cache_driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#------------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation.
# All rights reserved.
#
# This code is licensed under the MIT License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files(the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions :
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#------------------------------------------------------------------------------

import unittest
try:
from unittest import mock
except ImportError:
import mock

from adal.log import create_log_context
from adal.cache_driver import CacheDriver


class TestCacheDriver(unittest.TestCase):
def test_rt_less_item_wont_cause_exception(self): # Github issue #82
rt_less_entry_came_from_previous_client_credentials_grant = {
"expiresIn": 3600,
"_authority": "https://login.microsoftonline.com/foo",
"resource": "spn:00000002-0000-0000-c000-000000000000",
"tokenType": "Bearer",
"expiresOn": "1999-05-22 16:31:46.202000",
"isMRRT": True,
"_clientId": "client_id",
"accessToken": "this is an AT",
}
refresh_function = mock.MagicMock(return_value={})
cache_driver = CacheDriver(
{"log_context": create_log_context()}, "authority", "resource",
"client_id", mock.MagicMock(), refresh_function)
entry = cache_driver._refresh_entry_if_necessary(
rt_less_entry_came_from_previous_client_credentials_grant, False)
refresh_function.assert_not_called() # Otherwise it will cause an exception
self.assertIsNone(entry)