-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorize.py
93 lines (69 loc) · 2.55 KB
/
authorize.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
import time
from urllib import parse
from uuid import uuid4
import requests
from selenium import webdriver
from selenium.webdriver.support.expected_conditions import visibility_of
from selenium.webdriver.support.ui import WebDriverWait
STEPS = [
('#sign-in', True),
('[data-patient-id=smart-1288992]', True),
('[data-slide=next]', True),
('[type=checkbox]', '''
Array.from(document.querySelectorAll('input.glyphbox')).forEach(function(el) { el.disabled = false; });
Array.from(document.querySelectorAll('input.glyphbox')).forEach(function(el) { el.checked = true; });
Array.from(document.querySelectorAll('button[data-slide=next]')).forEach(function(el) { el.disabled = false; });
Array.from(document.querySelectorAll('button[data-slide=next]'))[1].click();
'''),
('#authorize', True)
]
CLIENT_ID = '890870a1-c021-4a2a-8df4-1e7113a8f5e0'
CLIENT_SECRET = '703e05fd-fec1-4b81-a699-cbb988f3c647'
REDIRECT_URI = 'https://not-a-real-site/authorized'
SCOPE = 'launch/patient patient/*.read offline_access'
AUTHORIZE_URI = 'https://portal.demo.syncfor.science/oauth/authorize'
TOKEN_URI = 'https://portal.demo.syncfor.science/oauth/token'
AUD = 'https://portal.demo.syncfor.science/api/fhir/'
class CurrentURIContains:
def __init__(self, s):
self._s = s
def __call__(self, driver):
return self._s in driver.current_url
def get_code():
driver = webdriver.Firefox()
driver.implicitly_wait(10)
launch_params = {
'response_type': 'code',
'client_id': CLIENT_ID,
'redirect_uri': REDIRECT_URI,
'scope': SCOPE,
'state': uuid4(),
'aud': AUD
}
authorize_url = f'{AUTHORIZE_URI}?{parse.urlencode(launch_params)}'
driver.get(authorize_url)
for step in STEPS:
execute_step(driver, step)
wait = WebDriverWait(driver, 60)
wait.until(CurrentURIContains(REDIRECT_URI))
url = parse.urlparse(driver.current_url)
return parse.parse_qs(url.query)['code'][0]
def execute_step(driver, step):
time.sleep(1)
subject, predicate = step
if predicate in (True, False):
driver.find_element_by_css_selector(subject).click()
else:
driver.execute_script(predicate)
def get_bearer_token():
code = get_code()
data = {
'grant_type': 'authorization_code',
'code': code,
'client_id': CLIENT_ID,
'redirect_uri': REDIRECT_URI
}
response = requests.post(TOKEN_URI, auth=(CLIENT_ID, CLIENT_SECRET), data=data)
return response.json()['access_token']
if __name__ == '__main__':
print(get_bearer_token())