Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-20 07:58:59

0001 import copy
0002 import hashlib
0003 import json
0004 import time
0005 
0006 import requests
0007 
0008 
0009 def _md5sum(data):
0010     """
0011     get md5sum hexadecimal string of data
0012     """
0013     hash = hashlib.md5()
0014     hash.update(str(data).encode("latin_1"))
0015     hash_hex = hash.hexdigest()
0016     return hash_hex
0017 
0018 
0019 def endpoint_to_filename(endpoint):
0020     """
0021     get token file name according to service (CE, storage, etc.) endpoint
0022     currently directly take its md5sum hash as file name
0023     """
0024     filename = _md5sum(endpoint)
0025     return filename
0026 
0027 
0028 class WLCG_scopes(object):
0029     COMPUTE_ALL = "compute.read compute.modify compute.create compute.cancel"
0030 
0031 
0032 class IssuerBroker(object):
0033     """
0034     Talk to token issuer with client credentials flow
0035     """
0036 
0037     def __init__(self, issuer, client_id, client_secret, name="unknown"):
0038         self.issuer = issuer
0039         self.client_id = client_id
0040         self.client_secret = client_secret
0041         self.name = name
0042         self.timeout = 3
0043         # derived attributes
0044         self.token_request_url = f"{self.issuer}/token"
0045         self._base_post_data = {
0046             "grant_type": "client_credentials",
0047             "client_id": self.client_id,
0048             "client_secret": self.client_secret,
0049         }
0050 
0051     def _post(self, **kwarg):
0052         data_dict = copy.deepcopy(self._base_post_data)
0053         data_dict.update(kwarg)
0054         data = copy.deepcopy(data_dict)
0055         resp = requests.post(self.token_request_url, data=data, timeout=self.timeout)
0056         return resp
0057 
0058     def get_access_token(self, aud=None, scope=None):
0059         resp = self._post(audience=aud, scope=scope)
0060         if resp.status_code == requests.codes.ok:
0061             try:
0062                 resp_dict = json.loads(resp.text)
0063             except Exception as e:
0064                 raise
0065             token = resp_dict["access_token"]
0066             return token
0067         else:
0068             resp.raise_for_status()