Files
AthonetTools/auth_utils.py
T
2026-05-07 12:30:51 -04:00

67 lines
2.0 KiB
Python

import os
import requests
import json
from requests.exceptions import HTTPError
requests.packages.urllib3.disable_warnings()
DASHBOARD_USER = os.getenv("DASHBOARD_USER", "admin@hpe.com")
DASHBOARD_PASSWORD = os.getenv(
"DASHBOARD_PASSWORD",
"kxly7o6FboYUoQXSeLrs2xodHIeybQRwcMs33QC5#",
)
def _format_ipv6(host_ip):
"""If the host_ip is an IPv6 address, enclose it in square brackets."""
if ":" in host_ip and not host_ip.startswith("["):
return f"[{host_ip}]"
return host_ip
# ----- ComboCore --------
def authenticate(host_ip):
formatted_ip = _format_ipv6(host_ip) # Use the helper
url = f"https://{formatted_ip}/core/pls/api/1/auth/login"
payload = {"username": "admin", "password": "Super4dmin!"}
try:
response = requests.post(url, json=payload, verify=False)
response.raise_for_status()
return response.json()["access_token"]
except HTTPError as http_err:
raise http_err
# ----- Dashboard --------
def get_vpn_dashboard_token(base_url):
session = requests.Session()
session.headers.update({
"Content-Type": "application/json", "Accept": "*/*", "User-Agent": "Mozilla/5.0"
})
credentials = {
"user": DASHBOARD_USER,
"password": DASHBOARD_PASSWORD,
"lang": "en",
"auth_provider": "enterprise",
}
auth_response = session.post(f"{base_url}/portal/api/session/authenticate", json=credentials, verify=False)
auth_response.raise_for_status()
tenant_id = auth_response.json().get("tenants", [{}])[0].get("id")
if not tenant_id:
raise ValueError("Authentication failed: Could not retrieve Tenant ID.")
login_data = {"tenant_id": tenant_id, **credentials}
login_response = session.post(f"{base_url}/portal/api/session/login", json=login_data, verify=False)
login_response.raise_for_status()
token = login_response.json().get("token")
if not token:
raise ValueError("Login failed: Could not retrieve session token.")
return token, session