Skip to content

Commit

Permalink
progress save, authentication server unfinished, urwid patch removed
Browse files Browse the repository at this point in the history
  • Loading branch information
iiPythonx committed Jul 18, 2024
1 parent 615aa95 commit 9c1ae62
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 25 deletions.
16 changes: 0 additions & 16 deletions nightwatch/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 48 additions & 0 deletions nightwatch/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -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."})
Empty file added nightwatch/auth/__main__.py
Empty file.
13 changes: 13 additions & 0 deletions nightwatch/auth/models.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions nightwatch/logging.py
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 8 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 9c1ae62

Please sign in to comment.