multi-use-api / app /routers /security /password_generator.py
sameerbanchhor's picture
Upload folder using huggingface_hub
438ec1c verified
import random
import string
from fastapi import APIRouter, Query, Depends # Added 'Depends'
from app.routers.auth.system import get_current_user # Import our Auth System
# Define the APIRouter object as per documentation standards
router = APIRouter(
prefix="/security", # Endpoints will start with /security
tags=["Password Tools"],
)
@router.get("/generate-password")
def generate_password(
length: int = Query(12, ge=8, le=64, description="Length of the password (8-64)"),
include_uppercase: bool = True,
include_numbers: bool = True,
include_symbols: bool = True,
# πŸ‘‡ This line locks the endpoint!
current_user: dict = Depends(get_current_user)
):
"""
Generates a secure, random password.
**Requires Authentication.**
"""
# Base character set
chars = string.ascii_lowercase
# Advanced logic to append character sets based on flags
if include_uppercase:
chars += string.ascii_uppercase
if include_numbers:
chars += string.digits
if include_symbols:
chars += string.punctuation
# Generate the password
generated_password = "".join(random.choice(chars) for _ in range(length))
return {
"requested_by": current_user.username, # We can now see who asked for it!
"password": generated_password,
"length": length,
"complexity": "Advanced"
}