Implement basic auth for frontend
This commit is contained in:
70
app/internal/auth_wrappers.py
Normal file
70
app/internal/auth_wrappers.py
Normal file
@@ -0,0 +1,70 @@
|
||||
# internal/auth_wrappers.py
|
||||
import os
|
||||
import asyncio
|
||||
from uuid import uuid4
|
||||
from typing import Optional, List, Dict, Any
|
||||
from internal.db_handler import MongoHandler
|
||||
from internal.types import User, UserRoles
|
||||
|
||||
DB_NAME = os.getenv("DB_NAME", "default_db")
|
||||
MONGO_URL = os.getenv("MONGO_URL", "mongodb://10.10.202.4:27017/")
|
||||
|
||||
USER_DB_COLLECTION_NAME = "users"
|
||||
|
||||
class UserDbController:
|
||||
def __init__(self):
|
||||
self.db_h = MongoHandler(DB_NAME, USER_DB_COLLECTION_NAME, MONGO_URL)
|
||||
|
||||
async def create_user(self, user_data: Dict[str, Any]) -> Optional[User]:
|
||||
try:
|
||||
if not user_data.get("_id"):
|
||||
user_data['_id'] = str(uuid4())
|
||||
|
||||
inserted_id = None
|
||||
async with self.db_h as db:
|
||||
insert_result = await db.insert_one(user_data)
|
||||
inserted_id = insert_result.inserted_id
|
||||
|
||||
if inserted_id:
|
||||
query = {"_id": inserted_id}
|
||||
inserted_doc = None
|
||||
async with self.db_h as db:
|
||||
inserted_doc = await db.find_one(query)
|
||||
if inserted_doc:
|
||||
return User.from_dict(inserted_doc)
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"Create user failed: {e}")
|
||||
return None
|
||||
|
||||
async def find_user(self, query: Dict[str, Any]) -> Optional[User]:
|
||||
try:
|
||||
found_doc = None
|
||||
async with self.db_h as db:
|
||||
found_doc = await db.find_one(query)
|
||||
if found_doc:
|
||||
return User.from_dict(found_doc)
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"Find user failed: {e}")
|
||||
return None
|
||||
|
||||
async def update_user(self, query: Dict[str, Any], update_data: Dict[str, Any]) -> Optional[int]:
|
||||
try:
|
||||
update_result = None
|
||||
async with self.db_h as db:
|
||||
update_result = await db.update_one(query, update_data)
|
||||
return update_result.modified_count
|
||||
except Exception as e:
|
||||
print(f"Update user failed: {e}")
|
||||
return None
|
||||
|
||||
async def delete_user(self, query: Dict[str, Any]) -> Optional[int]:
|
||||
try:
|
||||
delete_result = None
|
||||
async with self.db_h as db:
|
||||
delete_result = await db.delete_one(query)
|
||||
return delete_result.deleted_count
|
||||
except Exception as e:
|
||||
print(f"Delete user failed: {e}")
|
||||
return None
|
||||
@@ -1,3 +1,4 @@
|
||||
# internal/types.py
|
||||
from typing import Optional, List, Dict, Any
|
||||
from enum import Enum
|
||||
|
||||
@@ -20,7 +21,7 @@ class TalkgroupTag:
|
||||
|
||||
# Add a method to convert to a dictionary, useful for sending as JSON
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {"talkgroup": self.talkgroup, "tagDec": self.tagDec}
|
||||
return {"talkgroup": self.talkgroup, "tagDec": self.talkgroup}
|
||||
|
||||
|
||||
class DiscordId:
|
||||
@@ -189,4 +190,48 @@ class ActiveClient:
|
||||
|
||||
def __init__(self, websocket= None, active_token:DiscordId=None):
|
||||
self.active_token = active_token
|
||||
self.websocket = websocket
|
||||
self.websocket = websocket
|
||||
|
||||
class UserRoles(str, Enum):
|
||||
ADMIN = "admin"
|
||||
MOD = "mod"
|
||||
USER = "user"
|
||||
|
||||
class User:
|
||||
"""
|
||||
A data model for a User entry.
|
||||
"""
|
||||
def __init__(self,
|
||||
_id: str,
|
||||
username: str,
|
||||
password_hash: str,
|
||||
role: UserRoles,
|
||||
api_key: Optional[str] = None):
|
||||
self._id: str = str(_id)
|
||||
self.username: str = username
|
||||
self.password_hash: str = password_hash
|
||||
self.role: UserRoles = role
|
||||
self.api_key: Optional[str] = api_key
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (f"User(_id='{self._id}', username='{self.username}', role='{self.role.value}', "
|
||||
f"api_key={'<hidden>' if self.api_key else 'None'})")
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"_id": self._id,
|
||||
"username": self.username,
|
||||
"password_hash": self.password_hash,
|
||||
"role": self.role.value,
|
||||
"api_key": self.api_key,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "User":
|
||||
return cls(
|
||||
_id=data.get("_id"),
|
||||
username=data.get("username", ""),
|
||||
password_hash=data.get("password_hash", ""),
|
||||
role=UserRoles(data.get("role", UserRoles.USER.value)),
|
||||
api_key=data.get("api_key", None),
|
||||
)
|
||||
Reference in New Issue
Block a user