2023-02-12 17:41:38 -07:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import secrets
|
|
|
|
import urllib
|
2023-04-09 22:42:40 -06:00
|
|
|
from pprint import pformat
|
2023-02-12 17:41:38 -07:00
|
|
|
from urllib import parse
|
|
|
|
|
2023-03-16 18:56:04 -06:00
|
|
|
from yarl import URL
|
2023-02-12 17:41:38 -07:00
|
|
|
|
2023-02-12 19:36:09 -07:00
|
|
|
from pyhon import const
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
class HonAuth:
|
2023-04-09 10:13:50 -06:00
|
|
|
def __init__(self, session, email, password, device) -> None:
|
|
|
|
self._session = session
|
|
|
|
self._email = email
|
|
|
|
self._password = password
|
2023-03-16 18:56:04 -06:00
|
|
|
self._access_token = ""
|
|
|
|
self._refresh_token = ""
|
2023-02-12 17:41:38 -07:00
|
|
|
self._cognito_token = ""
|
|
|
|
self._id_token = ""
|
2023-04-09 10:13:50 -06:00
|
|
|
self._device = device
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def cognito_token(self):
|
|
|
|
return self._cognito_token
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id_token(self):
|
|
|
|
return self._id_token
|
|
|
|
|
2023-03-16 18:56:04 -06:00
|
|
|
@property
|
|
|
|
def access_token(self):
|
|
|
|
return self._access_token
|
|
|
|
|
|
|
|
@property
|
|
|
|
def refresh_token(self):
|
|
|
|
return self._refresh_token
|
|
|
|
|
2023-04-09 10:13:50 -06:00
|
|
|
async def _load_login(self):
|
2023-03-16 18:56:04 -06:00
|
|
|
nonce = secrets.token_hex(16)
|
|
|
|
nonce = f"{nonce[:8]}-{nonce[8:12]}-{nonce[12:16]}-{nonce[16:20]}-{nonce[20:]}"
|
|
|
|
params = {
|
|
|
|
"response_type": "token+id_token",
|
|
|
|
"client_id": const.CLIENT_ID,
|
2023-04-09 12:55:36 -06:00
|
|
|
"redirect_uri": urllib.parse.quote(
|
|
|
|
f"{const.APP}://mobilesdk/detect/oauth/done"
|
|
|
|
),
|
2023-03-16 18:56:04 -06:00
|
|
|
"display": "touch",
|
|
|
|
"scope": "api openid refresh_token web",
|
2023-04-09 12:55:36 -06:00
|
|
|
"nonce": nonce,
|
2023-03-16 18:56:04 -06:00
|
|
|
}
|
|
|
|
params = "&".join([f"{k}={v}" for k, v in params.items()])
|
2023-04-09 12:55:36 -06:00
|
|
|
async with self._session.get(
|
|
|
|
f"{const.AUTH_API}/services/oauth2/authorize/expid_Login?{params}"
|
|
|
|
) as resp:
|
2023-03-16 18:56:04 -06:00
|
|
|
if not (login_url := re.findall("url = '(.+?)'", await resp.text())):
|
|
|
|
return False
|
2023-04-09 10:13:50 -06:00
|
|
|
async with self._session.get(login_url[0], allow_redirects=False) as redirect1:
|
2023-03-16 18:56:04 -06:00
|
|
|
if not (url := redirect1.headers.get("Location")):
|
|
|
|
return False
|
2023-04-09 10:13:50 -06:00
|
|
|
async with self._session.get(url, allow_redirects=False) as redirect2:
|
2023-04-09 12:55:36 -06:00
|
|
|
if not (
|
|
|
|
url := redirect2.headers.get("Location")
|
|
|
|
+ "&System=IoT_Mobile_App&RegistrationSubChannel=hOn"
|
|
|
|
):
|
2023-03-16 18:56:04 -06:00
|
|
|
return False
|
2023-04-09 10:13:50 -06:00
|
|
|
async with self._session.get(URL(url, encoded=True)) as login_screen:
|
2023-04-09 12:55:36 -06:00
|
|
|
if context := re.findall(
|
|
|
|
'"fwuid":"(.*?)","loaded":(\\{.*?})', await login_screen.text()
|
|
|
|
):
|
2023-03-16 18:56:04 -06:00
|
|
|
fw_uid, loaded_str = context[0]
|
|
|
|
loaded = json.loads(loaded_str)
|
2023-04-09 12:55:36 -06:00
|
|
|
login_url = login_url[0].replace(
|
|
|
|
"/".join(const.AUTH_API.split("/")[:-1]), ""
|
|
|
|
)
|
2023-03-16 18:56:04 -06:00
|
|
|
return fw_uid, loaded, login_url
|
|
|
|
return False
|
|
|
|
|
2023-04-09 10:13:50 -06:00
|
|
|
async def _login(self, fw_uid, loaded, login_url):
|
2023-02-12 17:41:38 -07:00
|
|
|
data = {
|
|
|
|
"message": {
|
|
|
|
"actions": [
|
|
|
|
{
|
|
|
|
"id": "79;a",
|
|
|
|
"descriptor": "apex://LightningLoginCustomController/ACTION$login",
|
|
|
|
"callingDescriptor": "markup://c:loginForm",
|
|
|
|
"params": {
|
2023-04-09 10:13:50 -06:00
|
|
|
"username": self._email,
|
|
|
|
"password": self._password,
|
2023-04-09 12:55:36 -06:00
|
|
|
"startUrl": parse.unquote(
|
|
|
|
login_url.split("startURL=")[-1]
|
|
|
|
).split("%3D")[0],
|
|
|
|
},
|
2023-02-12 17:41:38 -07:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"aura.context": {
|
|
|
|
"mode": "PROD",
|
2023-03-16 18:56:04 -06:00
|
|
|
"fwuid": fw_uid,
|
2023-02-12 17:41:38 -07:00
|
|
|
"app": "siteforce:loginApp2",
|
2023-03-16 18:56:04 -06:00
|
|
|
"loaded": loaded,
|
2023-02-12 17:41:38 -07:00
|
|
|
"dn": [],
|
|
|
|
"globals": {},
|
2023-04-09 12:55:36 -06:00
|
|
|
"uad": False,
|
|
|
|
},
|
2023-03-16 18:56:04 -06:00
|
|
|
"aura.pageURI": login_url,
|
2023-04-09 12:55:36 -06:00
|
|
|
"aura.token": None,
|
|
|
|
}
|
2023-02-12 17:41:38 -07:00
|
|
|
params = {"r": 3, "other.LightningLoginCustom.login": 1}
|
2023-04-09 10:13:50 -06:00
|
|
|
async with self._session.post(
|
2023-04-09 12:55:36 -06:00
|
|
|
const.AUTH_API + "/s/sfsites/aura",
|
|
|
|
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
|
|
data="&".join(f"{k}={json.dumps(v)}" for k, v in data.items()),
|
|
|
|
params=params,
|
2023-02-12 17:41:38 -07:00
|
|
|
) as response:
|
2023-03-16 18:56:04 -06:00
|
|
|
if response.status == 200:
|
|
|
|
try:
|
2023-04-09 22:42:40 -06:00
|
|
|
data = await response.json()
|
|
|
|
return data["events"][0]["attributes"]["values"]["url"]
|
2023-03-16 18:56:04 -06:00
|
|
|
except json.JSONDecodeError:
|
|
|
|
pass
|
2023-04-09 22:42:40 -06:00
|
|
|
except KeyError:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Can't get login url - %s", pformat(await response.json())
|
|
|
|
)
|
2023-04-09 12:55:36 -06:00
|
|
|
_LOGGER.error(
|
|
|
|
"Unable to login: %s\n%s", response.status, await response.text()
|
|
|
|
)
|
2023-03-16 18:56:04 -06:00
|
|
|
return ""
|
2023-02-12 17:41:38 -07:00
|
|
|
|
2023-04-09 10:13:50 -06:00
|
|
|
async def _get_token(self, url):
|
|
|
|
async with self._session.get(url) as resp:
|
2023-02-12 17:41:38 -07:00
|
|
|
if resp.status != 200:
|
2023-03-16 18:56:04 -06:00
|
|
|
_LOGGER.error("Unable to get token: %s", resp.status)
|
2023-02-12 17:41:38 -07:00
|
|
|
return False
|
2023-04-09 22:42:40 -06:00
|
|
|
url = re.findall("href\\s*=\\s*[\"'](http.+?)[\"']", await resp.text())
|
|
|
|
if not url:
|
|
|
|
_LOGGER.error("Can't get login url - \n%s", await resp.text())
|
|
|
|
raise PermissionError
|
2023-04-09 10:13:50 -06:00
|
|
|
async with self._session.get(url[0]) as resp:
|
2023-03-16 18:56:04 -06:00
|
|
|
if resp.status != 200:
|
|
|
|
_LOGGER.error("Unable to get token: %s", resp.status)
|
|
|
|
return False
|
|
|
|
url = re.findall("href\\s*=\\s*[\"'](.*?)[\"']", await resp.text())
|
|
|
|
url = "/".join(const.AUTH_API.split("/")[:-1]) + url[0]
|
2023-04-09 10:13:50 -06:00
|
|
|
async with self._session.get(url) as resp:
|
2023-02-12 17:41:38 -07:00
|
|
|
if resp.status != 200:
|
|
|
|
_LOGGER.error("Unable to connect to the login service: %s", resp.status)
|
|
|
|
return False
|
2023-03-16 18:56:04 -06:00
|
|
|
text = await resp.text()
|
|
|
|
if access_token := re.findall("access_token=(.*?)&", text):
|
|
|
|
self._access_token = access_token[0]
|
|
|
|
if refresh_token := re.findall("refresh_token=(.*?)&", text):
|
|
|
|
self._refresh_token = refresh_token[0]
|
|
|
|
if id_token := re.findall("id_token=(.*?)&", text):
|
|
|
|
self._id_token = id_token[0]
|
2023-02-12 17:41:38 -07:00
|
|
|
return True
|
|
|
|
|
2023-04-09 10:13:50 -06:00
|
|
|
async def authorize(self):
|
|
|
|
if login_site := await self._load_login():
|
|
|
|
fw_uid, loaded, login_url = login_site
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
if not (url := await self._login(fw_uid, loaded, login_url)):
|
|
|
|
return False
|
|
|
|
if not await self._get_token(url):
|
|
|
|
return False
|
2023-02-12 17:41:38 -07:00
|
|
|
|
2023-04-09 10:43:57 -06:00
|
|
|
post_headers = {"id-token": self._id_token}
|
2023-04-09 10:13:50 -06:00
|
|
|
data = self._device.get()
|
2023-04-09 12:55:36 -06:00
|
|
|
async with self._session.post(
|
|
|
|
f"{const.API_URL}/auth/v1/login", headers=post_headers, json=data
|
|
|
|
) as resp:
|
2023-04-09 10:13:50 -06:00
|
|
|
try:
|
|
|
|
json_data = await resp.json()
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
_LOGGER.error("No JSON Data after POST: %s", await resp.text())
|
|
|
|
return False
|
|
|
|
self._cognito_token = json_data["cognitoUser"]["Token"]
|
2023-02-12 17:41:38 -07:00
|
|
|
return True
|
2023-04-09 10:43:57 -06:00
|
|
|
|
|
|
|
async def refresh(self):
|
|
|
|
params = {
|
|
|
|
"client_id": const.CLIENT_ID,
|
|
|
|
"refresh_token": self._refresh_token,
|
2023-04-09 12:55:36 -06:00
|
|
|
"grant_type": "refresh_token",
|
2023-04-09 10:43:57 -06:00
|
|
|
}
|
2023-04-09 12:55:36 -06:00
|
|
|
async with self._session.post(
|
|
|
|
f"{const.AUTH_API}/services/oauth2/token", params=params
|
|
|
|
) as resp:
|
2023-04-09 10:43:57 -06:00
|
|
|
if resp.status >= 400:
|
|
|
|
return False
|
|
|
|
data = await resp.json()
|
2023-04-09 22:42:40 -06:00
|
|
|
self._id_token = data["id_token"]
|
|
|
|
self._access_token = data["access_token"]
|
|
|
|
return True
|