From 9c1ae6279d0f03da0901b253d64cf61f73948d3f Mon Sep 17 00:00:00 2001 From: iiPython Date: Wed, 17 Jul 2024 22:24:32 -0500 Subject: [PATCH] progress save, authentication server unfinished, urwid patch removed --- nightwatch/__main__.py | 16 ------------- nightwatch/auth/__init__.py | 48 +++++++++++++++++++++++++++++++++++++ nightwatch/auth/__main__.py | 0 nightwatch/auth/models.py | 13 ++++++++++ nightwatch/logging.py | 27 +++++++++++++++++++++ pyproject.toml | 17 +++++++------ 6 files changed, 96 insertions(+), 25 deletions(-) create mode 100644 nightwatch/auth/__init__.py create mode 100644 nightwatch/auth/__main__.py create mode 100644 nightwatch/auth/models.py create mode 100644 nightwatch/logging.py diff --git a/nightwatch/__main__.py b/nightwatch/__main__.py index 1fa4c3b..ec5f8ca 100644 --- a/nightwatch/__main__.py +++ b/nightwatch/__main__.py @@ -2,22 +2,6 @@ # Initialization def main() -> None: - - # Check urwid - def invalid_urwid_distrib(): - exit("ERROR: Nightwatch requires urwid to have a specific unicode patch not currently included in upstream urwid.\n\n" \ - "To fix this, run:\npip install -U urwid@git+https://github.com/iiPythonx/urwid\n\n" \ - "and relaunch Nightwatch.") - - try: - from urwid import is_iipython_urwid - if not is_iipython_urwid: - invalid_urwid_distrib() - - except ImportError: - invalid_urwid_distrib() - - # Modules from argparse import ArgumentParser from nightwatch.client import start_client diff --git a/nightwatch/auth/__init__.py b/nightwatch/auth/__init__.py new file mode 100644 index 0000000..196ecc8 --- /dev/null +++ b/nightwatch/auth/__init__.py @@ -0,0 +1,48 @@ +# Copyright (c) 2024 iiPython + +# Modules +from fastapi import FastAPI + +import argon2 +from fastapi.responses import JSONResponse +from pymongo import MongoClient + +from nightwatch.config import config +from nightwatch.logging import log + +from . import models + +# Initialization +class AuthenticationServer(FastAPI): + def __init__(self) -> None: + super().__init__() + + # Initialize Argon2 + self.hasher = argon2.PasswordHasher() + + # Establish MongoDB connection + mongo: MongoClient = MongoClient( + host = config["server.connections.mongodb"], + serverSelectionTimeoutMS = 5000 + ) + mongo.admin.command("ping") + + # Connect to Nightwatch / Authentication to avoid pollution + self.db = mongo.nightwatch.auth + +app: AuthenticationServer = AuthenticationServer() + +# Routing +@app.post(path = "/api/login") +async def route_api_login(payload: models.LoginModel) -> JSONResponse: + response: dict | None = app.db.find_one(filter = {"username": payload.username}) + if response is None: + return JSONResponse( + content = {"code": 400, "data": "No account with that username exists."}, + status_code = 400 + ) + + # Check password + + + return JSONResponse(content = {"code": 200, "data": "Logging in isn't a thing yet bud."}) diff --git a/nightwatch/auth/__main__.py b/nightwatch/auth/__main__.py new file mode 100644 index 0000000..e69de29 diff --git a/nightwatch/auth/models.py b/nightwatch/auth/models.py new file mode 100644 index 0000000..af52d54 --- /dev/null +++ b/nightwatch/auth/models.py @@ -0,0 +1,13 @@ +# Copyright (c) 2024 iiPython + +# Modules +from typing import Annotated +from pydantic import BaseModel, HttpUrl, StringConstraints + +# Models +class BaseAuthenticationModel(BaseModel): + username: Annotated[str, StringConstraints(min_length = 4, max_length = 36)] + password: Annotated[str, StringConstraints(min_length = 8, max_length = 512)] + +class LoginModel(BaseAuthenticationModel): + server: HttpUrl diff --git a/nightwatch/logging.py b/nightwatch/logging.py new file mode 100644 index 0000000..082f397 --- /dev/null +++ b/nightwatch/logging.py @@ -0,0 +1,27 @@ +# Copyright (c) 2024 iiPython + +# Initialization +class NightwatchLogger(): + def __init__(self) -> None: + self._color_map = { + "info": "34", "warn": "33", "error": "31", "critical": "31" + } + + def log(self, level: str, component: str, message: str) -> None: + print(f"\033[{self._color_map[level]}m⚡︎ {level.upper()} ({component}): {message}\033[0m") + + # Submethods + def info(self, *args) -> None: + self.log("info", *args) + + def warn(self, *args) -> None: + self.log("warn", *args) + + def error(self, *args) -> None: + self.log("error", *args) + + def critical(self, *args) -> None: + self.log("critical", *args) + exit(1) + +log = NightwatchLogger() diff --git a/pyproject.toml b/pyproject.toml index 5bee738..e87f785 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,20 +27,19 @@ classifiers = [ "Operating System :: OS Independent", ] dependencies = [ - "websockets", + "urwid", "orjson", - "urwid", # NEEDED: urwid@git+https://github.com/iiPythonx/urwid - "nanoid" + "nanoid", + "websockets" ] [project.optional-dependencies] -dev = [ - "hatch", - "build" -] serve = [ - "ujson", - "socketify" + "fastapi", + "pymongo", + "argon2-cffi", + "redis[hiredis]", + "uvicorn[standard]" ] [project.urls]