Files
rickandCursor 20ed62b963 Sign in via Cognito directly so PerfectDraft gets the domain
PerfectDraft's Cognito PreAuthentication Lambda now requires a "domain"
field in ClientMetadata. The api.perfectdraft.com /authentication/sign-in
gateway does not forward it, so every setup and reauth failed with
"PreAuthentication failed with error 'domain'" (#11, #12, #13).
authenticate() now calls Cognito InitiateAuth (USER_PASSWORD_AUTH)
itself, the endpoint token refresh already uses.

A failed sign-in now says what was rejected and why. A wrong email or
password returns to the credentials form; a rejected token stays on the
token step; both show Cognito's reason instead of the old blanket "token
may have expired", which hid this outage.

The README snippet copies the token to the clipboard. It captures copy()
up front because Chromium removes it once the console command returns.

Root cause and fix by @cesa030 in #11; confirmed in @brettjenkins' fork.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-07 11:20:47 +01:00

255 lines
9.6 KiB
Python

"""Async API client for the PerfectDraft cloud service."""
from __future__ import annotations
import json
import logging
from typing import Any
import aiohttp
from .const import (
API_BASE_URL,
API_KEY,
COGNITO_CLIENT_ID,
COGNITO_REGION,
RECAPTCHA_ACTION,
RECAPTCHA_DOMAIN,
)
from .exceptions import (
AuthenticationError,
PerfectDraftApiError,
PerfectDraftConnectionError,
)
_LOGGER = logging.getLogger(__name__)
COGNITO_IDP_URL = f"https://cognito-idp.{COGNITO_REGION}.amazonaws.com/"
COGNITO_HEADERS = {
"Content-Type": "application/x-amz-json-1.1",
"X-Amz-Target": "AWSCognitoIdentityProviderService.InitiateAuth",
}
def _parse_cognito_error(body: str) -> tuple[str | None, str | None]:
"""Return (error type, message) from a Cognito error body.
Cognito answers ``{"__type": "...", "message": "..."}``, sometimes with a
namespaced type (``com.amazonaws...#NotAuthorizedException``). A body that
is not JSON, such as an HTML error page, becomes the message, truncated.
"""
try:
data = json.loads(body)
except ValueError:
return None, body.strip()[:200] or None
if not isinstance(data, dict):
return None, None
code = data.get("__type")
reason = data.get("message")
if isinstance(code, str):
code = code.rsplit("#", 1)[-1]
else:
code = None
return code, reason if isinstance(reason, str) else None
class PerfectDraftApiClient:
"""Async client for api.perfectdraft.com with Cognito token refresh."""
def __init__(self, session: aiohttp.ClientSession) -> None:
self._session = session
self._base = API_BASE_URL
self._access_token: str | None = None
self._id_token: str | None = None
self._refresh_token: str | None = None
@property
def access_token(self) -> str | None:
return self._access_token
@property
def refresh_token(self) -> str | None:
return self._refresh_token
@property
def id_token(self) -> str | None:
return self._id_token
def set_tokens(
self,
access_token: str | None = None,
id_token: str | None = None,
refresh_token: str | None = None,
) -> None:
"""Restore tokens from persisted config entry data."""
if access_token is not None:
self._access_token = access_token
if id_token is not None:
self._id_token = id_token
if refresh_token is not None:
self._refresh_token = refresh_token
async def authenticate(
self, email: str, password: str, recaptcha_token: str
) -> dict[str, str]:
"""Sign in directly against Cognito with a reCAPTCHA token.
The token must be generated from a real browser on perfectdraft.com
using the web reCAPTCHA Enterprise key with action Magento/login.
PerfectDraft's PreAuthentication Lambda also requires the shop's
origin as ``domain``; the api.perfectdraft.com sign-in gateway does
not forward it, so sign-in cannot go through the gateway.
"""
payload = {
"AuthFlow": "USER_PASSWORD_AUTH",
"ClientId": COGNITO_CLIENT_ID,
"AuthParameters": {"USERNAME": email, "PASSWORD": password},
"ClientMetadata": {
"recaptchaToken": recaptcha_token,
"recaptchaAction": RECAPTCHA_ACTION,
"domain": RECAPTCHA_DOMAIN,
},
}
try:
async with self._session.post(
COGNITO_IDP_URL, json=payload, headers=COGNITO_HEADERS
) as resp:
if resp.status in (400, 401, 403):
body = await resp.text()
code, reason = _parse_cognito_error(body)
raise AuthenticationError(
f"Sign-in rejected ({resp.status}): {body}",
code=code,
reason=reason,
)
if resp.status != 200:
body = await resp.text()
raise PerfectDraftApiError(resp.status, body)
data = await resp.json(content_type=None)
except aiohttp.ClientError as exc:
raise PerfectDraftConnectionError(str(exc)) from exc
result = data.get("AuthenticationResult") if isinstance(data, dict) else None
if not isinstance(result, dict) or not all(
result.get(key) for key in ("AccessToken", "IdToken", "RefreshToken")
):
challenge = data.get("ChallengeName") if isinstance(data, dict) else None
raise AuthenticationError(
f"Sign-in returned no tokens (challenge: {challenge})",
code=challenge,
reason=f"Sign-in requires {challenge}" if challenge else None,
)
self._access_token = result["AccessToken"]
self._id_token = result["IdToken"]
self._refresh_token = result["RefreshToken"]
_LOGGER.debug("Authenticated successfully")
return result
async def refresh_access_token(self) -> dict[str, Any]:
"""Refresh tokens directly via AWS Cognito (bypasses API gateway + reCAPTCHA)."""
if not self._refresh_token:
raise AuthenticationError("No refresh token available")
headers = {
"Content-Type": "application/x-amz-json-1.1",
"X-Amz-Target": "AWSCognitoIdentityProviderService.InitiateAuth",
}
payload = {
"AuthFlow": "REFRESH_TOKEN_AUTH",
"ClientId": COGNITO_CLIENT_ID,
"AuthParameters": {
"REFRESH_TOKEN": self._refresh_token,
},
}
try:
async with self._session.post(
COGNITO_IDP_URL, json=payload, headers=headers
) as resp:
if resp.status in (400, 401, 403):
body = await resp.text()
raise AuthenticationError(
f"Cognito refresh failed ({resp.status}): {body}"
)
if resp.status != 200:
body = await resp.text()
raise PerfectDraftApiError(resp.status, body)
data = await resp.json(content_type=None)
except aiohttp.ClientError as exc:
raise PerfectDraftConnectionError(str(exc)) from exc
result = data.get("AuthenticationResult", {})
self._access_token = result.get("AccessToken", self._access_token)
self._id_token = result.get("IdToken", self._id_token)
# Cognito refresh doesn't return a new RefreshToken — the original stays valid
_LOGGER.debug("Token refreshed via Cognito (expires in %ss)", result.get("ExpiresIn"))
return result
async def _request(
self, method: str, path: str, **kwargs: Any
) -> dict[str, Any]:
"""Make an authenticated API request with automatic 401 retry."""
url = f"{self._base}{path}"
headers = kwargs.pop("headers", {})
headers["x-api-key"] = API_KEY
if self._access_token:
headers["x-access-token"] = self._access_token
try:
async with self._session.request(
method, url, headers=headers, **kwargs
) as resp:
if resp.status == 401:
_LOGGER.debug("Got 401, attempting Cognito token refresh")
await self.refresh_access_token()
headers["x-access-token"] = self._access_token or ""
async with self._session.request(
method, url, headers=headers, **kwargs
) as retry_resp:
if retry_resp.status == 401:
raise AuthenticationError(
"Still unauthorized after token refresh"
)
if retry_resp.status == 429:
raise PerfectDraftApiError(429, "Rate limited")
if retry_resp.status >= 400:
body = await retry_resp.text()
raise PerfectDraftApiError(retry_resp.status, body)
return await retry_resp.json()
if resp.status == 429:
raise PerfectDraftApiError(429, "Rate limited")
if resp.status >= 400:
body = await resp.text()
raise PerfectDraftApiError(resp.status, body)
return await resp.json()
except aiohttp.ClientError as exc:
raise PerfectDraftConnectionError(str(exc)) from exc
async def get_user_profile(self) -> dict[str, Any]:
"""GET /api/me — returns user profile including machine IDs."""
return await self._request("GET", "/api/me")
async def get_machine_details(self, machine_id: str) -> dict[str, Any]:
"""GET /api/perfectdraft_machines/{machine_id} — full machine status."""
return await self._request(
"GET", f"/api/perfectdraft_machines/{machine_id}"
)
async def get_machine_active_keg(self, machine_id: str) -> dict[str, Any]:
"""GET the machine serialized with the active-keg group.
Returns the machine with a ``kegActive`` object holding the currently
tapped keg's product reference and its insertion date. This group is
mutually exclusive with the default status fields, so it needs its own
request alongside get_machine_details.
"""
return await self._request(
"GET",
f"/api/perfectdraft_machines/{machine_id}",
params={"groups[]": "perfectdraft_keg_active_read"},
)