16
16
.. image:: /_static/Systems/FS/TokenManager_getToken.png
17
17
:alt: https://dirac.readthedocs.io/en/integration/_images/TokenManager_getToken.png (source https://github.com/TaykYoku/DIRACIMGS/raw/main/TokenManagerService_getToken.ai)
18
18
19
- The service and its client have a mechanism for caching the received tokens.
19
+ The client has a mechanism for caching the received tokens.
20
20
This helps reduce the number of requests to both the service and the Identity Provider (IdP).
21
21
22
22
If the client has a valid **access token** in the cache, it is used until it expires.
23
23
After that you need to update. The client can update it independently if on the server where it is in ``dirac.cfg``
24
24
``client_id`` and ``client_secret`` of the Identity Provider client are registered.
25
25
26
26
Otherwise, the client makes an RPC call to the **TornadoManager** service.
27
- It in turn checks the cache and if the ``access token`` is already invalid tries to update it using a ``refresh token``.
28
- If the required token is not in the cache, then the ``refresh token`` from :py:class:`TokenDB <DIRAC.FrameworkSystem.DB.TokenDB.TokenDB>`
29
- is taken and the **exchange token** request to Identity Provider is made. The received tokens are cached.
27
+ The ``refresh token`` from :py:class:`TokenDB <DIRAC.FrameworkSystem.DB.TokenDB.TokenDB>`
28
+ is taken and the **exchange token** request to Identity Provider is made.
30
29
"""
31
30
32
31
import pprint
42
41
from DIRAC .FrameworkSystem .Utilities .TokenManagementUtilities import (
43
42
getIdProviderClient ,
44
43
getCachedKey ,
45
- getCachedToken ,
46
44
DEFAULT_RT_EXPIRATION_TIME ,
47
45
DEFAULT_AT_EXPIRATION_TIME ,
48
46
)
49
47
50
48
51
- # Used to synchronize the cache with user tokens
52
- gTokensSync = ThreadSafe .Synchronizer ()
53
-
54
-
55
49
class TokenManagerHandler (TornadoService ):
56
50
DEFAULT_AUTHORIZATION = ["authenticated" ]
57
51
@@ -61,8 +55,6 @@ def initializeHandler(cls, *args):
61
55
62
56
:return: S_OK()/S_ERROR()
63
57
"""
64
- # Cache containing tokens from scope requested by the client
65
- cls .__tokensCache = DictCache ()
66
58
67
59
# The service plays an important OAuth 2.0 role, namely it is an Identity Provider client.
68
60
# This allows you to manage tokens without the involvement of their owners.
@@ -181,7 +173,6 @@ def __checkProperties(self, requestedUserDN: str, requestedUserGroup: str):
181
173
# Not authorized!
182
174
return S_ERROR ("You can't get tokens!" )
183
175
184
- @gTokensSync
185
176
def export_getToken (
186
177
self ,
187
178
username : str = None ,
@@ -213,12 +204,8 @@ def export_getToken(
213
204
return result
214
205
idpObj = result ["Value" ]
215
206
216
- # Search for an existing token in tokensCache
217
- cachedKey = getCachedKey (idpObj , username , userGroup , scope , audience )
218
- result = getCachedToken (self .__tokensCache , cachedKey , requiredTimeLeft )
219
- if result ["OK" ]:
220
- # A valid token has been found and is returned
221
- return result
207
+ # getCachedKey is just used here to resolve the default scopes
208
+ _ , scope , * _ = getCachedKey (idpObj , username , userGroup , scope , audience )
222
209
223
210
# A client token is requested
224
211
if not username :
@@ -227,19 +214,8 @@ def export_getToken(
227
214
return result
228
215
229
216
# Get the client token with requested scope and audience
230
- scope = cachedKey [1 ]
231
- audience = cachedKey [2 ]
232
217
result = idpObj .fetchToken (grant_type = "client_credentials" , scope = scope , audience = audience )
233
- if not result ["OK" ]:
234
- return result
235
- token = result ["Value" ]
236
-
237
- # Caching new token: only get an access token (no refresh token in this context)
238
- self .__tokensCache .add (
239
- cachedKey ,
240
- result ["Value" ].get_claim ("exp" , "access_token" ) or DEFAULT_AT_EXPIRATION_TIME ,
241
- token ,
242
- )
218
+
243
219
return result
244
220
245
221
# A user token is requested
@@ -262,12 +238,6 @@ def export_getToken(
262
238
# refresh token with requested scope
263
239
result = idpObj .refreshToken (tokens .get ("refresh_token" ), group = userGroup , scope = scope )
264
240
if result ["OK" ]:
265
- # caching new tokens
266
- self .__tokensCache .add (
267
- cachedKey ,
268
- result ["Value" ].get_claim ("exp" , "refresh_token" ) or DEFAULT_RT_EXPIRATION_TIME ,
269
- result ["Value" ],
270
- )
271
241
return result
272
242
# Did not find any token associated with the found user ID
273
243
err .append (result .get ("Message" , f"No token found for { uid } " ))
0 commit comments