summaryrefslogtreecommitdiff
path: root/backend/src/esi/refresh_token.py
diff options
context:
space:
mode:
authorlazydancer <jrpucula@gmail.com>2024-12-22 17:33:11 -0500
committerlazydancer <jrpucula@gmail.com>2024-12-22 17:33:11 -0500
commit2c5b7da8165e3f8b90748e8ced1de0e3e8d52309 (patch)
tree6d6e4e1d0cb0c7ec5e1dff83bfeda4b044258b81 /backend/src/esi/refresh_token.py
downloadhaul-2c5b7da8165e3f8b90748e8ced1de0e3e8d52309.tar.gz
Initial
Diffstat (limited to 'backend/src/esi/refresh_token.py')
-rw-r--r--backend/src/esi/refresh_token.py43
1 files changed, 43 insertions, 0 deletions
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