Skip to content

Commit

Permalink
Added: Proper Refresh Token handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
basrieter committed Oct 16, 2023
1 parent bf32728 commit 789698f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 61 deletions.
19 changes: 2 additions & 17 deletions channels/channel.se/tv4se/chn_tv4se.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,8 @@
"settings": [
{
"order": 1,
"id": "channel_tv4play_se_username",
"value": "id=\"channel_tv4play_se_username\" type=\"text\" label=\"30094\" default=\"\""
},
{
"order": 2,
"id": "channel_tv4play_se_password_set",
"value": "id=\"channel_tv4play_se_password_set\" label=\"30093\" type=\"action\" action=\"RunScript(plugin.video.retrospect, 0, ?action=encryptsetting&settingid=channel_tv4play_se_password&settingname=TV4Play.se password&tabfocus=102)\" option=\"close\""
},
{
"order": 3,
"id": "channel_tv4play_se_password",
"value": "id=\"channel_tv4play_se_password\" default=\"\""
},
{
"order": 4,
"id": "cookie",
"value": "id=\"channel_tv4play_se_token\" default=\"\""
"id": "set_refresh_token",
"value": "id=\"set_refresh_token\" label=\"30113\" type=\"action\" action=\"RunScript(plugin.video.retrospect, 0, ?channel=channel.se.tv4se&channelcode=tv4segroup&action=execute&command=fetch_token)\" option=\"close\""
}
]
}
61 changes: 35 additions & 26 deletions channels/channel.se/tv4se/chn_tv4se.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,51 +114,60 @@ def __init__(self, channel_info):
# self.__maxPageSize = 100 # The Android app uses a page size of 20
self.__program_fields = '{__typename,description,displayCategory,id,image,images{main16x9},name,nid,genres,videoPanels{id}}'
# self.__season_count_meta = "season_count"
self.__refresh_token_setting_id = "refresh_token"

# ===============================================================================================================
# Test cases:

# ====================================== Actual channel setup STOPS here =======================================
return

# No logon for now
def log_on(self):
""" Makes sure that we are logged on. """

if self.__access_token:
return True

username = AddonSettings.get_setting("channel_tv4play_se_username")
if not username:
Logger.info("No user name for TV4 Play, not logging in")
return False

# Fetch an existing token
refresh_token_setting_id = "refresh_token"
token = AddonSettings.get_channel_setting(self, refresh_token_setting_id, store=LOCAL)
def fetch_token(self) -> Optional[str]:
wd = WebDialogue()
token, cancelled = wd.input(LanguageHelper.SetRefreshToken, LanguageHelper.PasteRefreshToken, time_out=120)

if not token:
# Read the refresh token from a webpage
wd = WebDialogue()
token, cancelled = wd.input("Refresh Token", "Paste refresh token here.", time_out=60)

if not token or cancelled:
return False
if not token or cancelled:
return None

AddonSettings.set_channel_setting(self, refresh_token_setting_id, token, store=LOCAL)
split_data = token.split(".")
if len(split_data) != 3:
AddonSettings.set_channel_setting(self, self.__refresh_token_setting_id, "", store=LOCAL)
XbmcWrapper.show_notification(LanguageHelper.InvalidRefreshToken, LanguageHelper.InvalidRefreshToken)
# Retry
return self.fetch_token()

header, payload, signature = token.split(".")
header, payload, signature = split_data
payload_data = EncodingHelper.decode_base64(payload + '=' * (-len(payload) % 4))
payload = JsonHelper(payload_data)
expires_at = payload.get_value("exp")
expire_date = DateHelper.get_date_from_posix(float(expires_at), tz=pytz.UTC)
if expire_date < datetime.datetime.now(tz=pytz.UTC).astimezone(tz=pytz.UTC):
Logger.info("Found expired TV4Play token (valid until: %s)", expire_date)
AddonSettings.set_setting(refresh_token_setting_id, "", store=LOCAL)
AddonSettings.set_channel_setting(self, self.__refresh_token_setting_id, "", store=LOCAL)
XbmcWrapper.show_notification(LanguageHelper.InvalidRefreshToken, LanguageHelper.ExpireRefreshToken)
# Retry
return self.log_on()
return self.fetch_token()

# (Re)Store the valid token.
Logger.info("Found existing valid TV4Play token (valid until: %s)", expire_date)
AddonSettings.set_channel_setting(self, self.__refresh_token_setting_id, token, store=LOCAL)
return token

# No logon for now
def log_on(self):
""" Makes sure that we are logged on. """

if self.__access_token:
return True

# Fetch an existing token
token: str = AddonSettings.get_channel_setting(self, self.__refresh_token_setting_id, store=LOCAL)
if not token:
token = self.fetch_token()

if not token:
return False

url = "https://avod-auth-alb.a2d.tv/oauth/refresh"
result = UriHandler.open(
url, json={"refresh_token": token, "client_id":"tv4-web"}, no_cache=True)
Expand Down
18 changes: 17 additions & 1 deletion resources/language/resource.language.en_gb/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,24 @@ msgctxt "#30120"
msgid "Browser input cancelled"
msgstr ""

msgctxt "#30121"
msgid "Paste refresh token here."
msgstr ""

msgctxt "#30122"
msgid "Invalid Refresh Token"
msgstr ""

msgctxt "#30123"
msgid "Expired Refresh Token"
msgstr ""

msgctxt "#30124"
msgid "Find the <b>refresh token</b> for your account by examining the site's cookies and paste the <b>value</b> of the refresh token cookie here."
msgstr ""

# used for list limits
# empty strings from id 30113 to 30189
# empty strings from id 30125 to 30189

msgctxt "#30190"
msgid "Monday"
Expand Down
5 changes: 5 additions & 0 deletions resources/lib/helpers/languagehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class LanguageHelper(object):
UpdateToNewKodi = 30045
YouTubeMissing = 30047

PasteRefreshToken = 30121
InvalidRefreshToken = 30122
ExpireRefreshToken = 30123
SetRefreshToken = 30124

Monday = 30190
Tuesday = 30191
Wednesday = 30192
Expand Down
31 changes: 14 additions & 17 deletions resources/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,24 @@
<setting id="channel_3AF9C61E-88B2-11E1-A03F-08F6BAE59B18_show_programs_folder" label="30582" type="bool" default="true" visible="eq(-11,SVT Play)" />
<setting id="channel_3AF9C61E-88B2-11E1-A03F-08F6BAE59B18_show_accessibility_videos" label="30587" type="bool" default="true" visible="eq(-12,SVT Play)" />
<!-- chn_tv4se.py -->
<setting id="channel_tv4play_se_username" type="text" label="30094" default="" visible="eq(-13,TV4 Play)" />
<setting id="channel_tv4play_se_password_set" label="30093" type="action" action="RunScript(plugin.video.retrospect, 0, ?action=encryptsetting&amp;settingid=channel_tv4play_se_password&amp;settingname=TV4Play.se password&amp;tabfocus=102)" option="close" visible="eq(-14,TV4 Play)" />
<setting id="channel_tv4play_se_password" default="" visible="eq(-15,TV4 Play)" />
<setting id="channel_tv4play_se_token" default="" visible="eq(-16,TV4 Play)" />
<setting id="set_refresh_token" label="30113" type="action" action="RunScript(plugin.video.retrospect, 0, ?channel=channel.se.tv4se&amp;channelcode=tv4segroup&amp;action=execute&amp;command=fetch_token)" option="close" visible="eq(-13,TV4 Play)" />
<!-- chn_urplay.py -->
<setting id="channel_9F0F57D6-B897-11E0-AF2E-92314924019B_mainlist_pages" type="number" label="30586" default="25" visible="eq(-17,UR Play)" />
<setting id="channel_9F0F57D6-B897-11E0-AF2E-92314924019B_mainlist_pages" type="number" label="30586" default="25" visible="eq(-14,UR Play)" />
<!-- chn_vier.py -->
<setting id="viervijfzes_username" type="text" label="30094" default="" visible="eq(-18,Zes)|eq(-18,Vijf)|eq(-18,Vier)" />
<setting id="viervijfzes_password_set" label="30093" type="action" action="RunScript(plugin.video.retrospect, 0, ?action=encryptsetting&amp;settingid=viervijfzes_password&amp;settingname=Vier/Vijf/Zes.be password&amp;tabfocus=102)" option="close" visible="eq(-19,Zes)|eq(-19,Vijf)|eq(-19,Vier)" />
<setting id="viervijfzes_password" default="" visible="eq(-20,Zes)|eq(-20,Vijf)|eq(-20,Vier)" />
<setting id="viervijfzes_refresh_token" default="" visible="eq(-21,Zes)|eq(-21,Vijf)|eq(-21,Vier)" />
<setting id="viervijfzes_username" type="text" label="30094" default="" visible="eq(-15,Zes)|eq(-15,Vijf)|eq(-15,Vier)" />
<setting id="viervijfzes_password_set" label="30093" type="action" action="RunScript(plugin.video.retrospect, 0, ?action=encryptsetting&amp;settingid=viervijfzes_password&amp;settingname=Vier/Vijf/Zes.be password&amp;tabfocus=102)" option="close" visible="eq(-16,Zes)|eq(-16,Vijf)|eq(-16,Vier)" />
<setting id="viervijfzes_password" default="" visible="eq(-17,Zes)|eq(-17,Vijf)|eq(-17,Vier)" />
<setting id="viervijfzes_refresh_token" default="" visible="eq(-18,Zes)|eq(-18,Vijf)|eq(-18,Vier)" />
<!-- chn_vrtnu.py -->
<setting id="channel_F530A9EC-3C0D-49B6-96C2-480273417460_username" label="30094" type="text" default="" visible="eq(-22,VRT NU)" />
<setting id="channel_F530A9EC-3C0D-49B6-96C2-480273417460_password_set" label="30093" type="action" action="RunScript(plugin.video.retrospect, 0, ?action=encryptsetting&amp;settingid=channel_F530A9EC-3C0D-49B6-96C2-480273417460_password&amp;settingname=VRT NU password&amp;tabfocus=102)" option="close" visible="eq(-23,VRT NU)" />
<setting id="channel_F530A9EC-3C0D-49B6-96C2-480273417460_password" default="" visible="eq(-24,VRT NU)" />
<setting id="channel_F530A9EC-3C0D-49B6-96C2-480273417460_hls_over_dash" label="30099" type="bool" default="" visible="eq(-25,VRT NU)" />
<setting id="channel_F530A9EC-3C0D-49B6-96C2-480273417460_username" label="30094" type="text" default="" visible="eq(-19,VRT NU)" />
<setting id="channel_F530A9EC-3C0D-49B6-96C2-480273417460_password_set" label="30093" type="action" action="RunScript(plugin.video.retrospect, 0, ?action=encryptsetting&amp;settingid=channel_F530A9EC-3C0D-49B6-96C2-480273417460_password&amp;settingname=VRT NU password&amp;tabfocus=102)" option="close" visible="eq(-20,VRT NU)" />
<setting id="channel_F530A9EC-3C0D-49B6-96C2-480273417460_password" default="" visible="eq(-21,VRT NU)" />
<setting id="channel_F530A9EC-3C0D-49B6-96C2-480273417460_hls_over_dash" label="30099" type="bool" default="" visible="eq(-22,VRT NU)" />
<!-- chn_vtmbe.py -->
<setting id="mediaan_username" type="text" label="30094" default="" visible="eq(-26,Stievie)|eq(-26,VTM.be)" />
<setting id="mediaan_password" default="" visible="eq(-27,Stievie)|eq(-27,VTM.be)" />
<setting id="mediaan_password_set" label="30093" type="action" action="RunScript(plugin.video.retrospect, 0, ?action=encryptsetting&amp;settingid=mediaan_password&amp;settingname=Mediaan Wachtwoord.be&amp;tabfocus=102)" option="close" visible="eq(-28,Stievie)|eq(-28,VTM.be)" />
<setting id="mediaan_signature" default="" visible="eq(-29,Stievie)|eq(-29,VTM.be)" />
<setting id="mediaan_username" type="text" label="30094" default="" visible="eq(-23,Stievie)|eq(-23,VTM.be)" />
<setting id="mediaan_password" default="" visible="eq(-24,Stievie)|eq(-24,VTM.be)" />
<setting id="mediaan_password_set" label="30093" type="action" action="RunScript(plugin.video.retrospect, 0, ?action=encryptsetting&amp;settingid=mediaan_password&amp;settingname=Mediaan Wachtwoord.be&amp;tabfocus=102)" option="close" visible="eq(-25,Stievie)|eq(-25,VTM.be)" />
<setting id="mediaan_signature" default="" visible="eq(-26,Stievie)|eq(-26,VTM.be)" />
<!-- end of channel settings -->
</category>

Expand Down

0 comments on commit 789698f

Please sign in to comment.