-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication_server.py
84 lines (53 loc) · 2.27 KB
/
authentication_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
from json import dumps
from aiohttp import web
from authlib.jose import jwt
from datetime import datetime, timedelta
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import load_pem_private_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
routes = web.RouteTableDef()
@routes.post("/api/register")
async def register_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!")
users = request.app["user_pass"]
if user_name in users:
raise web.HTTPUnprocessableEntity(text=f"User {user_name} already exists!")
users[user_name] = user_pass
return web.Response(text=f"User {user_name} successfully registered!")
@routes.post("/api/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!")
users = request.app["user_pass"]
if user_name in users and users[user_name] == user_pass:
private_key = request.app["private_key"]
current_datetime = datetime.utcnow()
expiration_delta = timedelta(seconds=15)
payload = {'iss': 'Project Agora', 'exp': current_datetime + expiration_delta}
header = {'alg': 'RS256'}
return web.Response(text=jwt.encode(header, payload, private_key).decode())
else:
raise web.HTTPUnprocessableEntity(text="Invalid username/password combination")
if __name__ == "__main__":
app = web.Application()
app["user_pass"] = {}
app["user_token"] = {}
# Change the string argument to your key pair's password
app["public_key"], app["private_key"] = load_keys("ButgersBuses")
app.add_routes(routes)
# app.add_routes([web.static("/", "static")])
# app.cleanup_ctx.append(setup_app)
web.run_app(app)