/
token.py
474 lines (413 loc) · 18.9 KB
/
token.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import base64
import datetime
import hashlib
import logging
import re
import secrets
import urllib
from urllib.parse import parse_qs, urlparse
import requests
from bs4 import BeautifulSoup
from requests.adapters import HTTPAdapter
from requests_toolbelt.utils import dump
from urllib3 import Retry
from postcard_creator.postcard_creator import PostcardCreatorException
LOGGING_TRACE_LVL = 5
logger = logging.getLogger('postcard_creator')
logging.addLevelName(LOGGING_TRACE_LVL, 'TRACE')
setattr(logger, 'trace', lambda *args: logger.log(LOGGING_TRACE_LVL, *args))
def base64_encode(string):
encoded = base64.urlsafe_b64encode(string).decode('ascii')
return encoded.rstrip("=")
def base64_decode(string):
padding = 4 - (len(string) % 4)
string = string + ("=" * padding)
return base64.urlsafe_b64decode(string)
def _log_response(h):
for h in h.history:
logger.debug(h.request.method + ': ' + str(h) + ' ' + h.url)
logger.debug(h.request.method + ': ' + str(h) + ' ' + h.url)
def _print_request(response):
logger.debug(' {} {} [{}]'.format(response.request.method, response.request.url, response.status_code))
def _dump_request(response):
_print_request(response)
data = dump.dump_all(response)
try:
logger.trace(data.decode())
except Exception:
data = str(data).replace('\\r\\n', '\r\n')
logger.trace(data)
def _log_and_dump(r):
_log_response(r)
_dump_request(r)
class Token(object):
def __init__(self, _protocol='https://'):
self.protocol = _protocol
self.base = '{}account.post.ch'.format(self.protocol)
self.swissid = '{}login.swissid.ch'.format(self.protocol)
self.token_url = '{}postcardcreator.post.ch/saml/SSO/alias/defaultAlias'.format(self.protocol)
self.user_agent = 'Mozilla/5.0 (Linux; Android 6.0.1; wv) ' + \
'AppleWebKit/537.36 (KHTML, like Gecko) ' + \
'Version/4.0 Chrome/52.0.2743.98 Mobile Safari/537.36'
self.legacy_headers = {
'User-Agent': self.user_agent
}
self.swissid_headers = {
'User-Agent': self.user_agent
}
self.token = None
self.token_type = None
self.token_expires_in = None
self.token_fetched_at = None
self.cache_token = False
def has_valid_credentials(self, username, password, method='mixed'):
try:
self.fetch_token(username, password, method=method)
return True
except PostcardCreatorException:
return False
def fetch_token(self, username, password, method='mixed'):
logger.debug('fetching postcard account token')
if username is None or password is None:
raise PostcardCreatorException('No username/ password given')
methods = ['mixed', 'legacy', 'swissid']
if method not in methods:
raise PostcardCreatorException('unknown method. choose from: ' + methods)
success = False
access_token = None
implementation_type = ''
if method != 'swissid':
logger.info("using legacy username password authentication")
session = self._create_session()
try:
access_token = self._get_access_token_legacy(session, username, password)
logger.debug('legacy username/password authentication was successful')
success = True
implementation_type = 'legacy'
except Exception as e:
logger.info("legacy username password authentication failed")
logger.info(e)
if method == 'mixed':
logger.info("Trying swissid now because method=legacy")
else:
logger.info("giving up")
raise e
pass
if method != 'legacy' and not success:
logger.info("using swissid username password authentication")
try:
session = self._create_session()
access_token = self._get_access_token_swissid(session, username, password)
logger.debug('swissid username/password authentication was successful')
implementation_type = 'swissid'
except Exception as e:
logger.info("swissid username password authentication failed")
logger.info(e)
raise e
try:
logger.trace(access_token)
self.token = access_token['access_token']
self.token_type = access_token['token_type']
self.token_expires_in = access_token['expires_in']
self.token_fetched_at = datetime.datetime.now()
self.token_implementation = implementation_type
logger.info("access_token successfully fetched")
except Exception as e:
logger.info("access_token does not contain required values. someting broke")
raise e
def _create_session(self, retries=5, backoff_factor=0.5, status_forcelist=(500, 502, 504)):
# XXX: Backend will terminate connection if we request too frequently
session = requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def _get_code_verifier(self):
return base64_encode(secrets.token_bytes(64))
def _get_code(self, code_verifier):
m = hashlib.sha256()
m.update(code_verifier.encode('utf-8'))
return base64_encode(m.digest())
def _get_access_token_legacy(self, session, username, password):
code_verifier = self._get_code_verifier()
code_resp_uri = self._get_code(code_verifier)
redirect_uri = 'ch.post.pcc://auth/1016c75e-aa9c-493e-84b8-4eb3ba6177ef'
client_id = 'ae9b9894f8728ca78800942cda638155'
client_secret = '89ff451ede545c3f408d792e8caaddf0'
init_data = {
'client_id': client_id,
'response_type': 'code',
'redirect_uri': redirect_uri,
'scope': 'PCCWEB offline_access',
'response_mode': 'query',
'state': 'abcd',
'code_challenge': code_resp_uri,
'code_challenge_method': 'S256',
'lang': 'en'
}
url = 'https://pccweb.api.post.ch/OAuth/authorization?'
resp = session.get(url + urllib.parse.urlencode(init_data),
allow_redirects=True,
headers=self.legacy_headers)
_log_and_dump(resp)
url_payload = {
'targetURL': 'https://pccweb.api.post.ch/SAML/ServiceProvider/?redirect_uri=' + redirect_uri,
'profile': 'default',
'app': 'pccwebapi',
'inMobileApp': 'true',
'layoutType': 'standard'
}
data_payload = {
'isiwebuserid': username,
'isiwebpasswd': password,
'confirmLogin': '',
}
url = 'https://account.post.ch/idp/?login&'
resp = session.post(url + urllib.parse.urlencode(url_payload),
allow_redirects=True,
headers=self.legacy_headers, data=data_payload)
_log_and_dump(resp)
resp = session.post(url + urllib.parse.urlencode(url_payload),
allow_redirects=True,
headers=self.legacy_headers)
saml_soup = BeautifulSoup(resp.text, 'html.parser')
saml_response = saml_soup.find('input', {'name': 'SAMLResponse'})
if saml_response is None or saml_response.get('value') is None:
raise PostcardCreatorException('Username/password authentication failed. Are your credentials valid?.')
saml_response = saml_response['value']
relay_state = saml_soup.find('input', {'name': 'RelayState'})['value'],
url = "https://pccweb.api.post.ch/OAuth/" # important: '/' at the end
customer_headers = self.legacy_headers
customer_headers['Origin'] = 'https://account.post.ch'
customer_headers['X-Requested-With'] = 'ch.post.it.pcc'
customer_headers['Upgrade-Insecure-Requests'] = str(1)
saml_payload = {
'RelayState': relay_state,
'SAMLResponse': saml_response
}
resp = session.post(url,
headers=customer_headers,
data=saml_payload,
allow_redirects=False) # do not follow redirects as we cannot redirect to android uri
try:
code_resp_uri = resp.headers['Location']
init_data = parse_qs(urlparse(code_resp_uri).query)
resp_code = init_data['code'][0]
except Exception as e:
print(e)
raise PostcardCreatorException('response does not have code attribute: ' + url + '. Did endpoint break?')
# get access token
data = {
'grant_type': 'authorization_code',
'client_id': client_id,
'client_secret': client_secret,
'code': resp_code,
'code_verifier': code_verifier,
'redirect_uri': redirect_uri,
}
url = 'https://pccweb.api.post.ch/OAuth/token'
resp = requests.post(url,
data=data,
headers=self.legacy_headers,
allow_redirects=False)
_log_and_dump(resp)
if 'access_token' not in resp.json() or resp.status_code != 200:
raise PostcardCreatorException("not able to fetch access token: " + resp.text)
return resp.json()
def _get_access_token_swissid(self, session, username, password):
code_verifier = self._get_code_verifier()
code_resp_uri = self._get_code(code_verifier)
redirect_uri = 'ch.post.pcc://auth/1016c75e-aa9c-493e-84b8-4eb3ba6177ef'
client_id = 'ae9b9894f8728ca78800942cda638155'
client_secret = '89ff451ede545c3f408d792e8caaddf0'
init_data = {
'client_id': client_id,
'response_type': 'code',
'redirect_uri': redirect_uri,
'scope': 'PCCWEB offline_access',
'response_mode': 'query',
'state': 'abcd',
'code_challenge': code_resp_uri,
'code_challenge_method': 'S256',
'lang': 'en'
}
url = 'https://pccweb.api.post.ch/OAuth/authorization?'
resp = session.get(url + urllib.parse.urlencode(init_data),
allow_redirects=True,
headers=self.swissid_headers)
_log_and_dump(resp)
saml_payload = {
'externalIDP': 'externalIDP'
}
url = 'https://account.post.ch/idp/?login' \
'&targetURL=https://pccweb.api.post.ch/SAML/ServiceProvider/' \
'?redirect_uri=' + redirect_uri + \
'&profile=default' \
'&app=pccwebapi&inMobileApp=true&layoutType=standard'
resp = session.post(url,
data=saml_payload,
allow_redirects=True,
headers=self.swissid_headers)
_log_and_dump(resp)
if len(resp.history) == 0:
raise PostcardCreatorException('fail to fetch ' + url)
step1_goto_url = resp.history[len(resp.history) - 1].headers['Location']
goto_param = re.search(r'goto=(.*?)$', step1_goto_url).group(1)
try:
goto_param = goto_param.split('&')[0]
except Exception as e:
# only use goto_param without further params
pass
logger.trace("goto parm=" + goto_param)
if goto_param is None or goto_param == '':
raise PostcardCreatorException('swissid: cannot fetch goto param')
url = "https://login.swissid.ch/api-login/authenticate/token/status?locale=en&goto=" + goto_param + \
"&acr_values=loa-1&realm=%2Fsesam&service=qoa1"
resp = session.get(url, allow_redirects=True)
_log_and_dump(resp)
url = "https://login.swissid.ch/api-login/welcome-pack?locale=en" + goto_param + \
"&acr_values=loa-1&realm=%2Fsesam&service=qoa1"
resp = session.get(url, allow_redirects=True)
_log_and_dump(resp)
# login with username and password
url = 'https://login.swissid.ch/api-login/authenticate/init?locale=en&goto=' + goto_param + \
"&acr_values=loa-1&realm=%2Fsesam&service=qoa1"
resp = session.post(url, allow_redirects=True)
_log_and_dump(resp)
# submit username and password
url_query_string = "locale=en&goto=" + goto_param + \
"&acr_values=loa-1&realm=%2Fsesam&service=qoa1"
url = "https://login.swissid.ch/api-login/authenticate/basic?" + url_query_string
headers = self.swissid_headers
headers['authId'] = resp.json()['tokens']['authId']
step_data = {
'username': username,
'password': password
}
resp = session.post(url, json=step_data, headers=headers, allow_redirects=True)
_log_and_dump(resp)
# anomaly detection
resp = self._swiss_id_anomaly_detection(session, resp, url_query_string)
try:
url = resp.json()['nextAction']['successUrl']
except Exception as e:
logger.info("failed to login. username/password wrong?")
raise PostcardCreatorException("failed to login, username/password wrong?")
resp = session.get(url, headers=self.swissid_headers, allow_redirects=True)
_log_and_dump(resp)
step7_soup = BeautifulSoup(resp.text, 'html.parser')
url = step7_soup.find('form', {'name': 'LoginForm'})['action']
resp = session.post(url, headers=self.swissid_headers)
_log_and_dump(resp)
# find saml response
step7_soup = BeautifulSoup(resp.text, 'html.parser')
saml_response = step7_soup.find('input', {'name': 'SAMLResponse'})
if saml_response is None or saml_response.get('value') is None:
raise PostcardCreatorException('Username/password authentication failed. Are your credentials valid?.')
# prepare access token
url = "https://pccweb.api.post.ch/OAuth/" # important: '/' at the end
customer_headers = self.swissid_headers
customer_headers['Origin'] = 'https://account.post.ch'
customer_headers['X-Requested-With'] = 'ch.post.it.pcc'
customer_headers['Upgrade-Insecure-Requests'] = str(1)
saml_payload = {
'RelayState': step7_soup.find('input', {'name': 'RelayState'})['value'],
'SAMLResponse': saml_response.get('value')
}
resp = session.post(url, headers=customer_headers,
data=saml_payload,
allow_redirects=False) # do not follow redirects as we cannot redirect to android uri
try:
code_resp_uri = resp.headers['Location']
init_data = parse_qs(urlparse(code_resp_uri).query)
resp_code = init_data['code'][0]
except Exception as e:
print(e)
raise PostcardCreatorException('response does not have code attribute: ' + url + '. Did endpoint break?')
# get access token
data = {
'grant_type': 'authorization_code',
'client_id': client_id,
'client_secret': client_secret,
'code': resp_code,
'code_verifier': code_verifier,
'redirect_uri': redirect_uri,
}
url = 'https://pccweb.api.post.ch/OAuth/token'
resp = requests.post(url, # we do not use session here!
data=data,
headers=self.swissid_headers,
allow_redirects=False)
_log_and_dump(resp)
if 'access_token' not in resp.json() or resp.status_code != 200:
raise PostcardCreatorException("not able to fetch access token: " + resp.text)
return resp.json()
def _swiss_id_anomaly_detection(self, session, prev_response, url_query_string):
# XXX: Starting 2022-10, endpoints introduce anomaly detection, possibly to further restrict automated access
# Currently, any valid device_print payload seems to work
# useragent in request and payload can differ and still be valid
url = 'https://login.swissid.ch/api-login/anomaly-detection/device-print?' + url_query_string
device_print_ctx = prev_response.json()
try:
next_action = device_print_ctx['nextAction']['type']
if next_action != 'SEND_DEVICE_PRINT':
logger.warning('next action must be SEND_DEVICE_PRINT but got ' + next_action)
auth_id_device_print = device_print_ctx['tokens']['authId']
device_print = self._formulate_anomaly_detection()
headers = self.swissid_headers
headers['authId'] = auth_id_device_print
resp = session.post(url, json=device_print, headers=headers)
_log_and_dump(resp)
except Exception as e:
msg = "Anomaly detection step failed. \n" \
+ f"previous response body: {device_print_ctx}\n" \
+ f"pending request: {url} \n"
logger.info(msg)
logger.info(e)
raise PostcardCreatorException(msg, e)
return resp
def _formulate_anomaly_detection(self):
# Valid device_print generated in an x86 android 12 emulator,
# XXX: if something breaks in the future, we may have to get more clever here
device_print = {
"appCodeName": "Mozilla",
"appName": "Netscape",
"appVersion": self.user_agent.replace('Mozilla/', ''), # Mozilla/5.0
"fonts": {
"installedFonts": "cursive;monospace;serif;sans-serif;fantasy;default;Arial;Courier;" + \
"Courier New;Georgia;Tahoma;Times;Times New Roman;Verdana"
},
"language": "de",
"platform": "Linux x86_64",
"plugins": {
"installedPlugins": ""
},
"product": "Gecko",
"productSub": "20030107",
"screen": {
"screenColourDepth": 24,
"screenHeight": 732,
"screenWidth": 412
},
"timezone": {
"timezone": -120
},
"userAgent": self.user_agent,
"vendor": "Google Inc."
}
return device_print
def to_json(self):
return {
'fetched_at': self.token_fetched_at,
'token': self.token,
'expires_in': self.token_expires_in,
'type': self.token_type,
'implementation': self.token_implementation
}