Draft Login

This commit is contained in:
Kai Vogelgesang 2020-04-17 00:41:28 +02:00
parent b5cee96d7a
commit b736a0b65c
3 changed files with 26 additions and 5 deletions

View File

@ -33,9 +33,9 @@
</head> </head>
<body> <body>
<div id="container"></div>
this is the base site this is the base site
</div>
<a href="/newclient">Log in</a>
</body> </body>
</html> </html>

16
main.py
View File

@ -15,9 +15,15 @@ def get_client(request: aiohttp.web.Request):
client = request.match_info.get("client", None) client = request.match_info.get("client", None)
model = request.app['model'] model = request.app['model']
if not client or not model.exists_client(client): if not client:
print('[get_client] client is not set, wtf')
raise aiohttp.web.HTTPBadRequest() raise aiohttp.web.HTTPBadRequest()
if not model.exists_client(client):
print(f'[get_client] model does not know {client=}')
raise aiohttp.web.HTTPBadRequest()
return client return client
@ -86,6 +92,14 @@ async def handler(request):
return aiohttp.web.FileResponse('index.html') return aiohttp.web.FileResponse('index.html')
@routes.get('/newclient')
async def handler(request):
model = request.app['model']
client = model.create_client()
raise aiohttp.web.HTTPFound(f"/{client}/")
if __name__ == '__main__': if __name__ == '__main__':
app = aiohttp.web.Application() app = aiohttp.web.Application()
app.add_routes(routes) app.add_routes(routes)

View File

@ -5,6 +5,13 @@ import os
import datetime import datetime
def generate_random_id(_s=set()):
while (new_id := base64.b32encode(bytearray(random.randint(0, 0xFF) for _ in range(10)))[:16].decode().lower()) in _s:
pass
_s.add(new_id)
return new_id
class Model: class Model:
def __init__(self, filename = "tehmodel.json"): def __init__(self, filename = "tehmodel.json"):
self.sockets = {} # mapping: client -> socket self.sockets = {} # mapping: client -> socket
@ -48,13 +55,13 @@ class Model:
return clientid in self.model["clients"] return clientid in self.model["clients"]
def create_client(self) -> str: def create_client(self) -> str:
clientname = base64.b32encode(bytearray(random.randint(0, 0xFF) for _ in range(10)))[:16].decode().lower() clientname = generate_random_id()
newclient = {"id": clientname} newclient = {"id": clientname}
self.model["clients"][clientname] = newclient self.model["clients"][clientname] = newclient
return clientname return clientname
def create_session(self) -> str: def create_session(self) -> str:
sessionname = base64.b32encode(bytearray(random.randint(0, 0xFF) for _ in range(10)))[:16].decode().lower() sessionname = generate_random_id()
newsession = {"id": sessionname, "clients": []} newsession = {"id": sessionname, "clients": []}
self.model["sessions"][sessionname] = newsession self.model["sessions"][sessionname] = newsession
return sessionname return sessionname