|
5 | 5 | from dotenv import load_dotenv |
6 | 6 | from browserbase import Browserbase |
7 | 7 | import aiohttp |
| 8 | +import boto3 |
8 | 9 |
|
9 | 10 | # Load environment variables from .env file |
10 | 11 | load_dotenv() |
@@ -97,117 +98,46 @@ async def restore_cookies(browser_tab: Page, cookie_file_path: str): |
97 | 98 |
|
98 | 99 |
|
99 | 100 | async def authenticate(browser_tab: Page, cookie_file_path: str): |
100 | | - """Authenticate to Magento using Playwright form submission, then show a detailed cookie table.""" |
101 | | - print("Attempting login with Playwright form submission") |
102 | | - username = "emma.lopez@gmail.com" |
103 | | - password = "Password.123" |
104 | | - |
105 | | - # Start fresh without cookies |
106 | | - await browser_tab.context.clear_cookies() |
107 | | - print("Cleared cookies before login.\n") |
108 | | - |
109 | | - # Optional: set a test cookie |
110 | | - await browser_tab.context.add_cookies([{ |
111 | | - "name": "test_cookie", |
112 | | - "value": "1", |
113 | | - "domain": "128.105.145.205", |
114 | | - "path": "/", |
115 | | - }]) |
116 | | - |
117 | | - # Navigate to login page |
118 | | - await browser_tab.goto(SITE_LOGIN_URL) |
119 | | - await browser_tab.wait_for_load_state("networkidle") |
120 | | - |
121 | | - print("Current URL:", browser_tab.url) |
122 | | - print("Filling in login form...\n") |
123 | | - |
124 | | - # Fill the username |
125 | | - email_field = await browser_tab.query_selector("#email") |
126 | | - if email_field: |
127 | | - await email_field.fill(username) |
128 | | - print("Filled email field") |
129 | | - else: |
130 | | - print("⚠️ Could not find email field") |
131 | | - |
132 | | - # Fill the password |
133 | | - password_field = await browser_tab.query_selector("#pass") |
134 | | - if password_field: |
135 | | - await password_field.fill(password) |
136 | | - print("Filled password field") |
137 | | - else: |
138 | | - print("⚠️ Could not find password field") |
139 | | - |
140 | | - print("Examining form elements...\n") |
141 | | - form_elements = await browser_tab.query_selector_all("form.form-login input, form#login-form input") |
142 | | - for element in form_elements: |
143 | | - name = await element.get_attribute("name") |
144 | | - value = await element.get_attribute("value") |
145 | | - input_type = await element.get_attribute("type") |
146 | | - if name: |
147 | | - print(f" Form input: {name} = {value if value else '[empty]'} (type: {input_type})") |
148 | | - |
149 | | - print("\nClicking login button...") |
150 | | - login_button = ( |
151 | | - await browser_tab.query_selector(".action.login.primary") |
152 | | - or await browser_tab.query_selector("#send2") |
153 | | - or await browser_tab.query_selector("button[type='submit']") |
154 | | - ) |
155 | | - |
156 | | - if login_button: |
157 | | - print(f"Found login button: id={await login_button.get_attribute('id')} type={await login_button.get_attribute('type')}") |
158 | | - try: |
159 | | - async with browser_tab.expect_navigation(wait_until="networkidle", timeout=15000): |
160 | | - await login_button.click() |
161 | | - print("Clicked login button and waited for navigation.\n") |
162 | | - except Exception as e: |
163 | | - print(f"Navigation timeout or error after clicking login: {e}") |
164 | | - else: |
165 | | - print("⚠️ Could not find login button!") |
166 | | - |
167 | | - # Save the cookies regardless of success |
168 | | - await store_cookies(browser_tab, cookie_file_path) |
169 | | - |
170 | | - # Check if login succeeded |
171 | | - print("Checking if login succeeded...\n") |
172 | | - |
173 | | - cookies = await browser_tab.context.cookies() |
174 | | - print(f"Cookies after login attempt ({len(cookies)}) (Markdown Table):\n") |
175 | | - print() |
176 | | - |
177 | | - # Check for Magento 2's typical session cookie (PHPSESSID) or Magento 1's (frontend) |
178 | | - magento_session_cookies = [c for c in cookies if c["name"] in ("frontend", "frontend_cid", "PHPSESSID")] |
179 | | - if magento_session_cookies: |
180 | | - print(f"✅ Found {len(magento_session_cookies)} potential Magento session cookie(s): {', '.join(c['name'] for c in magento_session_cookies)}") |
181 | | - else: |
182 | | - print("❌ No Magento 'frontend' or 'PHPSESSID' cookie found - likely not authenticated.\n") |
183 | | - |
184 | | - # Navigate to account page to confirm |
185 | | - await browser_tab.goto(f"{SITE_URL}/customer/account/") |
186 | | - await browser_tab.wait_for_load_state("networkidle") |
187 | | - |
188 | | - # Check if we're truly logged in by searching for My Account or a welcome message |
189 | | - is_logged_in = False |
190 | | - welcome_msg = await browser_tab.query_selector(".box-information .box-content p") or await browser_tab.query_selector(".welcome-msg") |
191 | | - if welcome_msg: |
192 | | - welcome_text = await welcome_msg.text_content() |
193 | | - if "Emma" in welcome_text: |
194 | | - is_logged_in = True |
195 | | - print(f"✅ Found welcome message containing 'Emma': {welcome_text.strip()}") |
196 | | - |
197 | | - page_title = await browser_tab.title() |
198 | | - if "My Account" in page_title and "Login" not in page_title: |
199 | | - is_logged_in = True |
200 | | - print(f"✅ Page title indicates logged in: {page_title}") |
201 | | - |
202 | | - if is_logged_in: |
203 | | - print("✅ Successfully logged in!\n") |
204 | | - return True |
205 | | - else: |
206 | | - current_title = await browser_tab.title() |
207 | | - print(f"❌ Login verification failed. Current page: {browser_tab.url} | Title: {current_title}\n") |
208 | | - content = await browser_tab.content() |
209 | | - snippet = content[:500].replace("\n", " ") |
210 | | - print(f"Page content snippet:\n{snippet}...\n") |
| 101 | + """Authenticate using remote API call and store cookies if successful""" |
| 102 | + print("Attempting authentication via remote API") |
| 103 | + |
| 104 | + auth_url = os.environ["AUTHENTICATE_URL"] |
| 105 | + auth_data = { |
| 106 | + "username": "emma.lopez@gmail.com", |
| 107 | + "password": "Password.123", |
| 108 | + "site_url": SITE_URL |
| 109 | + } |
| 110 | + |
| 111 | + try: |
| 112 | + async with aiohttp.ClientSession() as session: |
| 113 | + headers = { |
| 114 | + 'Content-Type': 'application/json', |
| 115 | + 'Connection': 'close' |
| 116 | + } |
| 117 | + async with session.post(auth_url, json=auth_data, headers=headers) as response: |
| 118 | + result = await response.json() |
| 119 | + |
| 120 | + if response.status == 200 and result.get('status') == 'success': |
| 121 | + print("✅ Remote authentication successful") |
| 122 | + |
| 123 | + # Navigate to account page to verify |
| 124 | + s3_client = boto3.client('s3') |
| 125 | + s3_client.download_file('test-litewebagent', 'shopping.json', cookie_file_path) |
| 126 | + print("✅ downloaded cookies from s3") |
| 127 | + cookies_restored = await restore_cookies(browser_tab, cookie_file_path) |
| 128 | + print("✅ restored cookies") |
| 129 | + await browser_tab.goto(f"{SITE_URL}/customer/account/") |
| 130 | + await browser_tab.wait_for_load_state("networkidle") |
| 131 | + |
| 132 | + # Store cookies if login succeeded |
| 133 | + # await store_cookies(browser_tab, cookie_file_path) |
| 134 | + return True |
| 135 | + else: |
| 136 | + print(f"❌ Remote authentication failed: {result.get('message', 'Unknown error')}") |
| 137 | + return False |
| 138 | + |
| 139 | + except Exception as e: |
| 140 | + print(f"❌ Remote authentication error: {str(e)}") |
211 | 141 | return False |
212 | 142 |
|
213 | 143 | async def check_login_status(browser_tab: Page) -> bool: |
|
0 commit comments