-
Notifications
You must be signed in to change notification settings - Fork 20
/
tokenresolver.py
280 lines (244 loc) · 12 KB
/
tokenresolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, unicode_literals
from resources.lib.helperobjects import helperobjects
try:
from urllib.parse import urlencode, unquote
from urllib.request import build_opener, install_opener, ProxyHandler, HTTPCookieProcessor, HTTPErrorProcessor, urlopen, Request
except ImportError:
from urllib import urlencode # pylint: disable=ungrouped-imports
from urllib2 import build_opener, install_opener, ProxyHandler, HTTPCookieProcessor, HTTPErrorProcessor, unquote, urlopen, Request
class NoRedirection(HTTPErrorProcessor):
def http_response(self, request, response):
return response
https_response = http_response
class TokenResolver:
_API_KEY = '3_qhEcPa5JGFROVwu5SWKqJ4mVOIkwlFNMSKwzPDAh8QZOtHqu6L4nD5Q7lk0eXOOG'
_LOGIN_URL = 'https://accounts.vrt.be/accounts.login'
_TOKEN_GATEWAY_URL = 'https://token.vrt.be'
_VRT_LOGIN_URL = 'https://login.vrt.be/perform_login'
_ONDEMAND_COOKIE = 'ondemand_vrtPlayerToken'
_LIVE_COOKIE = 'live_vrtPlayerToken'
_ROAMING_XVRTTOKEN_COOKIE = 'roaming_XVRTToken'
_FAV_TOKEN_GATEWAY_URL = 'https://token.vrt.be/vrtnuinitlogin?provider=site&destination=https://www.vrt.be/vrtnu/'
_FAV_XVRTTOKEN_COOKIE = 'user_XVRTToken'
_XVRT_TOKEN_COOKIE = 'XVRTToken'
def __init__(self, _kodi):
self._kodi = _kodi
self._proxies = _kodi.get_proxies()
install_opener(build_opener(ProxyHandler(self._proxies)))
def get_ondemand_playertoken(self, token_url, xvrttoken):
token_path = self._kodi.get_userdata_path() + self._ONDEMAND_COOKIE
token = self._get_cached_token(token_path, 'vrtPlayerToken')
if xvrttoken and token is None:
cookie_value = 'X-VRT-Token=' + xvrttoken
headers = {'Content-Type': 'application/json', 'Cookie': cookie_value}
token = self._get_new_playertoken(token_path, token_url, headers)
return token
def get_live_playertoken(self, token_url, xvrttoken):
token_path = self._kodi.get_userdata_path() + self._LIVE_COOKIE
token = self._get_cached_token(token_path, 'vrtPlayerToken')
if token is None:
if xvrttoken is None:
headers = {'Content-Type': 'application/json'}
else:
cookie_value = 'X-VRT-Token=' + xvrttoken
headers = {'Content-Type': 'application/json', 'Cookie': cookie_value}
token = self._get_new_playertoken(token_path, token_url, headers)
return token
def get_xvrttoken(self, get_roaming_token=False):
token_filename = self._ROAMING_XVRTTOKEN_COOKIE if get_roaming_token else self._XVRT_TOKEN_COOKIE
token_path = self._kodi.get_userdata_path() + token_filename
token = self._get_cached_token(token_path, 'X-VRT-Token')
if token is None:
token = self._get_new_xvrttoken(token_path, get_roaming_token)
return token
def get_fav_xvrttoken(self):
token_path = self._kodi.get_userdata_path() + self._FAV_XVRTTOKEN_COOKIE
token = self._get_cached_token(token_path, 'X-VRT-Token')
if token is None:
token = self._get_fav_xvrttoken(token_path)
return token
def _get_new_playertoken(self, path, token_url, headers):
import json
self._kodi.log_notice('URL post: ' + unquote(token_url), 'Verbose')
req = Request(token_url, data=b'', headers=headers)
playertoken = json.loads(urlopen(req).read())
with self._kodi.open_file(path, 'w') as f:
json.dump(playertoken, f)
return playertoken.get('vrtPlayerToken')
def _get_cached_token(self, path, token_name):
cached_token = None
if self._kodi.check_if_path_exists(path):
from datetime import datetime
import dateutil.parser
import dateutil.tz
import json
with self._kodi.open_file(path) as f:
token = json.loads(f.read())
now = datetime.now(dateutil.tz.tzlocal())
exp = dateutil.parser.parse(token.get('expirationDate'))
if exp > now:
self._kodi.log_notice("Got cached token '%s'" % path, 'Verbose')
cached_token = token.get(token_name)
else:
self._kodi.log_notice("Cached token '%s' deleted" % path, 'Verbose')
self._kodi.delete_file(path)
return cached_token
def _get_new_xvrttoken(self, path, get_roaming_token):
import json
cred = helperobjects.Credentials(self._kodi)
if not cred.are_filled_in():
self._kodi.open_settings()
cred.reload()
payload = dict(
loginID=cred.username,
password=cred.password,
sessionExpiration='-1',
APIKey=self._API_KEY,
targetEnv='jssdk',
)
data = urlencode(payload).encode('utf8')
self._kodi.log_notice('URL post: ' + unquote(self._LOGIN_URL), 'Verbose')
req = Request(self._LOGIN_URL, data=data)
logon_json = json.loads(urlopen(req).read())
token = None
if logon_json.get('errorCode') != 0:
self._handle_error(logon_json, cred)
return None
login_token = logon_json.get('sessionInfo', dict()).get('login_token')
login_cookie = 'glt_%s=%s' % (self._API_KEY, login_token)
payload = dict(
uid=logon_json.get('UID'),
uidsig=logon_json.get('UIDSignature'),
ts=logon_json.get('signatureTimestamp'),
email=cred.username,
)
data = json.dumps(payload).encode('utf8')
headers = {'Content-Type': 'application/json', 'Cookie': login_cookie}
self._kodi.log_notice('URL post: ' + unquote(self._TOKEN_GATEWAY_URL), 'Verbose')
req = Request(self._TOKEN_GATEWAY_URL, data=data, headers=headers)
try: # Python 2
cookie_data = urlopen(req).info().getheader('Set-Cookie').split('X-VRT-Token=')[1].split('; ')
except AttributeError:
cookie_data = urlopen(req).info().get('Set-Cookie').split('X-VRT-Token=')[1].split('; ')
xvrttoken = TokenResolver._create_token_dictionary_from_urllib(cookie_data)
if get_roaming_token:
xvrttoken = self._get_roaming_xvrttoken(xvrttoken)
if xvrttoken is not None:
token = xvrttoken.get('X-VRT-Token')
with self._kodi.open_file(path, 'w') as f:
json.dump(xvrttoken, f)
return token
def _get_fav_xvrttoken(self, path):
try:
import http.cookiejar as cookielib
except ImportError:
import cookielib
import json
cred = helperobjects.Credentials(self._kodi)
if not cred.are_filled_in():
self._kodi.open_settings()
cred.reload()
payload = dict(
loginID=cred.username,
password=cred.password,
sessionExpiration='-1',
APIKey=self._API_KEY,
targetEnv='jssdk',
)
data = urlencode(payload).encode('utf8')
self._kodi.log_notice('URL post: ' + unquote(self._LOGIN_URL), 'Verbose')
req = Request(self._LOGIN_URL, data=data)
logon_json = json.loads(urlopen(req).read())
token = None
if logon_json.get('errorCode') != 0:
self._handle_error(logon_json, cred)
return None
payload = dict(
UID=logon_json.get('UID'),
UIDSignature=logon_json.get('UIDSignature'),
signatureTimestamp=logon_json.get('signatureTimestamp'),
client_id='vrtnu-site',
submit='submit',
)
data = urlencode(payload).encode('utf8')
cookiejar = cookielib.CookieJar()
opener = build_opener(HTTPCookieProcessor(cookiejar))
self._kodi.log_notice('URL get: ' + unquote(self._FAV_TOKEN_GATEWAY_URL), 'Verbose')
opener.open(self._FAV_TOKEN_GATEWAY_URL)
self._kodi.log_notice('URL post: ' + unquote(self._VRT_LOGIN_URL), 'Verbose')
opener.open(self._VRT_LOGIN_URL, data=data)
xvrttoken = TokenResolver._create_token_dictionary(cookiejar)
if xvrttoken is not None:
token = xvrttoken.get('X-VRT-Token')
with self._kodi.open_file(path, 'w') as f:
json.dump(xvrttoken, f)
return token
def _handle_error(self, logon_json, cred):
message = logon_json.get('errorDetails')
if message == 'invalid loginID or password':
cred.reset()
message = self._kodi.localize(30952) # Invalid login!
elif message == 'loginID must be provided':
message = self._kodi.localize(30955) # Please fill in username
elif message == 'Missing required parameter: password':
message = self._kodi.localize(30956) # Please fill in password
self._kodi.show_ok_dialog(heading=self._kodi.localize(30951), message=message) # Login failed!
self._kodi.end_of_directory()
def _get_roaming_xvrttoken(self, xvrttoken):
roaming_xvrttoken = None
url = 'https://token.vrt.be/vrtnuinitloginEU?destination=https://www.vrt.be/vrtnu/'
cookie_value = 'X-VRT-Token=' + xvrttoken.get('X-VRT-Token')
headers = {'Cookie': cookie_value}
opener = build_opener(NoRedirection, ProxyHandler(self._proxies))
self._kodi.log_notice('URL get: ' + unquote(url), 'Verbose')
req = Request(url, headers=headers)
req_info = opener.open(req).info()
try: # Python 2
cookie_value += '; state=' + req_info.getheader('Set-Cookie').split('state=')[1].split('; ')[0]
except AttributeError: # Python 3
cookie_value += '; state=' + req_info.get('Set-Cookie').split('state=')[1].split('; ')[0]
url = req_info.getheader('Location')
self._kodi.log_notice('URL get: ' + unquote(url), 'Verbose')
try: # Python 2
url = opener.open(url).info().getheader('Location')
except AttributeError: # Python 3
url = opener.open(url).info().get('Location')
headers = {'Cookie': cookie_value}
if url is not None:
self._kodi.log_notice('URL get: ' + unquote(url), 'Verbose')
req = Request(url, headers=headers)
try: # Python 2
cookie_data = opener.open(req).info().getheader('Set-Cookie').split('X-VRT-Token=')[1].split('; ')
except AttributeError: # Python 3
cookie_data = opener.open(req).info().get('Set-Cookie').split('X-VRT-Token=')[1].split('; ')
roaming_xvrttoken = TokenResolver._create_token_dictionary_from_urllib(cookie_data)
return roaming_xvrttoken
@staticmethod
def _create_token_dictionary(cookie_jar):
token_dictionary = None
# Get cookie from cookiejar
xvrttoken_cookie = next(cookie for cookie in cookie_jar if cookie.name == 'X-VRT-Token')
if xvrttoken_cookie is not None:
from datetime import datetime
token_dictionary = {
xvrttoken_cookie.name: xvrttoken_cookie.value,
'expirationDate': datetime.utcfromtimestamp(xvrttoken_cookie.expires).strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
}
return token_dictionary
@staticmethod
def _create_token_dictionary_from_urllib(cookie_data):
import dateutil.parser
token_dictionary = {
'X-VRT-Token': cookie_data[0],
'expirationDate': dateutil.parser.parse(cookie_data[2].strip('Expires=')).strftime('%Y-%m-%dT%H:%M:%S.%fZ')
}
return token_dictionary
def reset_cookies(self):
user_data_path = self._kodi.get_userdata_path()
self._kodi.delete_file(user_data_path + self._ONDEMAND_COOKIE)
self._kodi.delete_file(user_data_path + self._LIVE_COOKIE)
self._kodi.delete_file(user_data_path + self._XVRT_TOKEN_COOKIE)
self._kodi.delete_file(user_data_path + self._ROAMING_XVRTTOKEN_COOKIE)
self._kodi.delete_file(user_data_path + self._FAV_XVRTTOKEN_COOKIE)