forked from segxy/samples-python-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
208 lines (171 loc) · 6.15 KB
/
Copy pathapp.py
File metadata and controls
208 lines (171 loc) · 6.15 KB
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
from datetime import datetime
from datetime import timedelta
import calendar
import json
import os
import urllib
from flask import Flask
from flask import make_response
from flask import redirect
from flask import request
from flask import session
from flask import url_for
from flask_stache import render_template
from jose import jws
from jose import jwt
from requests.auth import HTTPBasicAuth
import jose
import requests
cwd = os.path.dirname(os.path.realpath(__file__))
app = Flask(__name__,
static_folder='dist',
static_url_path='/assets',
template_folder='{}/tools/templates'.format(cwd))
app.secret_key = 'SECRET KEY THAT YOU **MUST** CHANGE ON PRODUCTION SYSTEMS!'
allowed_issuers = []
public_key_cache = {}
config = None
with open('.samples.config.json') as config_file:
config_json = json.load(config_file)
config = config_json['oktaSample']
# Get allowed issuer from the OKTA_ALLOWED_ISSUER environment variable,
# use the 'oktaUrl' from our config file if that doesn't exist
allowed_issuer = os.getenv('OKTA_ALLOWED_ISSUER', config['oidc']['issuer'])
allowed_issuers.append(allowed_issuer)
def fetch_jwk_for(id_token=None):
if id_token is None:
raise NameError('id_token is required')
jwks_uri = "{}/v1/keys".format(config['oidc']['issuer'])
unverified_header = jws.get_unverified_header(id_token)
key_id = None
if 'kid' in unverified_header:
key_id = unverified_header['kid']
else:
raise ValueError('The id_token header must contain a "kid"')
if key_id in public_key_cache:
return public_key_cache[key_id]
r = requests.get(jwks_uri)
jwks = r.json()
for key in jwks['keys']:
jwk_id = key['kid']
public_key_cache[jwk_id] = key
if key_id in public_key_cache:
return public_key_cache[key_id]
else:
raise RuntimeError("Unable to fetch public key from jwks_uri")
@app.route("/")
def scenarios():
return render_template('overview',
config=config)
@app.route("/authorization-code/login-redirect")
def auth_login_redirect():
return render_template('login-redirect',
config=config)
@app.route("/authorization-code/login-custom")
def auth_login_custom():
return render_template('login-custom',
config=config)
@app.route("/authorization-code/logout")
def auth_logout():
session.clear()
return redirect(url_for('scenarios'))
@app.route("/authorization-code/profile")
def auth_profile():
if 'user' not in session:
return redirect(url_for('scenarios'))
return make_response(render_template('profile',
user=session['user'],
config=config))
@app.route("/authorization-code/callback")
def auth_callback():
nonce = None
state = None
cookies = request.cookies
if (('okta-oauth-nonce' in cookies) and ('okta-oauth-state' in cookies)):
nonce = cookies['okta-oauth-nonce']
state = cookies['okta-oauth-state']
else:
return "invalid nonce or state", 401
if (request.args.get('state') != state):
err = "'{}' != '{}'".format(
request.args.get('state'),
state)
return "invalid state: {}".format(err), 401
if 'code' not in request.args:
return "no code in request arguments", 401
auth = HTTPBasicAuth(config['oidc']['clientId'],
config['oidc']['clientSecret'])
querystring = {
'grant_type': 'authorization_code',
'code': request.args.get('code'),
'redirect_uri': config['oidc']['redirectUri']
}
url = "{}/v1/token".format(config['oidc']['issuer'])
qs = "grant_type=authorization_code&code={}&redirect_uri={}".format(
urllib.quote_plus(querystring['code']),
urllib.quote_plus(querystring['redirect_uri'])
)
url = "{}/v1/token?{}".format(config['oidc']['issuer'], qs)
headers = {
'User-Agent': None,
'Connection': 'close',
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post(url,
# params=querystring,
stream=False,
auth=auth,
headers=headers)
return_value = r.json()
if 'id_token' not in return_value:
return "no id_token in response from /token endpoint", 401
id_token = return_value['id_token']
five_minutes_in_seconds = 300
leeway = five_minutes_in_seconds
jwt_kwargs = {
'algorithms': 'RS256',
'options': {
'verify_at_hash': False,
# Used for leeway on the "exp" claim
'leeway': leeway
},
'issuer': config['oidc']['issuer'],
'audience': config['oidc']['clientId']
}
if 'access_token' in return_value:
jwt_kwargs['access_token'] = return_value['access_token']
try:
jwks_with_public_key = fetch_jwk_for(id_token)
claims = jwt.decode(
id_token,
jwks_with_public_key,
**jwt_kwargs)
except (jose.exceptions.JWTClaimsError,
jose.exceptions.JWTError,
jose.exceptions.JWSError,
jose.exceptions.ExpiredSignatureError,
NameError,
ValueError), err:
return str(err), 401
if nonce != claims['nonce']:
return "invalid nonce", 401
# Validate 'iat' claim
# FIXME: Open PR for moving this code here: https://git.io/v1D8M
time_now_with_leeway = datetime.utcnow() + timedelta(seconds=leeway)
acceptable_iat = calendar.timegm((time_now_with_leeway).timetuple())
if 'iat' in claims and claims['iat'] > acceptable_iat:
return "invalid iat claim", 401
session['user'] = {
'email': claims['email'],
'claims': claims
}
return redirect(url_for('auth_profile'))
# FIXME: I shouldn't need to define this once we get static assets
# in a dedicated statics directory:
@app.route('/bundle.js')
def bundlejs():
return app.send_static_file('bundle.js')
if __name__ == "__main__":
app.debug = True
app.run(port=3000)