This commit is contained in:
+48
-43
@@ -8,7 +8,6 @@ and manages session tokens for API requests.
|
||||
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import json
|
||||
import logging
|
||||
from typing import Optional, Dict, Any
|
||||
from urllib.parse import urljoin
|
||||
@@ -22,7 +21,7 @@ class AuthManager:
|
||||
Args:
|
||||
api_url: Base URL of the API
|
||||
"""
|
||||
self.api_url = api_url.rstrip('/')
|
||||
self.api_url = api_url.rstrip("/")
|
||||
self.login_url = urljoin(self.api_url, "/v1/auth/login")
|
||||
self.create_session_url = urljoin(self.api_url, "/v1/auth/create-session")
|
||||
self.session_token: Optional[str] = None
|
||||
@@ -34,18 +33,18 @@ class AuthManager:
|
||||
|
||||
# Standard headers for login requests
|
||||
self.headers = {
|
||||
'accept': 'application/json, text/plain, */*',
|
||||
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,ro;q=0.7',
|
||||
'content-type': 'application/json;charset=UTF-8',
|
||||
'origin': 'https://www.parentzone.me',
|
||||
'priority': 'u=1, i',
|
||||
'sec-ch-ua': '"Not;A=Brand";v="99", "Google Chrome";v="139", "Chromium";v="139"',
|
||||
'sec-ch-ua-mobile': '?0',
|
||||
'sec-ch-ua-platform': '"macOS"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'same-site',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36'
|
||||
"accept": "application/json, text/plain, */*",
|
||||
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8,ro;q=0.7",
|
||||
"content-type": "application/json;charset=UTF-8",
|
||||
"origin": "https://www.parentzone.me",
|
||||
"priority": "u=1, i",
|
||||
"sec-ch-ua": '"Not;A=Brand";v="99", "Google Chrome";v="139", "Chromium";v="139"',
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-platform": '"macOS"',
|
||||
"sec-fetch-dest": "empty",
|
||||
"sec-fetch-mode": "cors",
|
||||
"sec-fetch-site": "same-site",
|
||||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
|
||||
}
|
||||
|
||||
async def login(self, email: str, password: str) -> bool:
|
||||
@@ -64,18 +63,13 @@ class AuthManager:
|
||||
self.logger.info(f"Attempting login for {email}")
|
||||
|
||||
# Step 1: Login to get user accounts
|
||||
login_data = {
|
||||
"email": email,
|
||||
"password": password
|
||||
}
|
||||
login_data = {"email": email, "password": password}
|
||||
|
||||
timeout = aiohttp.ClientTimeout(total=30)
|
||||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||
try:
|
||||
async with session.post(
|
||||
self.login_url,
|
||||
headers=self.headers,
|
||||
json=login_data
|
||||
self.login_url, headers=self.headers, json=login_data
|
||||
) as response:
|
||||
self.logger.info(f"Login response status: {response.status}")
|
||||
|
||||
@@ -89,20 +83,26 @@ class AuthManager:
|
||||
if isinstance(data, list) and len(data) > 0:
|
||||
# Use the first account
|
||||
first_account = data[0]
|
||||
self.user_id = first_account.get('id')
|
||||
self.user_name = first_account.get('name')
|
||||
self.provider_name = first_account.get('providerName')
|
||||
self.user_id = first_account.get("id")
|
||||
self.user_name = first_account.get("name")
|
||||
self.provider_name = first_account.get("providerName")
|
||||
|
||||
self.logger.info(f"Selected account: {self.user_name} at {self.provider_name} (ID: {self.user_id})")
|
||||
self.logger.info(
|
||||
f"Selected account: {self.user_name} at {self.provider_name} (ID: {self.user_id})"
|
||||
)
|
||||
|
||||
# Step 2: Create session with the account ID
|
||||
return await self._create_session(password)
|
||||
else:
|
||||
self.logger.error(f"Unexpected login response format: {data}")
|
||||
self.logger.error(
|
||||
f"Unexpected login response format: {data}"
|
||||
)
|
||||
return False
|
||||
else:
|
||||
error_text = await response.text()
|
||||
self.logger.error(f"Login failed with status {response.status}: {error_text}")
|
||||
self.logger.error(
|
||||
f"Login failed with status {response.status}: {error_text}"
|
||||
)
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
@@ -125,24 +125,21 @@ class AuthManager:
|
||||
|
||||
self.logger.info(f"Creating session for user ID: {self.user_id}")
|
||||
|
||||
session_data = {
|
||||
"id": self.user_id,
|
||||
"password": password
|
||||
}
|
||||
session_data = {"id": self.user_id, "password": password}
|
||||
|
||||
# Add x-api-product header for session creation
|
||||
session_headers = self.headers.copy()
|
||||
session_headers['x-api-product'] = 'iConnect'
|
||||
session_headers["x-api-product"] = "iConnect"
|
||||
|
||||
timeout = aiohttp.ClientTimeout(total=30)
|
||||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||
try:
|
||||
async with session.post(
|
||||
self.create_session_url,
|
||||
headers=session_headers,
|
||||
json=session_data
|
||||
self.create_session_url, headers=session_headers, json=session_data
|
||||
) as response:
|
||||
self.logger.info(f"Create session response status: {response.status}")
|
||||
self.logger.info(
|
||||
f"Create session response status: {response.status}"
|
||||
)
|
||||
|
||||
if response.status == 200:
|
||||
data = await response.json()
|
||||
@@ -150,16 +147,20 @@ class AuthManager:
|
||||
self.logger.debug(f"Session response data: {data}")
|
||||
|
||||
# Extract API key from response
|
||||
if isinstance(data, dict) and 'key' in data:
|
||||
self.api_key = data['key']
|
||||
if isinstance(data, dict) and "key" in data:
|
||||
self.api_key = data["key"]
|
||||
self.logger.info("API key obtained successfully")
|
||||
return True
|
||||
else:
|
||||
self.logger.error(f"No 'key' field in session response: {data}")
|
||||
self.logger.error(
|
||||
f"No 'key' field in session response: {data}"
|
||||
)
|
||||
return False
|
||||
else:
|
||||
error_text = await response.text()
|
||||
self.logger.error(f"Session creation failed with status {response.status}: {error_text}")
|
||||
self.logger.error(
|
||||
f"Session creation failed with status {response.status}: {error_text}"
|
||||
)
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
@@ -177,8 +178,8 @@ class AuthManager:
|
||||
|
||||
if self.api_key:
|
||||
# Use x-api-key header for authenticated requests
|
||||
headers['x-api-key'] = self.api_key
|
||||
headers['x-api-product'] = 'iConnect'
|
||||
headers["x-api-key"] = self.api_key
|
||||
headers["x-api-product"] = "iConnect"
|
||||
|
||||
return headers
|
||||
|
||||
@@ -216,7 +217,11 @@ async def test_login():
|
||||
print("✅ Login successful!")
|
||||
print(f"User: {auth_manager.user_name} at {auth_manager.provider_name}")
|
||||
print(f"User ID: {auth_manager.user_id}")
|
||||
print(f"API Key: {auth_manager.api_key[:20]}..." if auth_manager.api_key else "No API key found")
|
||||
print(
|
||||
f"API Key: {auth_manager.api_key[:20]}..."
|
||||
if auth_manager.api_key
|
||||
else "No API key found"
|
||||
)
|
||||
|
||||
# Test getting auth headers
|
||||
headers = auth_manager.get_auth_headers()
|
||||
|
||||
Reference in New Issue
Block a user