Skip to content

Commit

Permalink
fix: add oauth token refresh
Browse files Browse the repository at this point in the history
This should avoid expiration of cookies since we refresh it every sixty
minutes. Please force logout and relogin to enable oauth refresh.
closes #1005
  • Loading branch information
alandtse committed Dec 31, 2020
1 parent e9ee26f commit 77e9d6c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
41 changes: 29 additions & 12 deletions custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
CONF_DEBUG,
CONF_EXCLUDE_DEVICES,
CONF_INCLUDE_DEVICES,
CONF_OAUTH,
CONF_OTPSECRET,
CONF_QUEUE_DELAY,
DATA_ALEXAMEDIA,
Expand Down Expand Up @@ -135,6 +136,7 @@ async def async_setup(hass, config, discovery_info=None):
CONF_SCAN_INTERVAL: account[
CONF_SCAN_INTERVAL
].total_seconds(),
CONF_OAUTH: account.get("CONF_OAUTH", {}),
CONF_OTPSECRET: account.get(CONF_OTPSECRET, ""),
},
)
Expand All @@ -154,6 +156,7 @@ async def async_setup(hass, config, discovery_info=None):
CONF_INCLUDE_DEVICES: account[CONF_INCLUDE_DEVICES],
CONF_EXCLUDE_DEVICES: account[CONF_EXCLUDE_DEVICES],
CONF_SCAN_INTERVAL: account[CONF_SCAN_INTERVAL].total_seconds(),
CONF_OAUTH: account.get("CONF_OAUTH", {}),
CONF_OTPSECRET: account.get(CONF_OTPSECRET, ""),
},
)
Expand Down Expand Up @@ -182,12 +185,13 @@ async def relogin(event=None) -> None:
].get("login_obj")
if login_obj is None:
login_obj = AlexaLogin(
url,
email,
password,
hass.config.path,
account.get(CONF_DEBUG),
account.get(CONF_OTPSECRET, ""),
url=url,
email=email,
password=password,
outputpath=hass.config.path,
debug=account.get(CONF_DEBUG),
otp_secret=account.get(CONF_OTPSECRET, ""),
oauth=account.get(CONF_OAUTH, {}),
)
hass.data[DATA_ALEXAMEDIA]["accounts"][email][
"login_obj"
Expand Down Expand Up @@ -248,12 +252,13 @@ async def login_success(event=None) -> None:
login: AlexaLogin = hass.data[DATA_ALEXAMEDIA]["accounts"][email].get(
"login_obj",
AlexaLogin(
url,
email,
password,
hass.config.path,
account.get(CONF_DEBUG),
account.get(CONF_OTPSECRET, ""),
url=url,
email=email,
password=password,
outputpath=hass.config.path,
debug=account.get(CONF_DEBUG),
otp_secret=account.get(CONF_OTPSECRET, ""),
oauth=account.get(CONF_OAUTH, {}),
),
)
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["login_obj"] = login
Expand Down Expand Up @@ -475,6 +480,18 @@ async def async_update_data():

hass.data[DATA_ALEXAMEDIA]["accounts"][email]["new_devices"] = False
await login_obj.save_cookiefile()
if login_obj.access_token:
hass.config_entries.async_update_entry(
config_entry,
data={
**config_entry.data,
CONF_OAUTH: {
"access_token": login_obj.access_token,
"refresh_token": login_obj.refresh_token,
"expires_in": login_obj.expires_in,
},
},
)

@_catch_login_errors
async def process_notifications(login_obj, raw_notifications=None):
Expand Down
6 changes: 6 additions & 0 deletions custom_components/alexa_media/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
CONF_INCLUDE_DEVICES,
CONF_QUEUE_DELAY,
CONF_SECURITYCODE,
CONF_OAUTH,
CONF_OTPSECRET,
CONF_TOTP_REGISTER,
DATA_ALEXAMEDIA,
Expand Down Expand Up @@ -359,6 +360,11 @@ async def _test_login(self):
self.config.pop("reauth")
if self.config.get(CONF_SECURITYCODE):
self.config.pop(CONF_SECURITYCODE)
self.config[CONF_OAUTH] = {
"access_token": login.access_token,
"refresh_token": login.refresh_token,
"expires_in": login.expires_in,
}
if existing_entry:
self.hass.config_entries.async_update_entry(
existing_entry, data=self.config
Expand Down
1 change: 1 addition & 0 deletions custom_components/alexa_media/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
CONF_SECURITYCODE = "securitycode"
CONF_OTPSECRET = "otp_secret"
CONF_TOTP_REGISTER = "registered"
CONF_OAUTH = "oauth"
DATA_LISTENER = "listener"

EXCEPTION_TEMPLATE = "An exception of type {0} occurred. Arguments:\n{1!r}"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/alexa_media/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"issue_tracker": "https://github.com/custom-components/alexa_media_player/issues",
"dependencies": ["persistent_notification"],
"codeowners": ["@keatontaylor", "@alandtse"],
"requirements": ["alexapy==1.17.2", "packaging~=20.3", "wrapt~=1.12.1"]
"requirements": ["alexapy==1.18.0", "packaging~=20.3", "wrapt~=1.12.1"]
}

0 comments on commit 77e9d6c

Please sign in to comment.