From 2c5b7da8165e3f8b90748e8ced1de0e3e8d52309 Mon Sep 17 00:00:00 2001 From: lazydancer Date: Sun, 22 Dec 2024 17:33:11 -0500 Subject: Initial --- backend/src/esi/refresh_token.py | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 backend/src/esi/refresh_token.py (limited to 'backend/src/esi/refresh_token.py') diff --git a/backend/src/esi/refresh_token.py b/backend/src/esi/refresh_token.py new file mode 100644 index 0000000..0b3e4a3 --- /dev/null +++ b/backend/src/esi/refresh_token.py @@ -0,0 +1,43 @@ +import base64 +import datetime +import os + +from .shared_flow import send_token_request + +def get_access_token_from_refresh_token(refresh_token: str) -> dict[str, any]: + """ + Obtains a new access token using a refresh token. + + This function uses the provided refresh token to request a new access token + from the authentication server. It updates the expiration time of the token. + + Args: + refresh_token: The refresh token used to obtain a new access token. + + Returns: + A dictionary containing the new access token data, including 'expires_at'. + """ + client_id = os.getenv('CLIENT_ID') + secret_key = os.getenv('SECRET_KEY') + + if not client_id or not secret_key: + raise EnvironmentError("CLIENT_ID and SECRET_KEY must be set in environment variables.") + + user_pass = f"{client_id}:{secret_key}" + basic_auth = base64.urlsafe_b64encode(user_pass.encode('utf-8')).decode() + auth_header = f"Basic {basic_auth}" + + form_values = { + "grant_type": "refresh_token", + "refresh_token": refresh_token, + } + + headers = {"Authorization": auth_header} + + response = send_token_request(form_values, add_headers=headers) + data = response.json() + + current_timestamp = int(datetime.datetime.now().timestamp()) + data["expires_at"] = current_timestamp + data.get("expires_in", 0) + + return data -- cgit v1.3