feat: use of params for scanners

This commit is contained in:
dextmorgn
2025-07-20 22:39:11 +02:00
parent 41165a67f5
commit c2c7e7abb7
75 changed files with 2374 additions and 781 deletions

View File

@@ -2,53 +2,56 @@ from uuid import UUID, uuid4
from fastapi import APIRouter, HTTPException, Depends, status
from typing import List
from sqlalchemy.orm import Session
from pydantic import BaseModel
from app.core.postgre_db import get_db
from app.models.models import Profile, ThirdPartyKey
from app.models.models import Profile, Key
from app.api.deps import get_current_user
from app.api.schemas.key import ThirdPartyKeyRead, ThirdPartyKeyCreate
from app.api.schemas.key import KeyRead, KeyCreate
from datetime import datetime
router = APIRouter()
class ServiceInfo(BaseModel):
service: str
variable: str
url: str
active: bool
# Get the list of all services that require a key
@router.get("/services", response_model=List[ServiceInfo])
def get_services(db: Session = Depends(get_db)):
services = [
ServiceInfo(service="Mistral AI", variable="MISTRAL_API_KEY", url="https://mistral.ai/", active=True),
ServiceInfo(service="Etherscan", variable="ETHERSCAN_API_KEY", url="https://etherscan.io/", active=True),
ServiceInfo(service="HaveIBeenPwned", variable="HIBP_API_KEY", url="https://haveibeenpwned.com/", active=True),
ServiceInfo(service="Onyphe", variable="ONYPHE_API_KEY", url="https://www.onyphe.io/", active=False),
ServiceInfo(service="Shodan", variable="SHODAN_API_KEY", url="https://www.shodan.io/", active=False)
]
return services
def obfuscate_key(key: str) -> str:
"""Obfuscate a key by showing only the last 4 characters, replacing others with asterisks."""
if len(key) <= 4:
return key
return "*" * (len(key) - 4) + key[-4:]
# Get the list of all keys for a user
@router.get("", response_model=List[ThirdPartyKeyRead])
@router.get("", response_model=List[KeyRead])
def get_keys(db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)):
keys = db.query(ThirdPartyKey).filter(ThirdPartyKey.owner_id == current_user.id).all()
return keys
keys = db.query(Key).filter(Key.owner_id == current_user.id).all()
response_data = [KeyRead(
id=key.id,
owner_id=key.owner_id,
encrypted_key=obfuscate_key(key.encrypted_key),
name=key.name,
created_at=key.created_at
) for key in keys]
return response_data
# Get a key by ID
@router.get("/{id}", response_model=ThirdPartyKeyRead)
@router.get("/{id}", response_model=KeyRead)
def get_key_by_id(id: UUID, db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)):
key = db.query(ThirdPartyKey).filter(ThirdPartyKey.id == id, ThirdPartyKey.owner_id == current_user.id).first()
key = db.query(Key).filter(Key.id == id, Key.owner_id == current_user.id).first()
if not key:
raise HTTPException(status_code=404, detail="Key not found")
return key
# Create a response with obfuscated key
response_data = KeyRead(
id=key.id,
owner_id=key.owner_id,
encrypted_key=obfuscate_key(key.encrypted_key),
name=key.name,
created_at=key.created_at
)
return response_data
# Create a new key
@router.post("/create", response_model=ThirdPartyKeyRead, status_code=status.HTTP_201_CREATED)
def create_key(payload: ThirdPartyKeyCreate, db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)):
new_key = ThirdPartyKey(
@router.post("/create", response_model=KeyRead, status_code=status.HTTP_201_CREATED)
def create_key(payload: KeyCreate, db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)):
new_key = Key(
id=uuid4(),
service=payload.service,
name=payload.name,
owner_id=current_user.id,
encrypted_key=payload.key,
created_at=datetime.utcnow(),
@@ -61,9 +64,9 @@ def create_key(payload: ThirdPartyKeyCreate, db: Session = Depends(get_db), curr
# Delete a key by ID
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_key(id: UUID, db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)):
key = db.query(ThirdPartyKey).filter(ThirdPartyKey.id == id, ThirdPartyKey.owner_id == current_user.id).first()
key = db.query(Key).filter(Key.id == id, Key.owner_id == current_user.id).first()
if not key:
raise HTTPException(status_code=404, detail="Key not found")
db.delete(key)
db.commit()
return None
return None