-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
progress save, authentication server unfinished, urwid patch removed
- Loading branch information
Showing
6 changed files
with
96 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters