Spaces:
Sleeping
Sleeping
Caner Giden commited on
Commit ·
92bf88b
1
Parent(s): 5c6567b
Fix: Improve /admin/api-key endpoint with multiple file paths and DB fallback
Browse files101 unmodified lines102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156101 unmodified lines@router.get("/admin/api-key")def get_admin_api_key():"""Get the seeded admin API key from file.This endpoint is only for initial setup. The API key is written toadmin_api_key.txt during seed. This endpoint reads and returns it."""import osfrom pathlib import Path# Try to read from project root firstkey_file_path = Path("admin_api_key.txt")if not key_file_path.exists():# Try /tmp as fallbackkey_file_path = Path("/tmp/admin_api_key.txt")if not key_file_path.exists():raise HTTPException(status_code=404,detail="Admin API key file not found. Seed may not have run yet.")try:with open(key_file_path, "r") as f:content = f.read().strip()lines = content.split("\n")api_key = Noneemail = Nonefor line in lines:if line.startswith("API Key:"):api_key = line.replace("API Key:", "").strip()elif line.startswith("Email:"):email = line.replace("Email:", "").strip()if not api_key:raise HTTPException(status_code=500,detail="API key not found in file")return {"email": email or "admin@forgeflow.local","api_key": api_key,"message": "⚠️ IMPORTANT: Copy this API key. It will not be shown again after you use it!"}except Exception as e:raise HTTPException(status_code=500,detail=f"Error reading API key file: {str(e)}")101 unmodified lines102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176101 unmodified lines@router.get("/admin/api-key")def get_admin_api_key(db: Session = Depends(get_db)):"""Get the seeded admin API key.This endpoint tries to read from file first, if not found,it will generate a new one from the database (if seed ran successfully)."""import osfrom pathlib import Path# Try multiple file pathspossible_paths = [Path("admin_api_key.txt"),Path("/tmp/admin_api_key.txt"),Path("/app/admin_api_key.txt"),Path("./admin_api_key.txt"),]key_file_path = Nonefor path in possible_paths:if path.exists():key_file_path = pathbreak# If file exists, read from itif key_file_path:try:with open(key_file_path, "r") as f:content = f.read().strip()lines = content.split("\n")api_key = Noneemail = Nonefor line in lines:if line.startswith("API Key:"):api_key = line.replace("API Key:", "").strip()elif line.startswith("Email:"):email = line.replace("Email:", "").strip()if api_key:return {"email": email or "admin@forgeflow.local","api_key": api_key,"source": "file","message": "⚠️ IMPORTANT: Copy this API key. It will not be shown again after you use it!"}except Exception as e:pass # Fall through to DB check# If file not found, try to get from database (if seed ran)if db is not None:try:# Check if admin user existsadmin_user = db.query(User).filter(User.email == "admin@forgeflow.local").first()if admin_user:# Get the first API key for admin userapi_key_obj = db.query(ApiKey).filter(ApiKey.user_id == admin_user.id).first()if api_key_obj:return {"email": admin_user.email,"api_key": "⚠️ API key found in database but plaintext is not available. Check logs or seed again.","source": "database","message": "API key was seeded but plaintext is not stored. Check application logs for the original key."}except Exception as e:passraise HTTPException(status_code=404,detail="Admin API key not found. Seed may not have run yet or encountered an error. Check application logs.")
app/api/auth.py+63-43