Compare commits

...

2 Commits

Author SHA1 Message Date
33454f68b8 Encode username/password 2023-04-12 02:10:37 +02:00
6b2c60d552 Fix session issues 2023-04-12 01:07:33 +02:00
3 changed files with 15 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import secrets
import urllib import urllib
from pprint import pformat from pprint import pformat
from urllib import parse from urllib import parse
from urllib.parse import quote
from yarl import URL from yarl import URL
@ -86,7 +87,9 @@ class HonAuth:
): ):
await self._error_logger(redirect2) await self._error_logger(redirect2)
return False return False
async with self._session.get(URL(url, encoded=True)) as login_screen: async with self._session.get(
URL(url, encoded=True), headers={"user-agent": const.USER_AGENT}
) as login_screen:
self._called_urls.append( self._called_urls.append(
(login_screen.status, login_screen.request_info.url) (login_screen.status, login_screen.request_info.url)
) )
@ -111,8 +114,8 @@ class HonAuth:
"descriptor": "apex://LightningLoginCustomController/ACTION$login", "descriptor": "apex://LightningLoginCustomController/ACTION$login",
"callingDescriptor": "markup://c:loginForm", "callingDescriptor": "markup://c:loginForm",
"params": { "params": {
"username": self._email, "username": quote(self._email),
"password": self._password, "password": quote(self._password),
"startUrl": parse.unquote( "startUrl": parse.unquote(
login_url.split("startURL=")[-1] login_url.split("startURL=")[-1]
).split("%3D")[0], ).split("%3D")[0],

View File

@ -13,6 +13,7 @@ class HonBaseConnectionHandler:
_HEADERS = {"user-agent": const.USER_AGENT, "Content-Type": "application/json"} _HEADERS = {"user-agent": const.USER_AGENT, "Content-Type": "application/json"}
def __init__(self, session=None): def __init__(self, session=None):
self._create_session = session is None
self._session = session self._session = session
self._auth = None self._auth = None
@ -23,7 +24,8 @@ class HonBaseConnectionHandler:
await self.close() await self.close()
async def create(self): async def create(self):
self._session = aiohttp.ClientSession(headers=self._HEADERS) if self._create_session:
self._session = aiohttp.ClientSession()
return self return self
@asynccontextmanager @asynccontextmanager
@ -41,6 +43,7 @@ class HonBaseConnectionHandler:
yield response yield response
async def close(self): async def close(self):
if self._create_session:
await self._session.close() await self._session.close()
@ -75,7 +78,7 @@ class HonConnectionHandler(HonBaseConnectionHandler):
self._request_headers["id-token"] = self._auth.id_token self._request_headers["id-token"] = self._auth.id_token
else: else:
raise HonAuthenticationError("Can't login") raise HonAuthenticationError("Can't login")
return headers | self._request_headers return self._HEADERS | headers | self._request_headers
@asynccontextmanager @asynccontextmanager
async def _intercept(self, method, *args, loop=0, **kwargs): async def _intercept(self, method, *args, loop=0, **kwargs):
@ -95,6 +98,8 @@ class HonConnectionHandler(HonBaseConnectionHandler):
response.status, response.status,
await response.text(), await response.text(),
) )
self._request_headers = {}
self._session.cookie_jar.clear_domain(const.AUTH_API.split("/")[-2])
await self.create() await self.create()
async with self._intercept( async with self._intercept(
method, *args, loop=loop + 1, **kwargs method, *args, loop=loop + 1, **kwargs

View File

@ -7,7 +7,7 @@ with open("README.md", "r") as f:
setup( setup(
name="pyhOn", name="pyhOn",
version="0.7.0", version="0.7.2",
author="Andre Basche", author="Andre Basche",
description="Control hOn devices with python", description="Control hOn devices with python",
long_description=long_description, long_description=long_description,