#!/usr/bin/env python3 """ Direct fix for Web UI authentication issue """ import requests import os import subprocess import time def check_current_server(): """Check current server status and configuration""" print("=== CHECKING CURRENT SERVER ===") try: response = requests.get("http://localhost:3015/health") if response.status_code == 200: health_data = response.json() print("✅ Server is running") print(f"Auth mode: {health_data.get('auth_mode', 'Unknown')}") print(f"Auth configured: {health_data.get('auth_configured', 'Unknown')}") return health_data else: print(f"❌ Server health check failed: {response.status_code}") return None except Exception as e: print(f"❌ Server not accessible: {e}") return None def stop_current_servers(): """Stop any running LightRAG servers""" print("\n=== STOPPING CURRENT SERVERS ===") try: # Try to stop via taskkill subprocess.run(["taskkill", "/f", "/im", "python.exe"], capture_output=True) print("✅ Stopped Python processes") time.sleep(2) return True except Exception as e: print(f"❌ Error stopping servers: {e}") return False def start_server_with_auth(): """Start server with proper authentication""" print("\n=== STARTING SERVER WITH AUTHENTICATION ===") # Set environment variables env = os.environ.copy() env.update({ "OPENAI_API_KEY": "sk-55f6e57f1d834b0e93ceaf98cc2cb715", "OPENAI_BASE_URL": "https://api.deepseek.com/v1", "LLM_MODEL": "deepseek-chat", "OLLAMA_EMBEDDING_MODEL": "snowflake-arctic-embed:latest", "OLLAMA_RERANKER_MODEL": "jina-reranker:latest", "PYTHONIOENCODING": "utf-8", "API_KEY": "jleu1212", "AUTH_ACCOUNTS": "jleu3482:jleu1212", "REDIS_URI": "redis://localhost:6379", "NEO4J_URI": "bolt://localhost:7687", "NEO4J_USERNAME": "neo4j", "NEO4J_PASSWORD": "jleu1212", "QDRANT_URI": "http://localhost:6333/", "POSTGRES_URI": "postgresql://jleu3482:jleu1212@localhost:5432/rag_anything" }) # Start server directly with environment variables cmd = [ "python", "-m", "lightrag.api.lightrag_server", "--port", "3015", "--working-dir", "rag_storage", "--input-dir", "inputs", "--key", "jleu1212", "--auto-scan-at-startup", "--llm-binding", "openai", "--embedding-binding", "ollama", "--rerank-binding", "jina" ] try: print("Starting server with authentication...") process = subprocess.Popen( cmd, cwd="LightRAG-main", env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) # Wait for server to start print("⏳ Waiting for server to start...") time.sleep(10) # Check if server is running try: response = requests.get("http://localhost:3015/health", timeout=5) if response.status_code == 200: print("✅ Server started successfully!") return process else: print(f"❌ Server not responding properly: {response.status_code}") return None except: print("❌ Server failed to start within timeout") return None except Exception as e: print(f"❌ Error starting server: {e}") return None def test_authentication(): """Test if authentication is working""" print("\n=== TESTING AUTHENTICATION ===") # Test without auth try: response = requests.get("http://localhost:3015/webui/") print(f"Web UI without auth: {response.status_code}") if response.status_code == 401: print("✅ Authentication required!") return True else: print("❌ Authentication not required") return False except Exception as e: print(f"Error testing auth: {e}") return False def create_fixed_batch_file(): """Create a fixed batch file that properly sets environment""" print("\n=== CREATING FIXED BATCH FILE ===") batch_content = """@echo off echo Starting LightRAG Production Server with Authentication... echo. echo Setting environment variables... set OPENAI_API_KEY=sk-55f6e57f1d834b0e93ceaf98cc2cb715 set OPENAI_BASE_URL=https://api.deepseek.com/v1 set LLM_MODEL=deepseek-chat set OLLAMA_EMBEDDING_MODEL=snowflake-arctic-embed:latest set OLLAMA_RERANKER_MODEL=jina-reranker:latest set PYTHONIOENCODING=utf-8 echo Setting authentication environment variables... set API_KEY=jleu1212 set AUTH_ACCOUNTS=jleu3482:jleu1212 echo Setting database environment variables... set REDIS_URI=redis://localhost:6379 set NEO4J_URI=bolt://localhost:7687 set NEO4J_USERNAME=neo4j set NEO4J_PASSWORD=jleu1212 set QDRANT_URI=http://localhost:6333/ set POSTGRES_URI=postgresql://jleu3482:jleu1212@localhost:5432/rag_anything echo Starting LightRAG server on port 3015... python -m lightrag.api.lightrag_server --port 3015 --working-dir rag_storage --input-dir inputs --key jleu1212 --auto-scan-at-startup --llm-binding openai --embedding-binding ollama --rerank-binding jina pause """ try: with open("LightRAG-main/zrun_fixed.bat", "w") as f: f.write(batch_content) print("✅ Fixed batch file created: zrun_fixed.bat") return True except Exception as e: print(f"❌ Error creating batch file: {e}") return False def main(): print("Direct Web UI Authentication Fix") print("=" * 60) # Check current server health_data = check_current_server() if health_data and health_data.get('auth_mode') == 'enabled': print("\n⚠️ Server shows auth is enabled but it's not working") print("This suggests the environment variables aren't being applied") # Stop current servers if not stop_current_servers(): print("❌ Could not stop current servers") return # Create fixed batch file create_fixed_batch_file() # Start server with proper authentication process = start_server_with_auth() if process: # Test authentication if test_authentication(): print("\n🎉 AUTHENTICATION FIXED SUCCESSFULLY! 🎉") print("\n✅ Web UI now requires authentication") print("✅ You can now login with: jleu3482 / jleu1212") print("✅ Browser will properly prompt for credentials") else: print("\n❌ Authentication still not working") print("This might be a deeper configuration issue") else: print("\n❌ Failed to start server with authentication") print("\nNext steps:") print("1. Use the new batch file: LightRAG-main\\zrun_fixed.bat") print("2. Clear browser cache and try logging in again") print("3. Visit: http://localhost:3015/webui/") print("4. Use credentials: jleu3482 / jleu1212") if __name__ == "__main__": main()