From 7e334f0d318bfbcac679293b6cc75ddddbaeb017 Mon Sep 17 00:00:00 2001 From: Chris Burr Date: Thu, 2 Nov 2023 08:23:12 +0100 Subject: [PATCH] Add serialize_credentials method for DIRAC to use when writing tokens to proxies --- src/diracx/core/utils.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/diracx/core/utils.py b/src/diracx/core/utils.py index 39fb23de..9cd5f9b8 100644 --- a/src/diracx/core/utils.py +++ b/src/diracx/core/utils.py @@ -20,10 +20,12 @@ def dotenv_files_from_environment(prefix: str) -> list[str]: return [v for _, v in sorted(env_files.items())] -def write_credentials(token_response: TokenResponse, *, location: Path | None = None): - """Write credentials received in dirax_preferences.credentials_path""" - from diracx.core.preferences import get_diracx_preferences +def serialize_credentials(token_response: TokenResponse) -> str: + """Serialize DiracX client credentials to a string + This method is separated from write_credentials to allow for DIRAC to be + able to serialize credentials for inclusion in the proxy file. + """ expires = datetime.now(tz=timezone.utc) + timedelta( seconds=token_response.expires_in - EXPIRES_GRACE_SECONDS ) @@ -32,6 +34,13 @@ def write_credentials(token_response: TokenResponse, *, location: Path | None = "refresh_token": token_response.refresh_token, "expires_on": int(datetime.timestamp(expires)), } + return json.dumps(credential_data) + + +def write_credentials(token_response: TokenResponse, *, location: Path | None = None): + """Write credentials received in dirax_preferences.credentials_path""" + from diracx.core.preferences import get_diracx_preferences + credentials_path = location or get_diracx_preferences().credentials_path credentials_path.parent.mkdir(parents=True, exist_ok=True) - credentials_path.write_text(json.dumps(credential_data)) + credentials_path.write_text(serialize_credentials(token_response))