This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtornado_server.py
238 lines (181 loc) · 7.18 KB
/
tornado_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
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
import logging
import json
import os
import signal
import sys
import urllib
import urlparse
from multiprocessing.process import Process
sys.path.insert(0, os.path.abspath(os.path.realpath(__file__) + '/../../../'))
from oauth2 import Provider
from oauth2.error import UserNotAuthenticated
from oauth2.grant import AuthorizationCodeGrant
from oauth2.tokengenerator import Uuid4
from oauth2.store.memory import ClientStore, TokenStore
from oauth2.web import AuthorizationCodeGrantSiteAdapter
from oauth2.web.tornado import OAuth2Handler
from tornado.ioloop import IOLoop
from tornado.web import Application, url
from wsgiref.simple_server import make_server, WSGIRequestHandler
logging.basicConfig(level=logging.DEBUG)
class ClientRequestHandler(WSGIRequestHandler):
"""
Request handler that enables formatting of the log messages on the console.
This handler is used by the client application.
"""
def address_string(self):
return "client app"
class OAuthRequestHandler(WSGIRequestHandler):
"""
Request handler that enables formatting of the log messages on the console.
This handler is used by the python-oauth2 application.
"""
def address_string(self):
return "python-oauth2"
class TestSiteAdapter(AuthorizationCodeGrantSiteAdapter):
"""
This adapter renders a confirmation page so the user can confirm the auth
request.
"""
CONFIRMATION_TEMPLATE = """
<html>
<body>
<p>
<a href="{url}&confirm=1">confirm</a>
</p>
<p>
<a href="{url}&confirm=0">deny</a>
</p>
</body>
</html>
"""
def render_auth_page(self, request, response, environ, scopes, client):
url = request.path + "?" + request.query_string
response.body = self.CONFIRMATION_TEMPLATE.format(url=url)
return response
def authenticate(self, request, environ, scopes, client):
if request.method == "GET":
if request.get_param("confirm") == "1":
return
raise UserNotAuthenticated
def user_has_denied_access(self, request):
if request.method == "GET":
if request.get_param("confirm") == "0":
return True
return False
class ClientApplication(object):
"""
Very basic application that simulates calls to the API of the
python-oauth2 app.
"""
callback_url = "http://localhost:8081/callback"
client_id = "abc"
client_secret = "xyz"
api_server_url = "http://localhost:8080"
def __init__(self):
self.access_token = None
self.auth_token = None
self.token_type = ""
def __call__(self, env, start_response):
if env["PATH_INFO"] == "/app":
status, body, headers = self._serve_application(env)
elif env["PATH_INFO"] == "/callback":
status, body, headers = self._read_auth_token(env)
else:
status = "301 Moved"
body = ""
headers = {"Location": "/app"}
start_response(status,
[(header, val) for header,val in headers.iteritems()])
return body
def _request_access_token(self):
print("Requesting access token...")
post_params = {"client_id": self.client_id,
"client_secret": self.client_secret,
"code": self.auth_token,
"grant_type": "authorization_code",
"redirect_uri": self.callback_url}
token_endpoint = self.api_server_url + "/token"
result = urllib.urlopen(token_endpoint,
urllib.urlencode(post_params))
content = ""
for line in result:
content += line
result = json.loads(content)
self.access_token = result["access_token"]
self.token_type = result["token_type"]
confirmation = "Received access token '%s' of type '%s'" % (self.access_token, self.token_type)
print(confirmation)
return "302 Found", "", {"Location": "/app"}
def _read_auth_token(self, env):
print("Receiving authorization token...")
query_params = urlparse.parse_qs(env["QUERY_STRING"])
if "error" in query_params:
location = "/app?error=" + query_params["error"][0]
return "302 Found", "", {"Location": location}
self.auth_token = query_params["code"][0]
print("Received temporary authorization token '%s'" % (self.auth_token,))
return "302 Found", "", {"Location": "/app"}
def _request_auth_token(self):
print("Requesting authorization token...")
auth_endpoint = self.api_server_url + "/authorize"
query = urllib.urlencode({"client_id": "abc",
"redirect_uri": self.callback_url,
"response_type": "code"})
location = "%s?%s" % (auth_endpoint, query)
return "302 Found", "", {"Location": location}
def _serve_application(self, env):
query_params = urlparse.parse_qs(env["QUERY_STRING"])
if ("error" in query_params
and query_params["error"][0] == "access_denied"):
return "200 OK", "User has denied access", {}
if self.access_token is None:
if self.auth_token is None:
return self._request_auth_token()
else:
return self._request_access_token()
else:
confirmation = "Current access token '%s' of type '%s'" % (self.access_token, self.token_type)
return "200 OK", str(confirmation), {}
def run_app_server():
app = ClientApplication()
try:
httpd = make_server('', 8081, app, handler_class=ClientRequestHandler)
print("Starting Client app on http://localhost:8081/...")
httpd.serve_forever()
except KeyboardInterrupt:
httpd.server_close()
def run_auth_server():
client_store = ClientStore()
client_store.add_client(client_id="abc", client_secret="xyz",
redirect_uris=["http://localhost:8081/callback"])
token_store = TokenStore()
provider = Provider(access_token_store=token_store,
auth_code_store=token_store, client_store=client_store,
token_generator=Uuid4())
provider.add_grant(AuthorizationCodeGrant(site_adapter=TestSiteAdapter()))
try:
app = Application([
url(provider.authorize_path, OAuth2Handler, dict(provider=provider)),
url(provider.token_path, OAuth2Handler, dict(provider=provider)),
])
app.listen(8080)
print("Starting OAuth2 server on http://localhost:8080/...")
IOLoop.current().start()
except KeyboardInterrupt:
IOLoop.close()
def main():
auth_server = Process(target=run_auth_server)
auth_server.start()
app_server = Process(target=run_app_server)
app_server.start()
print("Access http://localhost:8081/app in your browser")
def sigint_handler(signal, frame):
print("Terminating servers...")
auth_server.terminate()
auth_server.join()
app_server.terminate()
app_server.join()
signal.signal(signal.SIGINT, sigint_handler)
if __name__ == "__main__":
main()