Added auth module, just a simple token at this stage

This commit is contained in:
Cyber MacGeddon 2024-12-02 19:49:15 +00:00
parent 6d200c79c5
commit c3fbee4a44
17 changed files with 121 additions and 33 deletions

View file

@ -11,11 +11,12 @@ logger.setLevel(logging.INFO)
class SocketEndpoint:
def __init__(
self,
endpoint_path="/api/v1/socket",
self, endpoint_path, auth,
):
self.path = endpoint_path
self.auth = auth
self.operation = "socket"
async def listener(self, ws, running):
@ -43,18 +44,33 @@ class SocketEndpoint:
async def handle(self, request):
try:
token = request.query['token']
except:
return web.HTTPUnauthorized()
if not self.auth.permitted(token, self.operation):
return web.HTTPUnauthorized()
running = Running()
ws = web.WebSocketResponse()
await ws.prepare(request)
task = asyncio.create_task(self.async_thread(ws, running))
await self.listener(ws, running)
try:
await task
await self.listener(ws, running)
except Exception as e:
print(e, flush=True)
running.stop()
await ws.close()
await task
return ws
async def start(self):