-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_server.py
More file actions
313 lines (214 loc) · 9.91 KB
/
Copy pathdatabase_server.py
File metadata and controls
313 lines (214 loc) · 9.91 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
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
"""
This script uses two dependencies:
+ aiohttp (https://docs.aiohttp.org/en/stable/index.html)
+ asyncpg (https://magicstack.github.io/asyncpg/current/index.html)
These can be installed by running:
pip install aiohttp aiohttp_cors asyncpg Authlib
The database I have running is called "postgres", is using a user with
name "postgres" and password "pg". I also created a table under the
"public" schema called "user" which has a "user_id" and "user_name" column
"""
from datetime import datetime
from hashlib import pbkdf2_hmac
from os import urandom
from asyncpg import create_pool
from aiohttp import web
import aiohttp_cors
from authlib.jose import errors, jwt
from datetime import datetime, timedelta
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import load_pem_private_key
def get_key_with_value(dictionary, target_value):
for key, value in dictionary.items():
if value == target_value:
return key
def load_keys(password):
with open('private.pem', 'rb') as f:
private_key = load_pem_private_key(
f.read(), password=password.encode(), backend=default_backend())
with open('public.pem', 'rb') as f:
public_key = f.read()
return public_key, private_key
def validate_request(request):
try:
authorization_value = request.headers["Authorization"]
except KeyError:
raise web.HTTPUnprocessableEntity(text="No authorization header provided!")
auth_type, auth_token = authorization_value.split(" ")
if auth_type != "Bearer":
raise web.HTTPUnprocessableEntity(text="Invalid authentication method!")
tokens = request.app["tokens"]
if auth_token not in tokens.values():
raise web.HTTPUnprocessableEntity(text="Invalidated authentication token used!")
claims = jwt.decode(auth_token, request.app["public_key"])
try:
claims.validate()
return claims
except errors.ExpiredTokenError:
del request.app["tokens"][get_key_with_value(tokens, auth_token)]
raise web.HTTPUnprocessableEntity(text="Provided token is expired!")
routes = web.RouteTableDef()
async def setup_app(app):
"""
An example of a cleanup context in aiohttp
https://docs.aiohttp.org/en/stable/web_advanced.html#cleanup-context
"""
# On server startup
# Using a static password for demo purposes
app["pg_pool"] = await create_pool(
database="agora", user="postgres", password="pg")
yield
# On server shutdown (cleanup)
await app['pg_pool'].close()
@routes.get("/users")
async def get_users(request):
""" Get list of users """
async with request.app["pg_pool"].acquire() as connection:
results = await connection.fetch("SELECT user_name, user_type_id FROM users")
return web.json_response([dict(result.items()) for result in results])
# @routes.get("/user/{user_name}")
# async def get_user(request):
# """ Get info on singular user """
# print(validate_request(request))
# user_name = request.match_info["user_name"]
# async with request.app["pg_pool"].acquire() as connection:
# # Creating a prepared statement where "$1" is replaced by first argument
# statement = await connection.prepare("SELECT user_name, user_type_id FROM users WHERE user_name = $1")
# result = await statement.fetchrow(user_name)
# if result:
# return web.json_response(dict(result.items()))
# else:
# raise web.HTTPUnprocessableEntity(text=f"User \"{user_name}\" not registered!")
@routes.post("/register")
async def post_user(request):
data = await request.json() # Use this like a dictionary
# Try to get the user_name value from the post data
try:
user_name, user_pass, user_type = data["user_name"], data["user_pass"], data["user_type"]
user_full_name = data["user_full_name"]
except KeyError:
raise web.HTTPUnprocessableEntity(text="Not all require parameters passed!")
try:
user_type_id = request.app["types"][user_type]
except KeyError:
raise web.HTTPUnprocessableEntity(text=f"Invalid user type '{user_type}' provided!")
password_salt = urandom(32)
password_hash = pbkdf2_hmac("sha256", user_pass.encode(), password_salt, 100000)
async with request.app["pg_pool"].acquire() as connection:
# Validate if the user already exists
validate_stmt = await connection.prepare("SELECT user_id FROM users WHERE user_name = $1")
# If the user is already registered
if await validate_stmt.fetchval(user_name):
raise web.HTTPUnprocessableEntity(text=f"User '{user_name}' already registered!")
await connection.execute("""
INSERT INTO users (user_type_id, user_name, user_full_name, user_hash, user_salt)
VALUES ($1, $2, $3, $4, $5)""",
user_type_id, user_name, user_full_name, password_hash, password_salt)
return web.Response(text=f"User '{user_name}' successfully registered!")
@routes.post("/authenticate")
async def authenticate_user(request):
data = await request.json()
try:
user_name, user_pass = data["user_name"], data["user_pass"]
except KeyError:
raise web.HTTPUnprocessableEntity(text="Not all keys provided!")
async with request.app["pg_pool"].acquire() as connection:
user_stmt = await connection.prepare("SELECT * FROM users NATURAL JOIN user_types WHERE user_name = $1")
user_result = await user_stmt.fetchrow(user_name)
if user_result is None:
raise web.HTTPUnprocessableEntity(text="Invalid credential provided!")
user_data = dict(user_result.items())
user_hash, user_salt = user_data["user_hash"], user_data["user_salt"]
password_hash = pbkdf2_hmac("sha256", user_pass.encode(), user_salt, 100000)
# If the hashes don't match
if password_hash != user_hash:
raise web.HTTPUnprocessableEntity(text="Invalid credential provided!")
private_key = request.app["private_key"]
current_datetime = datetime.utcnow()
expiration_delta = timedelta(minutes=20)
user_id = user_data["user_id"]
payload = {
'iss': "Agora VR",
'exp': current_datetime + expiration_delta,
'agora': {
'user_id': user_id,
'user_name': user_data["user_name"],
'user_type': user_data["user_type_name"],
},
}
token = jwt.encode({'alg': "RS256"}, payload, private_key)
request.app["tokens"][user_id] = token.decode()
return web.Response(body=token)
@routes.get("/user/clinicians")
async def get_clinicians(request):
session = validate_request(request)["agora"]
user_id, user_type = session["user_id"], session["user_type"]
if user_type not in request.app["types"] or user_type != "patient":
raise web.HTTPUnprocessableEntity(text="Client is not a valid user type for endpoint!")
async with request.app["pg_pool"].acquire() as connection:
clinician_stmt = await connection.prepare("""
SELECT user_id, user_name, user_full_name
FROM serves JOIN users ON users.user_id = serves.clinician_id
WHERE serves.patient_id = $1""")
results = await clinician_stmt.fetch(user_id)
return web.json_response([dict(result.items()) for result in results])
@routes.get("/user/patients")
async def get_patients(request):
session = validate_request(request)["agora"]
user_id, user_type = session["user_id"], session["user_type"]
if user_type not in request.app["types"] or user_type != "clinician":
raise web.HTTPUnprocessableEntity(text="Client is not a valid user type for this endpoint!")
async with request.app["pg_pool"].acquire() as connection:
clinician_stmt = await connection.prepare("""
SELECT user_id, user_name, user_full_name
FROM serves JOIN users ON users.user_id = serves.patient_id
WHERE serves.clinician_id = $1""")
results = await clinician_stmt.fetch(user_id)
return web.json_response([dict(result.items()) for result in results])
@routes.get("/user/register_form")
async def get_register_form(request):
session = validate_request(request)["agora"]
user_type = session["user_type"]
async with request.app["pg_pool"].acquire() as connection:
statement = await connection.prepare("""
SELECT forms.*
FROM user_types JOIN forms ON user_type_form_name = form_name
WHERE user_type_name = $1""")
result = await statement.fetchrow(user_type)
if result:
return web.json_response(dict(result.items()))
@routes.post("/user/register_form")
async def post_register_form(request):
session = validate_request(request)["agora"]
data = await request.json()
try:
form_name, form_data = data["name"], data["results"]
except KeyError:
raise web.HTTPUnprocessableEntity(text="Not all keys provided!")
async with request.app["pg_pool"].acquire() as connection:
await connection.execute("""
INSERT INTO responses (response_owner_id, response_form_name, response_datetime, response_data)
VALUES ($1, $2, $3, $4)""",
session["user_id"], form_name, datetime.utcnow(), form_data)
return web.Response(text="Form response registered successfully!")
if __name__ == "__main__":
app = web.Application()
app["public_key"], app["private_key"] = load_keys("EmotionComedian")
app["tokens"] = {}
app["types"] = {
"patient": 1,
"clinician": 2,
"caregiver": 3,
}
cors = aiohttp_cors.setup(app, defaults={
"http://localhost:5000": aiohttp_cors.ResourceOptions(
allow_credentials=False,
expose_headers="*",
allow_headers="*",
),
})
app.add_routes(routes)
for route in app.router.routes():
cors.add(route)
app.cleanup_ctx.append(setup_app)
web.run_app(app)