-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_server.py
71 lines (60 loc) · 2.1 KB
/
class_server.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
import http.server
import requests
import urllib.parse
from class_page import Page
class Server(http.server.BaseHTTPRequestHandler):
def get_addr(self, path='/'):
return 'http://{:s}:{:d}{:s}'.format(self.server.server_name, self.server.server_port, path)
def get_client(self):
return self.server.client
def get_auth_url(self):
return self.get_client()['authorization_endpoint'] + '?' + urllib.parse.urlencode({
'client_id': self.get_client()['client_id'],
'scope': 'offline_access files.readwrite',
'redirect_uri': self.get_addr('/auth'),
'response_type': 'code',
})
def get_exch_url(self):
return self.get_client()['token_endpoint']
def get_exch_data(self, code):
return {
'client_id': self.get_client()['client_id'],
'redirect_uri': self.get_addr('/auth'),
'client_secret': self.get_client()['client_secret'],
'code': code,
'grant_type': 'authorization_code',
}
def redirect(self, url):
self.send_response(307)
self.send_header('Location', url)
self.end_headers()
def alert(self, message):
page = Page()
page.add_body_tag('<p>{:s}</p>'.format(message)) # TODO escape html
html = page.get_html()
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(bytes(html, 'utf-8'))
def do_GET(self):
path = urllib.parse.urlparse(self.path)
try:
if path.path == '/init':
self.redirect(self.get_auth_url())
# TODO revoke user consent
elif path.path == '/auth':
query = urllib.parse.parse_qs(path.query, True)
assert 'error' not in query, query['error_description'][0]
code = query['code'][0]
r = requests.post(self.get_exch_url(), data=self.get_exch_data(code))
r = r.json()
assert 'error' not in r, r['error_description']
refresh_token = r['refresh_token']
self.server.callback(refresh_token)
self.redirect(self.get_addr('/over'))
elif path.path == '/over':
self.alert('Close this tab. Then return to the application and press Ctrl+C.')
except Exception as e:
error = str(e)
self.log_error(error)
self.alert('Error: {:s}'.format(error)) # TODO set error code