Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renew Cognito credentials on MQTT reconnection #37

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions pysesame3/chsesame2.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ def _iot_shadow_callback(self, client, userdata, message) -> None: # type: igno
# TODO: handle exceptions correctly
pass

def _iot_on_offline(self) -> None:
"""Callback that gets called when the AWS IoT client is offline.

This is intended to update the credentials before reconnecting.
"""
if not isinstance(self.authenticator, CognitoAuth):
raise NotImplementedError("This feature is not suppoted by the Web API.")
if self._iot_client is None:
raise RuntimeError("subscribeMechStatus is not called yet.")

(access_key_id, secret_key, session_token) = self.authenticator.authenticate()

self._iot_client.configureIAMCredentials(
access_key_id, secret_key, session_token
)

def subscribeMechStatus(
self,
callback: Optional[Callable[["CHSesame2", CHSesame2MechStatus], None]] = None,
Expand Down Expand Up @@ -137,6 +153,7 @@ def subscribeMechStatus(
self._iot_client.configureIAMCredentials(
access_key_id, secret_key, session_token
)
self._iot_client.onOffline = self._iot_on_offline
self._iot_client.connect()
self._iot_client.subscribe(
"$aws/things/sesame2/shadow/name/{}/update/accepted".format(
Expand Down