Implement token management and authentication features with logging

This commit is contained in:
Mabe
2026-08-30 21:42:21 +02:00
parent f697a0aca3
commit 83c288d490
7 changed files with 240 additions and 16 deletions
+15 -6
View File
@@ -1,9 +1,13 @@
from fastapi import APIRouter, HTTPException
from fastapi import APIRouter, HTTPException, Depends, Header
import logging
import db
import api_security as auth
from pydantic import BaseModel
from api import tokens
#Logging
logging.basicConfig(
@@ -42,8 +46,11 @@ class Item_weight(BaseModel):
class Item_piece(BaseModel):
pieces: int
class Item_duration(BaseModel):
duration: str
@router.post("/insert", status_code=201)
def insert(item_name: Item_name, item_weight: Item_weight, item_pieces: Item_piece):
def insert(item_name: Item_name, item_weight: Item_weight, item_pieces: Item_piece, token: str = Depends(tokens.check_token)):
name = item_name.name_of_clothes.upper()
weight = item_weight.weight
pieces = item_pieces.pieces
@@ -54,7 +61,7 @@ def insert(item_name: Item_name, item_weight: Item_weight, item_pieces: Item_pie
@router.put("/update/{clothe_name}/pieces", status_code=202)
def update_pieces(clothe_name: str, item_pieces: Item_piece):
def update_pieces(clothe_name: str, item_pieces: Item_piece, token: str = Depends(tokens.check_token)):
name = clothe_name.upper()
pieces = item_pieces.pieces
if db.check_clothe(name) != None:
@@ -68,7 +75,7 @@ def update_pieces(clothe_name: str, item_pieces: Item_piece):
@router.put("/update/{clothe_name}/weight", status_code=202)
def update_weight(clothe_name: str, item: Item_weight):
def update_weight(clothe_name: str, item: Item_weight, token: str = Depends(tokens.check_token)):
name = clothe_name.upper()
weight = item.weight
if db.check_clothe(name) != None:
@@ -82,7 +89,7 @@ def update_weight(clothe_name: str, item: Item_weight):
@router.delete("/{clothe_name}/delete", status_code=204)
def delete(clothe_name: str):
def delete(clothe_name: str, token: str = Depends(tokens.check_token)):
name = clothe_name.upper()
if db.check_clothe(name) != None:
db.delete(name)
@@ -90,4 +97,6 @@ def delete(clothe_name: str):
return {"message": "Item was deleted"}
else:
logging.error(f"Item {name} not found")
raise HTTPException(status_code=404, detail=f"Item {name} not found")
raise HTTPException(status_code=404, detail=f"Item {name} not found")
+60
View File
@@ -0,0 +1,60 @@
from fastapi import APIRouter, HTTPException, Depends, Header
import logging
import db
import api_security as auth
from pydantic import BaseModel
def check_token(token:str = Header(...)):
if not auth.check_token(token):
raise HTTPException(status_code=401, detail="Invalid token")
return token
def check_admin_token(admin_token: str = Header(...)):
if not auth.check_admin_token(admin_token):
raise HTTPException(status_code=401, detail="No admin token")
return admin_token
#Logging
logging.basicConfig(
filename='api.log', # Log to a file
level=logging.DEBUG, # Log everything (DEBUG and above)
format='%(asctime)s %(levelname)s %(message)s'
)
logging.info('Application started')
try:
logging.debug('Debug info here')
except Exception as e:
logging.exception('An error occurred')
router = APIRouter(prefix="/tokens", tags=["tokens"])
class Item_duration(BaseModel):
duration: str | int
class Item_token(BaseModel):
token: str
@router.get("/help")
def help():
return{"duration": "string or int"}
@router.post("/create")
def create_token(duration: Item_duration, admin_token: str = Depends(check_admin_token)):
new_token = auth.generate_token(duration.duration)
return {"new token": new_token}
@router.delete("/delete")
def delete(token: Item_token, admin_token: str = Depends(check_admin_token)):
auth.delete_token(token.token)
return {"messages": "token deleted"}
@router.get("/test")
def test(token: str = Depends(check_token)):
return {"message": "you can authenticate", "status": "ok"}