Skip to content

Commit 8191cca

Browse files
committed
- Fix for protect_token_cache.
- Refactor. - Added _resource_id to AuthProviderAccessToken so that it can be used to create shared access keys (required for PR tests on github).
1 parent 9397740 commit 8191cca

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

src/sumo/wrapper/_auth_provider.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def get_authorization(self):
6868

6969
def store_shared_access_key_for_case(self, case_uuid, token):
7070
with open(
71-
get_token_path(self._resource_id + "+" + case_uuid, ".sharedkey"),
71+
get_token_path(self._resource_id, ".sharedkey", case_uuid),
7272
"w",
7373
) as f:
7474
f.write(token)
@@ -93,6 +93,7 @@ def __init__(self, access_token):
9393
self._access_token = access_token
9494
payload = jwt.decode(access_token, options={"verify_signature": False})
9595
self._expires = payload["exp"]
96+
self._resource_id=payload["aud"]
9697
return
9798

9899
def get_token(self):
@@ -117,10 +118,17 @@ def __init__(self, refresh_token, client_id, authority, resource_id):
117118
pass
118119

119120

120-
def get_token_path(resource_id, suffix):
121-
return os.path.join(
122-
os.path.expanduser("~"), ".sumo", str(resource_id) + suffix
123-
)
121+
def get_token_path(resource_id, suffix, case_uuid=None):
122+
if case_uuid is not None:
123+
return os.path.join(
124+
os.path.expanduser("~"),
125+
".sumo",
126+
str(resource_id) + "+" + str(case_uuid) + suffix,
127+
)
128+
else:
129+
return os.path.join(
130+
os.path.expanduser("~"), ".sumo", str(resource_id) + suffix
131+
)
124132

125133

126134
@tn.retry(
@@ -176,8 +184,8 @@ def get_token_cache(resource_id, suffix):
176184
retry_error_callback=_return_last_value,
177185
before_sleep=_log_retry_info,
178186
)
179-
def protect_token_cache(resource_id, suffix):
180-
token_path = get_token_path(resource_id, suffix)
187+
def protect_token_cache(resource_id, suffix, case_uuid=None):
188+
token_path = get_token_path(resource_id, suffix, case_uuid)
181189

182190
if sys.platform.startswith("linux") or sys.platform == "darwin":
183191
filemode = stat.filemode(os.stat(token_path).st_mode)
@@ -369,9 +377,9 @@ class AuthProviderSumoToken(AuthProvider):
369377
retry_error_callback=_return_last_value,
370378
before_sleep=_log_retry_info,
371379
)
372-
def __init__(self, resource_id):
373-
protect_token_cache(resource_id, ".sharedkey")
374-
token_path = get_token_path(resource_id, ".sharedkey")
380+
def __init__(self, resource_id, case_uuid=None):
381+
protect_token_cache(resource_id, ".sharedkey", case_uuid)
382+
token_path = get_token_path(resource_id, ".sharedkey", case_uuid)
375383
with open(token_path, "r") as f:
376384
self._token = f.readline().strip()
377385
return
@@ -411,13 +419,8 @@ def get_auth_provider(
411419
if access_token:
412420
return AuthProviderAccessToken(access_token)
413421
# ELSE
414-
if case_uuid is not None and os.path.exists(
415-
get_token_path(resource_id + "+" + case_uuid, ".sharedkey")
416-
):
417-
return AuthProviderSumoToken(resource_id + "+" + case_uuid)
418-
# ELSE
419-
if os.path.exists(get_token_path(resource_id, ".sharedkey")):
420-
return AuthProviderSumoToken(resource_id)
422+
if os.path.exists(get_token_path(resource_id, ".sharedkey", case_uuid)):
423+
return AuthProviderSumoToken(resource_id, case_uuid)
421424
# ELSE
422425
if os.path.exists(get_token_path(resource_id, ".token")):
423426
auth_silent = AuthProviderSilent(client_id, authority, resource_id)

0 commit comments

Comments
 (0)