Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions simplipy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from simplipy.util.dt import utcnow
from simplipy.websocket import WebsocketClient

API_COOKIE_OVERRIDES = {"AWSALB": "", "AWSALBCORS": ""}
API_URL_HOSTNAME = "api.simplisafe.com"
API_URL_BASE = f"https://{API_URL_HOSTNAME}/v1"

Expand Down Expand Up @@ -224,6 +225,12 @@ async def _async_api_request(
if self.access_token:
kwargs["headers"]["Authorization"] = f"Bearer {self.access_token}"

if url_base == API_URL_BASE:
kwargs["cookies"] = {
**(kwargs.get("cookies") or {}),
**API_COOKIE_OVERRIDES,
}

data: dict[str, Any] | str = {}
async with self.session.request(
method, f"{url_base}/{endpoint}", **kwargs
Expand Down
17 changes: 16 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

import asyncio
from datetime import timedelta
from http.cookies import SimpleCookie
from typing import Any
from unittest.mock import AsyncMock, Mock, patch

import aiohttp
import pytest
from aresponses import ResponsesMockServer
from yarl import URL

from simplipy import API
from simplipy.errors import InvalidCredentialsError, RequestError, SimplipyError
Expand Down Expand Up @@ -114,12 +116,20 @@ async def test_401_refresh_token_success(
v2_settings_response: An API response payload.
v2_subscriptions_response: An API response payload.
"""

def unauthorized(request: Any) -> aresponses.Response:
"""Return a 401 after checking that ALB cookies are masked."""
cookies = SimpleCookie(request.headers["Cookie"])
assert cookies["AWSALB"].value == ""
assert cookies["AWSALBCORS"].value == ""
return aresponses.Response(text="Unauthorized", status=401)

async with authenticated_simplisafe_server:
authenticated_simplisafe_server.add(
"api.simplisafe.com",
f"/v1/users/{TEST_SUBSCRIPTION_ID}/subscriptions",
"get",
response=aresponses.Response(text="Unauthorized", status=401),
response=unauthorized,
)

api_token_response["access_token"] = "jjhhgg66" # noqa: S105
Expand Down Expand Up @@ -153,6 +163,11 @@ async def test_401_refresh_token_success(
TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session
)

session.cookie_jar.update_cookies(
{"AWSALB": "stale", "AWSALBCORS": "stale"},
response_url=URL("https://api.simplisafe.com"),
)

# Manually set the expiration datetime to force a refresh token flow:
simplisafe._token_last_refreshed = utcnow() - timedelta(seconds=30)

Expand Down
Loading