Skip to content

Commit

Permalink
update auth api
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlt8 committed Jan 3, 2024
1 parent 00e4ca3 commit 2ef1a2c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
21 changes: 15 additions & 6 deletions app/wyzebridge/wyze_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 15 additions & 12 deletions app/wyzecam/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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:
Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions app/wyzecam/api_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2ef1a2c

Please sign in to comment.