forked from Cal-CS-61A-Staff/composition-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
309 lines (263 loc) · 10.5 KB
/
auth.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
"""
Bacon OK integration: mostly ported from OK Client
https://github.com/okpy/ok-client/blob/master/client/utils/auth.py
"""
import http.server
import logging
import requests
import sys
import time
import webbrowser
from urllib.parse import parse_qsl, urlencode, urlparse
log = logging.getLogger(__name__)
CLIENT_ID = "hog-contest"
# Don't worry, it is ok to share this
CLIENT_SECRET = "1hWDyQjZS6PSVVhAJzQpUqzkYPyM0EN"
OAUTH_SCOPE = "all"
# Localhost
REDIRECT_HOST = "127.0.0.1"
REDIRECT_PORT = 6265
# OAuth post timeout
TIMEOUT = 10
# Server/API config
OK_SERVER_URL = "https://okpy.org"
INFO_ENDPOINT = "/api/v3/user/"
ASSIGNMENT_ENDPOINT = "/api/v3/assignment/"
AUTH_ENDPOINT = "/oauth/authorize"
TOKEN_ENDPOINT = "/oauth/token"
ERROR_ENDPOINT = "/oauth/errors"
# URL to redirect user to upon OAuth success
SUCCESS_ENDPOINT_URL = "https://okpy.org" # temporary
class BaconOkException(Exception):
"""Base exception class for Bacon/OK integration."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
log.debug("Exception raised: {}".format(type(self).__name__))
log.debug("python version: {}".format(sys.version_info))
log.debug("okpy version: {}".format(client.__version__))
class OAuthException(BaconOkException):
""" OAuth related exception """
def __init__(self, error="", error_description=""):
super().__init__()
self.error = error
self.error_description = error_description
def _pick_free_port(hostname=REDIRECT_HOST, port=0):
""" Try to bind a port. Default=0 selects a free port. """
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((hostname, port)) # port=0 finds an open port
except OSError as e:
if port == 0:
print("Unable to find an open port for authentication.")
raise BaconOkException(e)
else:
return _pick_free_port(hostname, 0)
addr, port = s.getsockname()
s.close()
return port
def _make_token_post(server, data):
"""Try getting an access token from the server. If successful, returns the
JSON response. If unsuccessful, raises an OAuthException.
"""
try:
response = requests.post(server + TOKEN_ENDPOINT, data=data, timeout=TIMEOUT)
body = response.json()
except Exception as e:
log.warning("Other error when exchanging code", exc_info=True)
raise OAuthException(error="Authentication Failed", error_description=str(e))
if "error" in body:
log.error(body)
raise OAuthException(
error=body.get("error", "Unknown Error"),
error_description=body.get("error_description", ""),
)
return body
def _make_code_post(server, code, redirect_uri):
data = {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"code": code,
"grant_type": "authorization_code",
"redirect_uri": redirect_uri,
}
info = _make_token_post(server, data)
return info["access_token"], int(info["expires_in"]), info["refresh_token"]
def _make_refresh_post(server, refresh_token):
data = {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"grant_type": "refresh_token",
"refresh_token": refresh_token,
}
info = _make_token_post(server, data)
return info["access_token"], int(info["expires_in"]), info["refresh_token"]
def _get_code():
""" Make the requests to get OK access code """
# email = input("Please enter your bCourses email: ")
host_name = REDIRECT_HOST
try:
port_number = _pick_free_port(port=REDIRECT_PORT)
except BaconOkException:
# Could not bind to REDIRECT_HOST:0, try localhost instead
host_name = "localhost"
port_number = _pick_free_port(host_name, 0)
redirect_uri = "http://{0}:{1}/".format(host_name, port_number)
params = {
"client_id": CLIENT_ID,
"redirect_uri": redirect_uri,
"response_type": "code",
"scope": OAUTH_SCOPE,
}
url = "{}{}?{}".format(OK_SERVER_URL, AUTH_ENDPOINT, urlencode(params))
try:
assert webbrowser.open_new(url)
return _get_code_via_browser(redirect_uri, host_name, port_number)
except Exception as e:
log.debug("Error with Browser Auth:\n{}".format(traceback.format_exc()))
log.warning("Browser auth failed, falling back to browserless auth")
# Try to do without a browser
redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
print("\nOpen this URL in a browser window:\n")
print("{}/client/login/".format(OK_SERVER_URL))
code = input("\nPaste your code here: ")
return _make_code_post(server_url(cmd_args), code, redirect_uri)
def _get_code_via_browser(redirect_uri, host_name, port_number):
""" Get OK access code by opening User's browser """
server = OK_SERVER_URL
code_response = None
oauth_exception = None
class CodeHandler(http.server.BaseHTTPRequestHandler):
def send_redirect(self, location):
self.send_response(302)
self.send_header("Location", location)
self.end_headers()
def send_failure(self, oauth_exception):
params = {
"error": oauth_exception.error,
"error_description": oauth_exception.error_description,
}
url = "{}{}?{}".format(server, ERROR_ENDPOINT, urlencode(params))
self.send_redirect(url)
def do_GET(self):
"""Respond to the GET request made by the OAuth"""
nonlocal code_response, oauth_exception
log.debug("Received GET request for %s", self.path)
path = urlparse(self.path)
qs = {k: v for k, v in parse_qsl(path.query)}
code = qs.get("code")
if code:
try:
code_response = _make_code_post(server, code, redirect_uri)
except OAuthException as e:
oauth_exception = e
else:
oauth_exception = OAuthException(
error=qs.get("error", "Unknown Error"),
error_description=qs.get("error_description", ""),
)
if oauth_exception:
self.send_failure(oauth_exception)
else:
self.send_redirect(SUCCESS_ENDPOINT_URL)
def log_message(self, format, *args):
return
server_address = (host_name, port_number)
log.info("Authentication server running on {}:{}".format(host_name, port_number))
try:
httpd = http.server.HTTPServer(server_address, CodeHandler)
httpd.handle_request()
except OSError as e:
log.warning("HTTP Server Err {}".format(server_address), exc_info=True)
raise
if oauth_exception:
raise oauth_exception
return code_response
class OAuthSession:
""" Represents OK OAuth state """
def __init__(self, access_token="", refresh_token="", expires_at=-1, session=None):
""" Create OK OAuth state with given tokens, and expiration """
self.session = self.refresh_token = self.access_token = None
self.expires_at = -1
self.assignment = None
if session is not None:
config = session.config()
self.session = session
if "ok_access_token" in config:
self.access_token = config["ok_access_token"]
if "ok_refresh_token" in config:
self.refresh_token = config["ok_refresh_token"]
if "ok_expires_at" in config:
self.expires_at = int(config["ok_expires_at"])
if "ok_last_download_assignment" in config:
self.assignment = config["ok_last_download_assignment"]
elif access_token and refresh_token and expires_at >= 0:
self.access_token = str(access_token)
self.refresh_token = str(refresh_token)
self.expires_at = expires_at
def _dump(self):
""" Dump state to a Bacon session """
if self.session is not None:
config = self.session.config()
if self.access_token:
config["ok_access_token"] = self.access_token
if self.refresh_token:
config["ok_refresh_token"] = self.refresh_token
if self.expires_at >= 0:
config["ok_expires_at"] = str(self.expires_at)
if self.assignment:
config["ok_last_download_assignment"] = self.assignment
def refresh(self):
""" Refreshes a token """
if not self.refresh_token:
return False
cur_time = int(time.time())
if cur_time < self.expires_at - 3600:
# expires in 1 hour
return True
self.access_token, expires_in, self.refresh_token = _make_refresh_post(
OK_SERVER_URL, self.refresh_token
)
if not (self.access_token and expires_in):
log.warning("Refresh authentication failed and returned an empty token.")
return False
cur_time = int(time.time())
self.expires_at = cur_time + expires_in
self._dump()
return True
def auth(self, force_reauth=False):
"""
Returns OAuth access token which can be passed to the server
for identification. If force_reauth is specified then will
force re-authenticate the user; else tries to reuse or
refresh previous token
"""
# Attempts to import SSL or raises an exception
try:
import ssl
except:
log.warning("Error importing SSL module", stack_info=True)
print(SSL_ERROR_MESSAGE)
sys.exit(1)
else:
log.info("SSL module is available")
# Refresh the token if not forcing reauth
if not force_reauth and self.refresh():
return self.access_token
# Perform OAuth
print("Token is not available, performing OAuth")
try:
self.access_token, expires_in, self.refresh_token = _get_code()
except UnicodeDecodeError as e:
with format.block("-"):
print("Authentication error\n:{}".format(HOSTNAME_ERROR_MESSAGE))
except OAuthException as e:
with format.block("-"):
print("Authentication error: {}".format(e.error.replace("_", " ")))
if e.error_description:
print(e.error_description)
else:
cur_time = int(time.time())
self.expires_at = cur_time + expires_in
self._dump()
return self.access_token