-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
274 lines (225 loc) · 8.26 KB
/
app.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
##########################################################################
# Copyright 2016 Curity AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################
import json
import sys
import urllib2
from flask import redirect, request, render_template, session, Flask
from jwkest import BadSignature
from urlparse import urlparse
from client import Client
from tools import decode_token, generate_random_string
from validator import JwtValidator
from config import Config
_app = Flask(__name__)
class UserSession:
def __init__(self):
pass
access_token = None
refresh_token = None
id_token = None
access_token_json = None
id_token_json = None
name = None
api_response = None
@_app.route('/')
def index():
"""
:return: the index page with the tokens, if set.
"""
user = None
if 'session_id' in session:
user = _session_store.get(session['session_id'])
if user:
if user.id_token:
user.id_token_json = decode_token(user.id_token)
if user.access_token:
user.access_token_json = decode_token(user.access_token)
return render_template('index.html',
server_name=urlparse(_config['authorization_endpoint']).netloc,
session=user)
@_app.route('/start-login')
def start_code_flow():
"""
:return: redirects to the authorization server with the appropriate parameters set.
"""
login_url = _client.get_authn_req_url(session)
return redirect(login_url)
@_app.route('/logout')
def logout():
"""
Logout clears the session, along with the tokens
:return: redirects to /
"""
if 'session_id' in session:
del _session_store[session['session_id']]
session.clear()
if 'logout_endpoint' in _config:
print "Logging out against", _config['logout_endpoint']
return redirect(_config['logout_endpoint'] + '?redirect_uri=' + _base_url)
return redirect_with_baseurl('/')
@_app.route('/refresh')
def refresh():
"""
Refreshes the access token using the refresh token
:return: redirects to /
"""
user = _session_store.get(session['session_id'])
try:
token_data = _client.refresh(user.refresh_token)
except Exception as e:
create_error('Could not refresh Access Token', e)
return
user.access_token = token_data['access_token']
user.refresh_token = token_data['refresh_token']
return redirect_with_baseurl('/')
@_app.route('/revoke')
def revoke():
"""
Revokes the access and refresh token and clears the sessions
:return: redirects to /
"""
if 'session_id' in session:
user = _session_store.get(session['session_id'])
if not user:
redirect_with_baseurl('/')
if user.refresh_token:
try:
_client.revoke(user.refresh_token)
except urllib2.URLError as e:
return create_error('Could not revoke refresh token', e)
user.refresh_token = None
return redirect_with_baseurl('/')
@_app.route('/call-api')
def call_api():
"""
Call an api using the Access Token
:return: the index template with the data from the api in the parameter 'data'
"""
if 'session_id' in session:
user = _session_store.get(session['session_id'])
if not user:
return redirect_with_baseurl('/')
if 'api_endpoint' in _config:
user.api_response = None
if user.access_token:
try:
request = urllib2.Request(_config['api_endpoint'])
request.add_header("Authorization", "Bearer %s" % user.access_token)
response = urllib2.urlopen(request)
user.api_response = {'code': response.code, 'data': response.read()}
except urllib2.HTTPError as e:
user.api_response = {'code': e.code, 'data': e.read()}
else:
user.api_response = None
print 'No access token in session'
else:
user.api_response = None
print 'No API endpoint configured'
return redirect_with_baseurl('/')
@_app.route('/callback')
def oauth_callback():
"""
Called when the resource owner is returning from the authorization server
:return:redirect to / with user info stored in the session.
"""
if 'state' not in session or session['state'] != request.args['state']:
return create_error('Missing or invalid state')
if 'code' not in request.args:
return create_error('No code in response')
try:
token_data = _client.get_token(request.args['code'])
except Exception as e:
return create_error('Could not fetch token(s)', e)
session.pop('state', None)
# Store in basic server session, since flask session use cookie for storage
user = UserSession()
if 'access_token' in token_data:
user.access_token = token_data['access_token']
if _jwt_validator and 'id_token' in token_data:
# validate JWS; signature, aud and iss.
# Token type, access token, ref-token and JWT
if 'issuer' not in _config:
return create_error('Could not validate token: no issuer configured')
try:
_jwt_validator.validate(token_data['id_token'], _config['issuer'], _config['client_id'])
except BadSignature as bs:
return create_error('Could not validate token: %s' % bs.message)
except Exception as ve:
return create_error("Unexpected exception: %s" % ve.message)
user.id_token = token_data['id_token']
if 'refresh_token' in token_data:
user.refresh_token = token_data['refresh_token']
session['session_id'] = generate_random_string()
_session_store[session['session_id']] = user
return redirect_with_baseurl('/')
def create_error(message, exception = None):
"""
Print the error and output it to the page
:param message:
:return: redirects to index.html with the error message
"""
print 'Caught error!'
print message, exception
if _app:
user = None
if 'session_id' in session:
user = _session_store.get(session['session_id'])
return render_template('index.html',
server_name=urlparse(_config['authorization_endpoint']).netloc,
session=user,
error=message)
def load_config():
"""
Load config from the file given by argument, or settings.json
:return:
"""
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = 'settings.json'
config = Config(filename)
return config.load_config()
def redirect_with_baseurl(path):
return redirect(_base_url + path)
if __name__ == '__main__':
# load the config
_config = load_config()
_client = Client(_config)
# load the jwk set.
if 'jwks_uri' in _config:
_jwt_validator = JwtValidator(_config)
else:
print 'Found no url to JWK set, will not be able to validate JWT signature.'
_jwt_validator = None
# create a session store
_session_store = {}
# initiate the app
_app.secret_key = generate_random_string()
# some default values
_debug = 'debug' in _config and _config['debug']
if 'port' in _config:
_port = _config['port']
else:
_port = 5443
_disable_https = 'disable_https' in _config and _config['disable_https']
if 'base_url' in _config:
_base_url = _config['base_url']
else:
_base_url = ''
if _disable_https:
_app.run('0.0.0.0', debug=_debug, port=_port)
else:
_app.run('0.0.0.0', debug=_debug, port=_port, ssl_context=('keys/localhost.pem', 'keys/localhost.pem'))