Last active
February 19, 2025 20:48
-
-
Save joshfinley/5c93175c6627fec24f77fc42d0954c36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import urllib.parse | |
def get_sign_on_url(credentials): | |
# Ensure credentials exist | |
if not credentials.access_key or not credentials.secret_key or not credentials.token: | |
raise ValueError("Invalid AWS credentials.") | |
# Generate session JSON | |
session_json = json.dumps({ | |
"sessionId": credentials.access_key, | |
"sessionKey": credentials.secret_key, | |
"sessionToken": credentials.token | |
}) | |
signin_token_url = "https://signin.aws.amazon.com/federation" | |
# Get the sign-in token | |
params = { | |
"Action": "getSigninToken", | |
"Session": session_json # Pass raw JSON, requests will encode it | |
} | |
response = requests.get(signin_token_url, params=params) | |
if response.status_code != 200: | |
raise ValueError(f"Failed to retrieve sign-in token. HTTP {response.status_code}: {response.text}") | |
signin_token = response.json().get("SigninToken") | |
if not signin_token: | |
raise ValueError("Failed to retrieve sign-in token.") | |
# Generate the AWS console sign-on URL | |
destination = "https://console.aws.amazon.com/" | |
signon_url = f"{signin_token_url}?Action=login&Issuer=Example&Destination={urllib.parse.quote(destination)}&SigninToken={signin_token}" | |
return signon_url | |
get_sign_on_url(request_session.get_credentials()) | |
fp = "C:/Program Files (x86)/Mozilla Firefox/firefox.exe" | |
webbrowser.register("firefox", None, webbrowser.BackgroundBrowser(fp)) | |
webbrowser.get("firefox").open(get_sign_on_url(request_session.get_credentials())) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment