Skip to content

Commit

Permalink
Implement re-authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
jampez77 committed Oct 4, 2023
1 parent fe40489 commit 21a6c49
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 37 deletions.
40 changes: 26 additions & 14 deletions custom_components/ryanair/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,17 @@ async def async_step_mfa(
# if data is not null and contains MFA TOKEN then initiate MFA capture
if CUSTOMER_ID in info["data"]:
users = load_json_object(CREDENTIALS)
ryanairData = {
CONF_DEVICE_FINGERPRINT: user_input[CONF_DEVICE_FINGERPRINT],
CUSTOMER_ID: info["data"][CUSTOMER_ID],
TOKEN: info["data"][TOKEN],
}
users[user_input[CONF_DEVICE_FINGERPRINT]] = ryanairData

users[user_input[CONF_DEVICE_FINGERPRINT]
][CONF_DEVICE_FINGERPRINT] = user_input[CONF_DEVICE_FINGERPRINT]
users[user_input[CONF_DEVICE_FINGERPRINT]
][CUSTOMER_ID] = info["data"][CUSTOMER_ID]
users[user_input[CONF_DEVICE_FINGERPRINT]
][TOKEN] = info["data"][TOKEN]

save_json(CREDENTIALS, users)
return self.async_create_entry(
title=info["title"], data=ryanairData
title=info["title"], data=users[user_input[CONF_DEVICE_FINGERPRINT]]
)

return self.async_show_form(
Expand Down Expand Up @@ -187,6 +189,14 @@ async def async_step_user(

user_input[CONF_DEVICE_FINGERPRINT] = self._fingerprint

users = load_json_object(CREDENTIALS)
ryanairData = {
CONF_EMAIL: user_input[CONF_EMAIL],
CONF_PASSWORD: user_input[CONF_PASSWORD],
CONF_DEVICE_FINGERPRINT: user_input[CONF_DEVICE_FINGERPRINT],
}
users[user_input[CONF_DEVICE_FINGERPRINT]] = ryanairData
save_json(CREDENTIALS, users)
try:
info = await validate_input(self.hass, user_input)
except CannotConnect:
Expand All @@ -211,15 +221,17 @@ async def async_step_user(
)
if CUSTOMER_ID in info["data"]:
users = load_json_object(CREDENTIALS)
ryanairData = {
CONF_DEVICE_FINGERPRINT: user_input[CONF_DEVICE_FINGERPRINT],
CUSTOMER_ID: info["data"][CUSTOMER_ID],
TOKEN: info["data"][TOKEN],
}
users[user_input[CONF_DEVICE_FINGERPRINT]] = ryanairData

users[user_input[CONF_DEVICE_FINGERPRINT]
][CONF_DEVICE_FINGERPRINT] = user_input[CONF_DEVICE_FINGERPRINT]
users[user_input[CONF_DEVICE_FINGERPRINT]
][CUSTOMER_ID] = info["data"][CUSTOMER_ID]
users[user_input[CONF_DEVICE_FINGERPRINT]
][TOKEN] = info["data"][TOKEN]

save_json(CREDENTIALS, users)
return self.async_create_entry(
title=info["title"], data=ryanairData
title=info["title"], data=users[user_input[CONF_DEVICE_FINGERPRINT]]
)

return self.async_show_form(
Expand Down
1 change: 1 addition & 0 deletions custom_components/ryanair/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
BOARDING_PASS_URL = "https://mntappbp.ryanair.com/v1/boardingpass"
BOOKING_DETAILS_URL = "https://nativeapps.ryanair.com/booking/rest/en-IE/query/getbookingbybookingid"
EMAIL = "Email"
PASSWORD = "password"
RECORD_LOCATOR = "RecordLocator"
USER_PROFILE = "usrprof/"
ORDERS = "orders/"
Expand Down
56 changes: 34 additions & 22 deletions custom_components/ryanair/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,21 @@ async def rememberMeToken(self, userData):
},
)
rememberMeTokenResponse = await rememberMeTokenResp.json()
print(rememberMeTokenResponse)

users = load_json_object(CREDENTIALS)

users[userData[CONF_DEVICE_FINGERPRINT]
][X_REMEMBER_ME_TOKEN] = rememberMeTokenResponse[TOKEN]
if rememberMeTokenResponse is not None and ((ACCESS_DENIED in rememberMeTokenResponse and rememberMeTokenResponse[CAUSE] == NOT_AUTHENTICATED) or (
TYPE in rememberMeTokenResponse and rememberMeTokenResponse[TYPE] == CLIENT_ERROR
)):
authResponse = await authenticateUser(self, userData)

users[userData[CONF_DEVICE_FINGERPRINT]
][TOKEN] = authResponse[TOKEN]
users[userData[CONF_DEVICE_FINGERPRINT]
][CUSTOMER_ID] = authResponse[CUSTOMER_ID]
else:
users[userData[CONF_DEVICE_FINGERPRINT]
][X_REMEMBER_ME_TOKEN] = rememberMeTokenResponse[TOKEN]

save_json(CREDENTIALS, users)

Expand Down Expand Up @@ -177,6 +187,24 @@ async def getBookingDetails(self, data, bookingInfo):
return body


async def authenticateUser(self, userData):
resp = await self.session.request(
method="POST",
url=USER_PROFILE_URL + ACCOUNT_LOGIN,
headers={
"Content-Type": CONTENT_TYPE_JSON,
CONF_DEVICE_FINGERPRINT: userData[CONF_DEVICE_FINGERPRINT],
},
json={
CONF_EMAIL: userData[CONF_EMAIL],
CONF_PASSWORD: userData[CONF_PASSWORD],
CONF_POLICY_AGREED: "true",
},
)
body = await resp.json()
return body


class RyanairBookingDetailsCoordinator(DataUpdateCoordinator):
"""Booking Details Coordinator"""

Expand Down Expand Up @@ -488,7 +516,7 @@ async def _async_update_data(self):
class RyanairCoordinator(DataUpdateCoordinator):
"""Data coordinator."""

def __init__(self, hass: HomeAssistant, session, data) -> None:
def __init__(self, hass: HomeAssistant, session, userData) -> None:
"""Initialize coordinator."""

super().__init__(
Expand All @@ -501,9 +529,7 @@ def __init__(self, hass: HomeAssistant, session, data) -> None:
)

self.session = session
self.email = data[CONF_EMAIL]
self.password = data[CONF_PASSWORD]
self.fingerprint = data[CONF_DEVICE_FINGERPRINT]
self.userData = userData

async def _async_update_data(self):
"""Fetch data from API endpoint.
Expand All @@ -512,21 +538,7 @@ async def _async_update_data(self):
so entities can quickly look up their data.
"""
try:
resp = await self.session.request(
method="POST",
url=USER_PROFILE_URL + ACCOUNT_LOGIN,
headers={
"Content-Type": CONTENT_TYPE_JSON,
CONF_DEVICE_FINGERPRINT: self.fingerprint,
},
json={
CONF_EMAIL: self.email,
CONF_PASSWORD: self.password,
CONF_POLICY_AGREED: "true",
},
)
body = await resp.json()

body = await authenticateUser(self, self.userData)
except InvalidAuth as err:
raise ConfigEntryAuthFailed from err
except RyanairError as err:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ryanair/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"issue_tracker": "https://github.com/jampez77/Ryanair/issues",
"requirements": ["aztec-code-generator==0.11"],
"ssdp": [],
"version": "2023.10.0",
"version": "2023.10.1",
"zeroconf": []
}

0 comments on commit 21a6c49

Please sign in to comment.