-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback_server.py
47 lines (35 loc) · 1.43 KB
/
callback_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
from auth_user import AuthUser
from bottle import Bottle, request, run, static_file, template
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError
import config
app = Bottle()
callback_func = None
@app.route('/callback')
def callback():
try:
user_id: str = request.params["state"]
user: AuthUser
if user_id == "server":
user = AuthUser.server_user()
else:
user = AuthUser.from_id(user_id)
print("USER: " + str(user))
user.get_access_token(request.params["code"])
callback_func(user)
return template("success")
except AttributeError: # user hasn't started the auth flow yet
return template("failure", reason="Authorization flow hasn't begun yet. Please make sure that you aren't using the same authorization link twice.")
except InvalidGrantError: # user is re-using an access code
return template("failure", reason="Invalid authorization grant.")
except KeyError: # query parameters missing
return template("failure", reason="Parameters missing")
@app.route("/static/<filename:path>")
def static(filename: str):
return static_file(filename, root="static")
def run_server(event_loop, callback):
def cb(user):
event_loop.call_soon_threadsafe(callback, user)
global callback_func
callback_func = cb
print("bottle server running!")
run(app, host=config.HOST, port=int(config.PORT))