Erster Checkin: Tool arbeitet
This commit is contained in:
51
app/auth.py
Normal file
51
app/auth.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import asyncio
|
||||
import json
|
||||
from pathlib import Path
|
||||
from playwright.async_api import async_playwright
|
||||
|
||||
AUTH_STATE_FILE = Path("auth_state.json")
|
||||
|
||||
|
||||
async def ensure_login(email: str) -> None:
|
||||
"""
|
||||
Öffnet den Browser mit/ohne bestehenden auth_state.
|
||||
Beim ersten Mal: manueller Login in M365.
|
||||
Danach wird der Auth-State persistiert.
|
||||
"""
|
||||
async with async_playwright() as p:
|
||||
browser_type = p.chromium
|
||||
|
||||
if AUTH_STATE_FILE.exists():
|
||||
context = await browser_type.launch_persistent_context(
|
||||
user_data_dir="user_data",
|
||||
headless=False,
|
||||
channel="chrome",
|
||||
)
|
||||
else:
|
||||
context = await browser_type.launch_persistent_context(
|
||||
user_data_dir="user_data",
|
||||
headless=False,
|
||||
channel="chrome",
|
||||
)
|
||||
|
||||
page = await context.new_page()
|
||||
await page.goto("https://m365.cloud.microsoft/search/?auth=2")
|
||||
|
||||
print(f"Bitte mit {email} in Microsoft 365 anmelden.")
|
||||
print("Nach erfolgreichem Login Browser-Fenster schliessen oder Skript abbrechen (Ctrl+C).")
|
||||
|
||||
# Langes Warten; in echter Implementierung könnte man Events verwenden
|
||||
await page.wait_for_load_state("domcontentloaded")
|
||||
'page.wait_for_timeout(1000 * 1000)'
|
||||
|
||||
|
||||
|
||||
state = await context.storage_state()
|
||||
# storage_state() liefert dict -> JSON speichern
|
||||
AUTH_STATE_FILE.write_text(json.dumps(state), encoding="utf-8")
|
||||
|
||||
await context.close()
|
||||
|
||||
|
||||
def ensure_login_sync(email: str) -> None:
|
||||
asyncio.run(ensure_login(email))
|
||||
Reference in New Issue
Block a user