@@ -52,7 +52,10 @@ def __init__(self, ceUniqueID):
52
52
self .timeout = 5.0
53
53
# Request session
54
54
self .session = None
55
- self .headers = {}
55
+ self .headers = {
56
+ "Accept" : "application/json" ,
57
+ "Content-Type" : "application/json" ,
58
+ }
56
59
# URL used to communicate with the REST interface
57
60
self .base_url = ""
58
61
@@ -90,13 +93,6 @@ def _reset(self):
90
93
# Set up the request framework
91
94
self .session = requests .Session ()
92
95
self .session .verify = Locations .getCAsLocation ()
93
- self .headers = {
94
- "Accept" : "application/json" ,
95
- "Content-Type" : "application/json" ,
96
- }
97
- # Attach the token to the headers if present
98
- if os .environ .get ("BEARER_TOKEN" ):
99
- self .headers ["Authorization" ] = "Bearer " + os .environ ["BEARER_TOKEN" ]
100
96
101
97
return S_OK ()
102
98
@@ -186,11 +182,22 @@ def _checkSession(self):
186
182
if not self .session :
187
183
return S_ERROR ("REST interface not initialised." )
188
184
189
- # Get a proxy
185
+ # Reinitialize the authentication parameters
186
+ self .session .cert = None
187
+ self .headers .pop ("Authorization" , None )
188
+
189
+ # Get a proxy: still mandatory, even if tokens are used to authenticate
190
190
result = self ._prepareProxy ()
191
191
if not result ["OK" ]:
192
192
self .log .error ("Failed to set up proxy" , result ["Message" ])
193
193
return result
194
+
195
+ if self .token :
196
+ # Attach the token to the headers if present
197
+ self .headers ["Authorization" ] = "Bearer " + self .token ["access_token" ]
198
+ return S_OK ()
199
+
200
+ # Attach the proxy to the session, only if the token is unavailable
194
201
self .session .cert = Locations .getProxyLocation ()
195
202
return S_OK ()
196
203
@@ -236,9 +243,12 @@ def __uploadCertificate(self, delegationID, csrContent):
236
243
237
244
# Get a proxy and sign the CSR
238
245
proxy = X509Chain ()
239
- result = proxy .loadProxyFromFile (self .session .cert )
246
+ proxyFile = Locations .getProxyLocation ()
247
+ if not proxyFile :
248
+ return S_ERROR (f"No proxy available" )
249
+ result = proxy .loadProxyFromFile (proxyFile )
240
250
if not result ["OK" ]:
241
- return S_ERROR (f"Can't load { self . session . cert } : { result ['Message' ]} " )
251
+ return S_ERROR (f"Can't load { proxyFile } : { result ['Message' ]} " )
242
252
result = proxy .generateChainFromRequestString (csrContent )
243
253
if not result ["OK" ]:
244
254
self .log .error ("Problem with the Certificate Signing Request:" , result ["Message" ])
0 commit comments