Skip to content

Commit

Permalink
add a way to get token without cache
Browse files Browse the repository at this point in the history
  • Loading branch information
chaen committed Mar 6, 2024
1 parent 69a7fbc commit 4f656e4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/DIRAC/DataManagementSystem/Client/FTS3Job.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ def _constructTransferJob(self, pinTime, allLFNs, target_spacetoken, protocols=N
userGroup="lhcb_data",
requiredTimeLeft=3600,
scope=[f"storage.read:/{srcTokenPath}", "offline_access"],
useCache=False,
)
if not res["OK"]:
return res
Expand All @@ -512,6 +513,7 @@ def _constructTransferJob(self, pinTime, allLFNs, target_spacetoken, protocols=N
userGroup="lhcb_data",
requiredTimeLeft=3600,
scope=[f"storage.modify:/{dstTokenPath}", f"storage.read:/{dstTokenPath}", "offline_access"],
useCache=False,
)
if not res["OK"]:
return res
Expand Down
54 changes: 53 additions & 1 deletion src/DIRAC/FrameworkSystem/Client/TokenManagerClient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" The TokenManagerClient is a class representing the client of the DIRAC
:py:mod:`TokenManager <DIRAC.FrameworkSystem.Service.TokenManagerHandler>` service.
"""

import time

from DIRAC import S_OK, S_ERROR
Expand Down Expand Up @@ -31,7 +32,6 @@ def __init__(self, **kwargs):
self.__tokensCache = DictCache()
self.idps = IdProviderFactory()

@gTokensSync
def getToken(
self,
username: str = None,
Expand All @@ -40,6 +40,58 @@ def getToken(
audience: str = None,
identityProvider: str = None,
requiredTimeLeft: int = 0,
useCache: bool = True,
):
meth = self.getTokenWithCache if useCache else self.getTokenWithoutCache

return meth(
username=username,
userGroup=userGroup,
scope=scope,
audience=audience,
identityProvider=identityProvider,
requiredTimeLeft=requiredTimeLeft,
)

def getTokenWithoutCache(
self,
username: str = None,
userGroup: str = None,
scope: list[str] = None,
audience: str = None,
identityProvider: str = None,
requiredTimeLeft: int = 0,
):
"""Get an access token for a user/group keeping the local cache
:param username: user name
:param userGroup: group name
:param scope: scope
:param audience: audience
:param identityProvider: identity Provider
:param requiredTimeLeft: required time
:param cacheToken: if True (default) save the token in cache
:return: S_OK(dict)/S_ERROR()
"""
# Get an IdProvider Client instance
result = getIdProviderClient(userGroup, identityProvider)
if not result["OK"]:
return result
idpObj = result["Value"]

# No token in cache: get a token from the server
return self.executeRPC(username, userGroup, scope, audience, idpObj.name, requiredTimeLeft, call="getToken")

@gTokensSync
def getTokenWithCache(
self,
username: str = None,
userGroup: str = None,
scope: list[str] = None,
audience: str = None,
identityProvider: str = None,
requiredTimeLeft: int = 0,
):
"""Get an access token for a user/group keeping the local cache
Expand Down

0 comments on commit 4f656e4

Please sign in to comment.