Skip to content

Commit 3ef0f26

Browse files
committed
Added debug info to authentication
1 parent d3b58f1 commit 3ef0f26

8 files changed

Lines changed: 397 additions & 119 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
__pycache__/
2+
*.py[cod]
3+
.DS_Store
14
_venv
25
venv
36
dist

conftest.py

Whitespace-only changes.

src/sumo/wrapper/_auth.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,21 @@ def __init__(self, client_id, resource_id, authority=AUTHORITY_URI, client_crede
2525
self.app = msal.PublicClientApplication(self.client_id, authority=AUTHORITY_URI,
2626
client_credential=self.client_credentials, token_cache=self.cache)
2727
self.accounts = self.app.get_accounts()
28-
self._oauth_get_token_silent() if self._cache_available() else self._oauth_device_code()
2928

29+
if self._cache_available():
30+
if not self.accounts:
31+
print("Token cache found but have no accounts")
32+
else:
33+
print("Get token and maybe refresh")
34+
self._oauth_get_token_silent()
35+
else:
36+
print("No token cache found, reauthenticate")
37+
self._oauth_device_code()
38+
39+
3040
def get_token(self):
3141
if self.is_token_expired():
42+
print("Token expired, get new token")
3243
self._oauth_get_token_silent()
3344

3445
return self.result["access_token"]
@@ -41,12 +52,21 @@ def is_token_expired(self):
4152

4253
def _oauth_get_token_silent(self):
4354
if not self.accounts:
55+
print("Get accounts")
4456
self.accounts = self.app.get_accounts()
4557

4658
if not self._check_token_security():
4759
raise SystemError('The token is not stored safely.')
4860

49-
self.result = self.app.acquire_token_silent([self.scope], account=self.accounts[0])
61+
self.result = self.app.acquire_token_silent_with_error([self.scope], account=self.accounts[0])
62+
63+
if "access_token" in self.result:
64+
print("Token found")
65+
elif "error" in self.result:
66+
print("Error getting access token" + self.result["error"])
67+
else:
68+
print("Acuire token failed")
69+
5070
self._set_expiring_date(int(self.result['expires_in']))
5171
self._write_cache()
5272

src/sumo/wrapper/_call_sumo_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .config import CLIENT_ID, RESOURCE_ID
1+
from .config import APP_REGISTRATION
22
from ._call_azure_api import CallAzureApi
33

44

@@ -16,8 +16,8 @@ def __init__(self, env='dev', resource_id=None, client_id=None, outside_token=Fa
1616
else:
1717
self.base_url = f'https://main-sumo-{env}.radix.equinor.com/api/v1'
1818

19-
resource_id = resource_id if resource_id else RESOURCE_ID
20-
client_id = client_id if client_id else CLIENT_ID
19+
resource_id = resource_id if resource_id else APP_REGISTRATION[env]["RESOURCE_ID"]
20+
client_id = client_id if client_id else APP_REGISTRATION[env]["CLIENT_ID"]
2121

2222
self.callAzureApi = CallAzureApi(resource_id, client_id, outside_token)
2323

src/sumo/wrapper/config.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
CLIENT_ID = '1826bd7c-582f-4838-880d-5b4da5c3eea2'
2-
RESOURCE_ID = '88d2b022-3539-4dda-9e66-853801334a86'
1+
APP_REGISTRATION = {
2+
"prod" : {
3+
"CLIENT_ID" : '1826bd7c-582f-4838-880d-5b4da5c3eea2',
4+
"RESOURCE_ID" : '88d2b022-3539-4dda-9e66-853801334a86'
5+
},
6+
"test" : {
7+
"CLIENT_ID" : 'd57a8f87-4e28-4391-84d6-34356d5876a2',
8+
"RESOURCE_ID" : '9e5443dd-3431-4690-9617-31eed61cb55a'
9+
},
10+
"dev" : {
11+
"CLIENT_ID" : '1826bd7c-582f-4838-880d-5b4da5c3eea2',
12+
"RESOURCE_ID" : '88d2b022-3539-4dda-9e66-853801334a86'
13+
}
14+
}
15+

0 commit comments

Comments
 (0)