@@ -36,6 +36,9 @@ def get_token(self):
3636 # ELSE
3737 return result ["access_token" ]
3838
39+ def get_authorization (self ):
40+ return {"Authorization" : "Bearer " + self .get_token ()}
41+
3942 pass
4043
4144
@@ -68,21 +71,21 @@ def __init__(self, refresh_token, client_id, authority, resource_id):
6871 pass
6972
7073
71- def get_token_path (resource_id ):
74+ def get_token_path (resource_id , suffix ):
7275 return os .path .join (
73- os .path .expanduser ("~" ), ".sumo" , str (resource_id ) + ".token"
76+ os .path .expanduser ("~" ), ".sumo" , str (resource_id ) + suffix
7477 )
7578
7679
77- def get_token_cache (resource_id ):
80+ def get_token_cache (resource_id , suffix ):
7881 # https://github.com/AzureAD/microsoft-authentication-extensions-\
7982 # for-python
8083 # Encryption not supported on linux servers like rgs, and
8184 # neither is common usage from many cluster nodes.
8285 # Encryption is supported on Windows and Mac.
8386
8487 cache = None
85- token_path = get_token_path (resource_id )
88+ token_path = get_token_path (resource_id , suffix )
8689 if sys .platform .startswith ("linux" ):
8790 persistence = FilePersistence (token_path )
8891 cache = PersistedTokenCache (persistence )
@@ -107,10 +110,10 @@ def get_token_cache(resource_id):
107110 return cache
108111
109112
110- def protect_token_cache (resource_id ):
111- token_path = get_token_path (resource_id )
113+ def protect_token_cache (resource_id , suffix ):
114+ token_path = get_token_path (resource_id , suffix )
112115
113- if sys .platform .startswith ("linux" ):
116+ if sys .platform .startswith ("linux" ) or sys . platform == "darwin" :
114117 filemode = stat .filemode (os .stat (token_path ).st_mode )
115118 if filemode != "-rw-------" :
116119 os .chmod (token_path , 0o600 )
@@ -127,7 +130,7 @@ def protect_token_cache(resource_id):
127130class AuthProviderInteractive (AuthProvider ):
128131 def __init__ (self , client_id , authority , resource_id ):
129132 super ().__init__ (resource_id )
130- cache = get_token_cache (resource_id )
133+ cache = get_token_cache (resource_id , ".token" )
131134 self ._app = msal .PublicClientApplication (
132135 client_id = client_id , authority = authority , token_cache = cache
133136 )
@@ -176,7 +179,7 @@ def login(self):
176179 )
177180 return
178181
179- protect_token_cache (self ._resource_id )
182+ protect_token_cache (self ._resource_id , ".token" )
180183 print ("Equinor Azure login for Sumo access was successful" )
181184 return
182185
@@ -186,7 +189,7 @@ def login(self):
186189class AuthProviderDeviceCode (AuthProvider ):
187190 def __init__ (self , client_id , authority , resource_id ):
188191 super ().__init__ (resource_id )
189- cache = get_token_cache (resource_id )
192+ cache = get_token_cache (resource_id , ".token" )
190193 self ._app = msal .PublicClientApplication (
191194 client_id = client_id , authority = authority , token_cache = cache
192195 )
@@ -215,7 +218,7 @@ def login(self):
215218 % json .dumps (result , indent = 4 )
216219 )
217220
218- protect_token_cache (self ._resource_id )
221+ protect_token_cache (self ._resource_id , ".token" )
219222
220223 return
221224
@@ -235,6 +238,21 @@ def get_token(self):
235238 pass
236239
237240
241+ class AuthProviderSumoToken (AuthProvider ):
242+ def __init__ (self , resource_id ):
243+ protect_token_cache (resource_id , ".sharedkey" )
244+ token_path = get_token_path (resource_id , ".sharedkey" )
245+ with open (token_path , "r" ) as f :
246+ self ._token = f .readline ().strip ()
247+ return
248+
249+ def get_token (self ):
250+ return self ._token
251+
252+ def get_authorization (self ):
253+ return {"X-SUMO-Token" : self ._token }
254+
255+
238256def get_auth_provider (
239257 client_id ,
240258 authority ,
@@ -252,6 +270,9 @@ def get_auth_provider(
252270 if access_token :
253271 return AuthProviderAccessToken (access_token )
254272 # ELSE
273+ if os .path .exists (get_token_path (resource_id , ".sharedkey" )):
274+ return AuthProviderSumoToken (resource_id )
275+ # ELSE
255276 if interactive :
256277 return AuthProviderInteractive (client_id , authority , resource_id )
257278 # ELSE
0 commit comments