-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_manager.py
More file actions
93 lines (79 loc) · 3.17 KB
/
session_manager.py
File metadata and controls
93 lines (79 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from playwright.sync_api import sync_playwright
import requests
import os
from utils import (
logger,
save_json,
load_json,
save_encrypted_json,
load_encrypted_json,
generate_encryption_key,
)
from config import config
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
def get_session_with_retry() -> requests.Session:
session = requests.Session()
retry = Retry(total=5, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504])
adapter = HTTPAdapter(max_retries=retry)
session.mount("http://", adapter)
session.mount("https://", adapter)
session.headers.update(config.HEADERS)
return session
def refresh_cookies(verbose: bool = False):
"""Refresh cookies using Playwright browser automation."""
logger.info("Launching Playwright for manual login...")
if config.ENCRYPT_COOKIES and not config.SECRET_KEY:
raise ValueError(
"ENCRYPT_COOKIES is enabled but SHIFT_SECRET_KEY environment "
"variable is not set. Please set SHIFT_SECRET_KEY to a "
"secure password."
)
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto(config.LOGIN_URL)
page.wait_for_timeout(config.PLAYWRIGHT_TIMEOUT)
cookies = context.cookies()
# Save cookies with encryption if enabled
if config.ENCRYPT_COOKIES and config.SECRET_KEY:
encryption_key = generate_encryption_key(config.SECRET_KEY)
save_encrypted_json(config.COOKIES_FILE, cookies, encryption_key)
logger.info("Cookies encrypted and saved.")
else:
save_json(config.COOKIES_FILE, cookies)
logger.info("Cookies saved (unencrypted).")
# Only close browser if not in verbose mode
if not verbose:
browser.close()
logger.info("Cookie refresh process completed.")
else:
logger.info(
"Cookie refresh process completed. "
"Browser left open for verbose mode."
)
def get_session() -> requests.Session:
"""Get authenticated session with cookies loaded."""
if not os.path.exists(config.COOKIES_FILE):
refresh_cookies()
# Load cookies with decryption if enabled
if config.ENCRYPT_COOKIES and config.SECRET_KEY:
encryption_key = generate_encryption_key(config.SECRET_KEY)
cookies = load_encrypted_json(config.COOKIES_FILE, encryption_key)
else:
cookies = load_json(config.COOKIES_FILE)
session = get_session_with_retry()
for c in cookies:
session.cookies.set(
c["name"], c["value"], domain=c.get("domain", "shift.gearboxsoftware.com")
)
return session
def verify_login(session: requests.Session) -> bool:
try:
resp = session.get(config.REDEEM_URL, timeout=10)
resp.raise_for_status()
return "Sign In" not in resp.text
except Exception as e:
logger.warning(f"Login verification failed: {e}")
return False