Initial commit of AthonetTools
This commit is contained in:
58
auth_utils.py
Normal file
58
auth_utils.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import requests
|
||||
import json
|
||||
from requests.exceptions import HTTPError
|
||||
|
||||
requests.packages.urllib3.disable_warnings()
|
||||
|
||||
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": "admin@hpe.com", "password": "JohnWayne#21",
|
||||
# "user": "admin@athonet.com", "password": "administratoR!1",
|
||||
"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
|
||||
Reference in New Issue
Block a user