-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth-server.py
356 lines (237 loc) · 11.1 KB
/
auth-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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from aiohttp import web
import base64
import hashlib
import json
import os
import secrets
import sys
import urllib.parse
# Datastore is a minimal implementation for the purposes of the example
class Datastore:
def __init__(self):
self.users = {}
self.tokens = {}
def add_user(self, user_id, **user_info):
self.users[user_id] = user_info
def get_user(self, user_id, remove_password=True):
if user_id not in self.users:
return None
user = self.users.get(user_id).copy()
user['user_id'] = user_id
if remove_password and 'password' in user:
del user['password']
return user
def get_user_by_email(self, email, remove_password=True):
for user_id, user in self.users.items():
if user.get('email') == email:
return self.get_user(user_id, remove_password=remove_password)
return None
def get_user_from_token(self, token, remove_password=True):
user_id = self.get_token(token)
return self.get_user(user_id, remove_password=remove_password)
def add_token(self, value, type='token', bits=128, test=None):
t = base64.b32encode(secrets.token_bytes(int(bits / 8))).decode('utf-8').lower().replace('=', '')
token = type + '-' + t
if test:
token = test
self.tokens[token] = value
return token
def get_token(self, token):
return self.tokens.get(token)
def delete_token(self, token):
if token in self.tokens:
del self.tokens[token]
routes = web.RouteTableDef()
def check_password(password, hashed_and_salted_password):
if password is None:
return False
password_to_check = base64.b64decode(hashed_and_salted_password.encode('utf-8'))
salt = password_to_check[:8] # 64 bit
hashed_password = password_to_check[8:]
h = hashlib.blake2b(salt=salt)
h.update(password.encode('utf-8'))
return hashed_password == h.digest()
def response_ok(text, headers={}):
print(f'HTTPOk {text=}')
print('headers =', json.dumps(headers, indent=2))
raise web.HTTPOk(text=text, headers=headers)
def redirect_to_signin_page(text, headers={}, redirect=None, host=None):
query = {}
if text is not None:
# comma not allowed in sign-in page because of a split on the comma
query['reason'] = text.replace(',', ';')
if redirect is not None:
# redirect can contain commas because it comes from X-Forwarded-Uri
query['redirect'] = redirect
query_text = urllib.parse.urlencode(query)
location = f'https://{host}/auth/sign-in?' + query_text
print(f'{location=}')
raise web.HTTPSeeOther(text=text, location=location, headers=headers)
def get_user_from_header(request):
host = request.headers.get('Host')
user = request.headers.get('X-User')
if user is None:
redirect_to_signin_page('X-User header not found', host=host)
try:
return json.loads(user)
except json.decoder.JSONDecodeError as e:
print('get_user_from_header()', e)
redirect_to_signin_page('invalid X-User header', host=host)
def delete_auth_cookie(request):
ds = request.app.get('ds')
domain = request.headers.get('Host')
session_token = request.cookies.get('Auth')
ds.delete_token(session_token)
return {'Set-Cookie': f'Auth=deleted; Domain={domain}; ' + \
'Path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT'}
# Authenticate each request (via forward_auth)
@routes.get('/auth/check')
async def auth_check(request):
ds = request.app.get('ds')
host = request.headers.get('Host')
forwarded_uri = request.headers.get('X-Forwarded-Uri')
forwarded_proto = request.headers.get('X-Forwarded-Proto')
# X-Forwarded-For: <client>, <proxy1>, <proxy2>
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
client_uri = forwarded_uri.split(',', 1)[0]
new_path = urllib.parse.urlparse(client_uri).path
print(f'---------- /auth/check ---------> {new_path=}')
print(f'{forwarded_uri=}')
print(f'{client_uri=}')
headers = {}
authorization = request.headers.get('Authorization')
if authorization:
# Bearer token authentication (assume JSON based request and response)
bearer, api_token = authorization.split(' ', 1)
user = ds.get_user_from_token(api_token)
print(f'{api_token=} {user=}')
if user is None:
raise web.HTTPUnauthorized()
else:
# Password authentication (with two public routes)
if new_path in ['/auth/sign-in', '/auth/password-authenticate']:
response_ok(f'continue to {new_path=}', headers=headers)
if new_path == '/auth/sign-out':
new_path = None
session_token = request.cookies.get('Auth')
print(f'found {session_token=}')
if session_token is None:
redirect_to_signin_page('(auth cookie not found)',
headers=headers, redirect=new_path, host=host)
user = ds.get_user_from_token(session_token)
if user is None:
headers.update(delete_auth_cookie(request))
redirect_to_signin_page(f'(user not found)',
headers=headers, redirect=new_path, host=host)
headers.update({'X-User': json.dumps(user)})
response_ok('continue to authorised page', headers=headers)
# all remaining routes are auth server functions (to routes /auth/*)
@routes.post('/auth/password-authenticate')
async def auth_password_authenticate(request):
ds = request.app.get('ds')
print('POST /auth/password-authenticate')
domain = request.headers.get('Host')
if request.can_read_body:
body = await request.text()
data = urllib.parse.parse_qs(body)
email = data.get('email', [None])[0]
password = data.get('password', [None])[0]
redirect = data.get('redirect', [None])[0]
if email is None:
redirect_to_signin_page('Please enter an email address', host=host)
if password is None:
redirect_to_signin_page('Please enter a password', host=host)
user = ds.get_user_by_email(email, remove_password=False)
if user is None:
redirect_to_signin_page('Email address not found', host=host)
user_id = user.get('user_id')
stored_password = user.get('password')
if not check_password(password, stored_password):
redirect_to_signin_page('Invalid password', host=host)
session_token = ds.add_token(user_id, type='session')
headers = {'Set-Cookie': f'Auth={session_token}; Max-Age=5184000; Domain={domain}; ' + \
'Path=/; Secure; HttpOnly; SameSite=Lax;'}
print(f'setting cookie and adding session token {session_token}')
if not redirect:
redirect = '/'
print(f'{ds.tokens=} {redirect=}')
raise web.HTTPFound(location=redirect, headers=headers)
@routes.get('/auth/sign-in')
async def sign_in(request):
reason = request.query.get('reason', '').split(',', 1)[0]
redirect = request.query.get('redirect', '').split(',', 1)[0]
print('/sign-in page (then post to /auth/password-authenticate)')
print(f'{reason=}')
print(f'{redirect=}')
return web.Response(content_type='text/html', text=f"""
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sign-in</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x"
crossorigin="anonymous">
</head>
<body>
<div class="container mt-5 text-center">
</div>
<div class="container mt-2 mb-5" style="width: 330px;">
<h4 class="text-center text-secondary mt-3">
Please Sign-in
</h4>
<p id="message" class="text-center small text-warning">{reason}</p>
<form action="/auth/password-authenticate" method="post">
<input type="hidden" id='redirect' name="redirect" vaue="{redirect}">
<div class="form-group mt-2">
<input type="email" class="form-control" name="email" placeholder="Email address">
</div>
<div class="form-group mt-2">
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
<button type="submit" name="sub-type" value="password" class="btn btn-primary w-100 mt-2">
Sign-in with password</button>
<!-- button type="submit" name="sub-type" value="email" class="btn btn-secondary w-100 mt-2">
Sign-in with email</button -->
</form>
<!-- p class="text-center small text-secondary">No password is needed when signing-in via email.
Check your email for link.</p -->
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4"
crossorigin="anonymous"></script>
<!-- script>
const params = new Proxy(new URLSearchParams(window.location.search), {{
get: (searchParams, prop) => searchParams.get(prop),
}});
document.getElementById("message").textContent = params.reason;
document.getElementById("redirect").value = params.redirect;
</script -->
</body>
</html>
""")
@routes.get('/auth/sign-out')
async def auth_sign_out(request):
headers = delete_auth_cookie(request)
user = get_user_from_header(request)
user_name = user.get('name')
response_ok(f'{user_name} signed-out', headers=headers)
if __name__ == '__main__':
print('starting auth server')
app = web.Application()
app.add_routes(routes)
ds = Datastore()
ds.add_user('30000', name='John Smith', email='[email protected]', roles=['admin'],
password='XhgQ0LD5a9V/FSeGn2s+LiF5eb4w2OspcPo/AjqNW1NkItexaB/z7Eltp+ymcIHrqSzIK+T2f3TRi09dKGmtSw1NHu4nynSV')
ds.add_user('30001', name='Service Account', roles=['service-account'])
ds.add_user('30002', name='Mary Jones', email='[email protected]', roles=['user'],
password='bDYJ4jMqxfDmiwAICkwYsfacfFFZKtcrRlu4YiuwC768k1PVy0wWJROH4TDmkQK1jxO8GQ4sjTqfkDFv3tHguBMIzjtCq00I')
if len(sys.argv) == 2:
# if a token is specified, make sure it is long and random (e.g. api-bfyujsagbtnfvfwjvwfut3hiwy)
api_token = ds.add_token('30001', test=sys.argv[1])
else:
api_token = ds.add_token('30001', type='api')
print(f'Authorization: Bearer {api_token}')
app['ds'] = ds
web.run_app(app, port=8001)