From 2ef1a2cbc06f14fbfe6a67189b152a8086182484 Mon Sep 17 00:00:00 2001 From: mrlt8 <67088095+mrlt8@users.noreply.github.com> Date: Wed, 3 Jan 2024 07:06:01 -0700 Subject: [PATCH] update auth api --- app/wyzebridge/wyze_api.py | 21 +++++++++++++++------ app/wyzecam/api.py | 27 +++++++++++++++------------ app/wyzecam/api_models.py | 2 -- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/app/wyzebridge/wyze_api.py b/app/wyzebridge/wyze_api.py index bc192ac0..f58a421b 100644 --- a/app/wyzebridge/wyze_api.py +++ b/app/wyzebridge/wyze_api.py @@ -73,11 +73,13 @@ def wrapper(self, *args: Any, **kwargs: Any): class WyzeCredentials: - __slots__ = "email", "password" + __slots__ = "email", "password", "key_id", "api_key" def __init__(self) -> None: self.email: str = getenv("WYZE_EMAIL", "").strip() self.password: str = getenv("WYZE_PASSWORD", "").strip() + self.key_id: str = getenv("API_ID", "").strip() + self.api_key: str = getenv("API_KEY", "").strip() if not self.is_set: logger.warning("[WARN] Credentials are NOT set") @@ -93,9 +95,6 @@ def update(self, email: str, password: str) -> None: def same_email(self, email: str) -> bool: return self.email.lower() == email.lower() if self.is_set else True - def creds(self) -> tuple[str, str]: - return (self.email, self.password) - def login_check(self): if self.is_set: return @@ -138,7 +137,12 @@ def login(self, fresh_data: bool = False) -> Optional[WyzeCredential]: self.creds.login_check() try: - self.auth = wyzecam.login(*self.creds.creds()) + self.auth = wyzecam.login( + email=self.creds.email, + password=self.creds.password, + api_key=self.creds.api_key, + key_id=self.creds.key_id, + ) except HTTPError as ex: logger.error(f"⚠️ {ex}") if ex.response.status_code == 403: @@ -263,7 +267,12 @@ def _mfa_auth(self): logger.info(f'🔑 Using {resp["verification_code"]} for authentication') try: - self.auth = wyzecam.login(*self.creds.creds(), self.auth.phone_id, resp) + self.auth = wyzecam.login( + email=self.creds.email, + password=self.creds.password, + phone_id=self.auth.phone_id, + mfa=resp, + ) if self.auth.access_token: logger.info("✅ Verification code accepted!") except HTTPError as ex: diff --git a/app/wyzecam/api.py b/app/wyzecam/api.py index 0f4c09fc..754c4f36 100644 --- a/app/wyzecam/api.py +++ b/app/wyzecam/api.py @@ -94,8 +94,6 @@ def login( [get_camera_list()][wyzecam.api.get_camera_list]. """ phone_id = phone_id or str(uuid.uuid4()) - key_id = key_id or getenv("API_ID") - api_key = api_key or getenv("API_KEY") headers = _headers(phone_id, key_id=key_id, api_key=api_key) headers["content-type"] = "application/json" @@ -113,10 +111,7 @@ def login( resp = post(f"{AUTH_API}/{api_version}/user/login", data=payload, headers=headers) resp.raise_for_status() - credential = WyzeCredential.model_validate(dict(resp.json(), phone_id=phone_id)) - credential.key_id = key_id - credential.api_key = api_key - return credential + return WyzeCredential.model_validate(dict(resp.json(), phone_id=phone_id)) def send_sms_code(auth_info: WyzeCredential, phone: str = "Primary") -> str: @@ -137,7 +132,7 @@ def send_sms_code(auth_info: WyzeCredential, phone: str = "Primary") -> str: "sessionId": auth_info.sms_session_id, "userId": auth_info.user_id, }, - headers=_headers(auth_info=auth_info), + headers=_headers(auth_info.phone_id), ) resp.raise_for_status() @@ -161,7 +156,7 @@ def send_email_code(auth_info: WyzeCredential) -> str: "userId": auth_info.user_id, "sessionId": auth_info.email_session_id, }, - headers=_headers(auth_info=auth_info), + headers=_headers(auth_info.phone_id), ) resp.raise_for_status() @@ -368,12 +363,20 @@ def _payload( } -def _headers(phone_id: Optional[str] = None, key_id: Optional[str] = None, api_key: Optional[str] = None, auth_info: Optional[WyzeCredential] = None) -> dict[str, str]: - phone_id = phone_id or (auth_info and auth_info.phone_id) +def _headers( + phone_id: Optional[str] = None, + key_id: Optional[str] = None, + api_key: Optional[str] = None, +) -> dict[str, str]: + """Format headers for api requests. + + key_id and api_key are only needed when making a request to the /api/user/login endpoint. + + phone_id is required for other login-related endpoints. + """ if not phone_id: return {"user-agent": SCALE_USER_AGENT} - key_id = key_id or (auth_info and auth_info.api_key) or getenv("API_ID") - api_key = api_key or (auth_info and auth_info.key_id) or getenv("API_KEY") + if key_id and api_key: return { "apikey": api_key, diff --git a/app/wyzecam/api_models.py b/app/wyzecam/api_models.py index 614e4c54..5c25e0c8 100644 --- a/app/wyzecam/api_models.py +++ b/app/wyzecam/api_models.py @@ -78,8 +78,6 @@ class WyzeCredential(BaseModel): sms_session_id: Optional[str] = None email_session_id: Optional[str] = None phone_id: Optional[str] = str(uuid.uuid4()) - key_id: Optional[str] = None - api_key: Optional[str] = None class WyzeAccount(BaseModel):