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

53 lines
1.7 KiB
Python

"""Exceptions for the PerfectDraft integration."""
class PerfectDraftError(Exception):
"""Base exception for PerfectDraft."""
_CREDENTIAL_ERROR_CODES = frozenset({"NotAuthorizedException", "UserNotFoundException"})
_TOKEN_ERROR_CODES = frozenset({"UserLambdaValidationException"})
_MAX_REASON_LENGTH = 200
class AuthenticationError(PerfectDraftError):
"""Raised when authentication fails (bad credentials, expired refresh token, reCAPTCHA rejection).
``code`` is the Cognito error type (e.g. ``NotAuthorizedException``) and
``reason`` its human-readable message, when the server supplied them.
"""
def __init__(
self, message: str, code: str | None = None, reason: str | None = None
) -> None:
super().__init__(message)
self.code = code
self.reason = reason
@property
def kind(self) -> str:
"""Which input was rejected: ``credentials``, ``token`` or ``other``."""
if self.code in _CREDENTIAL_ERROR_CODES:
return "credentials"
if self.code in _TOKEN_ERROR_CODES:
return "token"
return "other"
@property
def display_reason(self) -> str:
"""Server-supplied reason for the config flow, never empty."""
text = (self.reason or self.code or "").strip().rstrip(".")
return text[:_MAX_REASON_LENGTH] or "no reason given"
class PerfectDraftApiError(PerfectDraftError):
"""Raised on non-auth API errors (4xx/5xx)."""
def __init__(self, status: int, message: str = "") -> None:
super().__init__(f"API error {status}: {message}")
self.status = status
class PerfectDraftConnectionError(PerfectDraftError):
"""Raised when the API is unreachable (network error, timeout)."""