Skip to content

Commit

Permalink
fix: made every token expire
Browse files Browse the repository at this point in the history
  • Loading branch information
Lash-L committed Feb 14, 2023
1 parent f1156ae commit bea0e8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/southern_company_api/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ def __init__(self, username: str, password: str):
self._jwt: typing.Optional[str] = None
self._jwt_expiry: datetime.datetime = datetime.datetime.now()
self._sc: typing.Optional[str] = None
self._sc_expiry = datetime.datetime.now()
self._request_token: typing.Optional[str] = None
self._request_token_expiry: datetime.datetime = datetime.datetime.now()
self._accounts: List[Account] = []

@property
async def sc(self) -> str:
if self._sc is None:
if self._sc is None or datetime.datetime.now() >= self._sc_expiry:
return await self._get_sc_web_token()
return self._sc

Expand All @@ -68,8 +70,12 @@ async def jwt(self) -> str:

@property
async def request_token(self) -> str:
if self._request_token is None:
if (
self._request_token is None
or datetime.datetime.now() >= self._request_token_expiry
):
self._request_token = await get_request_verification_token()
self._request_token_expiry = datetime.datetime.now() + datetime.timedelta(hours=3)
return self._request_token

async def connect(self) -> None:
Expand All @@ -89,8 +95,15 @@ async def authenticate(self) -> bool:

async def _get_sc_web_token(self) -> str:
"""Gets a sc_web_token which we get from a successful log in"""
if self._request_token is None:
# update to use property
if (
self._request_token is None
or datetime.datetime.now() >= self._request_token_expiry
):
self._request_token = await get_request_verification_token()
self._request_token_expiry = datetime.datetime.now() + datetime.timedelta(
hours=3
)
headers = {
"Content-Type": "application/json; charset=utf-8",
"RequestVerificationToken": self._request_token,
Expand All @@ -117,6 +130,7 @@ async def _get_sc_web_token(self) -> str:
raise InvalidLogin()
sc_regex = re.compile(r"NAME='ScWebToken' value='(\S+.\S+.\S+)'", re.IGNORECASE)
sc_data = sc_regex.search(connection["data"]["html"])
self._sc_expiry = datetime.datetime.now() + datetime.timedelta(hours=3)
if sc_data and sc_data.group(1):
if "'>" in sc_data.group(1):
return sc_data.group(1).split("'>")[0]
Expand All @@ -126,7 +140,8 @@ async def _get_sc_web_token(self) -> str:
raise NoScTokenFound("Login request did not return a sc token")

async def _get_southern_jwt_cookie(self) -> str:
if self._sc is None:
# update to use property
if self._sc is None or datetime.datetime.now() >= self._sc_expiry:
self._sc = await self._get_sc_web_token()
data = {"ScWebToken": self._sc}
async with aiohttp.ClientSession() as session:
Expand Down Expand Up @@ -210,7 +225,7 @@ async def get_jwt(self) -> str:

async def get_accounts(self) -> List[Account]:
if await self.jwt is None:
raise CantReachSouthernCompany()
raise CantReachSouthernCompany("Can't get jwt. Expired and not refreshed")
headers = {"Authorization": f"bearer {self._jwt}"}
async with aiohttp.ClientSession() as session:
async with session.get(
Expand Down
2 changes: 2 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# type: ignore
# For get_accounts jwt mock. looking for better solution.
import datetime
import typing
from unittest.mock import patch

Expand Down Expand Up @@ -103,6 +104,7 @@ async def test_ga_power_get_jwt_cookie():
)
sca = SouthernCompanyAPI("", "")
sca._sc = ""
sca._sc_expiry = datetime.datetime.now() + datetime.timedelta(hours=3)
token = await sca._get_southern_jwt_cookie()
assert token == "sample_cookie"

Expand Down

0 comments on commit bea0e8c

Please sign in to comment.