-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2.py
executable file
·180 lines (150 loc) · 5.45 KB
/
oauth2.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
import requests, json, os, base64, webbrowser, time
from urllib.parse import urlparse, parse_qs
from http.server import BaseHTTPRequestHandler
from socketserver import ThreadingTCPServer
def rand_string(dlen):
return base64.b64encode(os.urandom(dlen)).decode()
def read_secrets():
with open('secrets.json', 'r') as secrets:
j = json.loads(secrets.read())
return j
def write_secret(key, val):
j = read_secrets()
j[key] = val
with open('secrets.json', 'w') as secrets:
secrets.write(json.dumps(j))
base_url = 'http://localhost:8000' # TODO replace with production url ('https://www.racetime.gg/')
auth_endpoint = '/o/authorize'
token_endpoint = '/o/token'
userinfo_endpoint = '/o/userinfo'
success_endpoint = '/o/done'
failure_endpoint = '/o/done?error=access_denied'
redirect_port = 1223
redirect_ip = '127.0.0.1'
redirect_uri = 'http://%s:%s' % (redirect_ip, redirect_port)
j = read_secrets()
client_id = j['client_id']
client_secret = j['client_secret']
client_code = j['client_code']
refresh_token = j['refresh_token']
auth_token = None
# for handling http redirect
class RedirectHandler(BaseHTTPRequestHandler):
def __init__(self, request, address, server):
super().__init__(request, address, server)
def do_GET(self):
global base_url, success_endpoint, failure_endpoint
parsed = urlparse(self.path)
qs = parse_qs(parsed.query)
if 'error' in qs:
self.server.error = True
self.send_response(301)
self.send_header('Location', base_url + failure_endpoint)
self.end_headers()
else:
self.server.error = False
self.server.code = qs['code'][0]
self.send_response(301)
self.send_header('Location', base_url + success_endpoint)
self.end_headers()
class RedirectServer(ThreadingTCPServer):
def __init__(self, redirect_ip, redirect_port):
self.code = None
self.error = None
super().__init__((redirect_ip, redirect_port), RedirectHandler)
self.allow_reuse_address = True
# first time user authorization
def do_auth():
response_type = 'code'
scope = 'read chat_message race_action'
state = rand_string(32)
full_url = base_url + auth_endpoint + '?response_type=%s&client_id=%s&redirect_uri=%s&scope=%s&state=%s' % (response_type, client_id, redirect_uri, scope, state)
webbrowser.open(full_url)
with RedirectServer(redirect_ip, redirect_port) as httpd:
httpd.handle_request()
while httpd.error is None:
time.sleep(1)
if httpd.error:
code = None
else:
code = httpd.code
write_secret('client_code', code)
return code
# get user's info
def get_user_info():
if auth_token == '':
return None
url = base_url + userinfo_endpoint
headers = {'Authorization': 'Bearer %s' % (auth_token,)}
r = requests.get(url, headers=headers)
if r.status_code == 200:
return json.loads(r.text)
else:
return None
def token_request(data):
print('Need client code.. it is ' + str(client_code))
if client_code == '' or client_code is None:
return None, None
full_url = base_url + token_endpoint
r = requests.post(full_url, data=data)
if r.status_code == 200:
j = json.loads(r.text)
token = j['access_token']
refresh_token = j['refresh_token']
write_secret('refresh_token', refresh_token)
print('Success. token %s refresh %s' % (token, refresh_token))
return token, refresh_token
else:
print('Error doing token request')
print(r.text)
return None, None
# get a token + refresh token
def get_new_token():
data = {
'grant_type': 'authorization_code',
'code': client_code,
'client_id': client_id
}
return token_request(data)
# refresh token
def try_renew_access():
if refresh_token == '' or refresh_token is None:
return None, None
data = {
'grant_type': 'refresh_token',
'code': client_code,
'client_id': client_id,
'client_secret': client_secret,
'refresh_token': refresh_token
}
return token_request(data)
# select the right method for getting tokens
def get_tokens():
print('Checking refresh token: ' + str(refresh_token))
if refresh_token == '' or refresh_token is None:
print('getting new')
return get_new_token()
else:
print('refreshing')
return try_renew_access()
def authorize():
global auth_token, refresh_token, client_code
# 1. try to get user info
user_info = get_user_info()
if user_info is not None:
print('User info get success :)')
return True
# 2. if this fails, try to renew access
auth_token, refresh_token = get_tokens()
if auth_token is not None:
print('Token refresh success :)')
return True
# 3. if that failed, ask user to authenticate
client_code = do_auth()
if client_code is None:
return False
# 4. user is authorized :)
print('Auth code get success :)')
auth_token, refresh_token = get_tokens()
print('Final auth step complete! token %s renew %s' % (auth_token, refresh_token))
return True