30 lines
943 B
Python
30 lines
943 B
Python
|
|
import httpx
|
||
|
|
from urllib.parse import urljoin
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.tlon.errors import UrbitAuthError
|
||
|
|
|
||
|
|
|
||
|
|
async def authenticate(ship_url: str, code: str, *,
|
||
|
|
dangerously_allow_private_network: bool = False,
|
||
|
|
timeout: float = 15.0) -> str:
|
||
|
|
login_url = urljoin(ship_url, "/~/login")
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(
|
||
|
|
timeout=timeout,
|
||
|
|
max_redirects=3,
|
||
|
|
follow_redirects=True,
|
||
|
|
) as client:
|
||
|
|
response = await client.post(
|
||
|
|
login_url,
|
||
|
|
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
||
|
|
content=f"password={code}",
|
||
|
|
)
|
||
|
|
|
||
|
|
if response.status_code != 200:
|
||
|
|
raise UrbitAuthError(f"Auth failed: HTTP {response.status_code}")
|
||
|
|
|
||
|
|
cookie = response.headers.get("set-cookie")
|
||
|
|
if not cookie:
|
||
|
|
raise UrbitAuthError("missing_cookie: Auth succeeded but no cookie")
|
||
|
|
|
||
|
|
return cookie.split(";")[0]
|