Fix jwt-token

This commit is contained in:
2025-09-07 11:15:59 +02:00
parent 991e601a35
commit 5f8938d5bb
4 changed files with 99 additions and 9 deletions

View File

@@ -10,10 +10,13 @@ API_BASE_URL=http://localhost:3002/api
# Base path (should match VITE_BASE_PATH)
VITE_BASE_PATH=/uggla
# Test authentication credentials
# Authentication configuration
TEST_USERNAME=admin
TEST_PASSWORD=admin123
# Skip authentication for local testing (set to 'true' to disable auth)
SKIP_AUTH=false
# Alternative configurations:
# For local development:
# API_BASE_URL=http://localhost:3002/api

View File

@@ -23,6 +23,11 @@ warnings.filterwarnings('ignore', message='Unverified HTTPS request')
API_BASE_URL = os.getenv('API_BASE_URL', 'http://localhost:3002/api')
BASE_PATH = os.getenv('VITE_BASE_PATH', '').rstrip('/')
# Authentication configuration - Optional for local testing
USERNAME = os.getenv('TEST_USERNAME', 'admin')
PASSWORD = os.getenv('TEST_PASSWORD', 'admin123')
SKIP_AUTH = os.getenv('SKIP_AUTH', 'false').lower() == 'true' # Set to 'true' to skip authentication
# If BASE_PATH is set, construct the full URL
if BASE_PATH and not API_BASE_URL.endswith('/api'):
# Extract domain from API_BASE_URL and add base path
@@ -34,6 +39,54 @@ print(f"🔗 Using API Base URL: {API_BASE_URL}")
# Debug configuration
DEBUG_MODE = False # Set to True to enable payload debugging
# Global variable to store authentication token
AUTH_TOKEN = None
def authenticate():
"""Authenticate with the API and get access token"""
global AUTH_TOKEN
if SKIP_AUTH:
return True
login_data = {
"username": USERNAME,
"password": PASSWORD
}
try:
print(f"🔐 Authenticating as user: {USERNAME}")
response = requests.post(f"{API_BASE_URL}/auth/login", json=login_data, verify=False)
if response.status_code == 200:
data = response.json()
AUTH_TOKEN = data.get('token')
if AUTH_TOKEN:
print("✅ Authentication successful")
return True
else:
print("❌ No token received in response")
return False
else:
print(f"❌ Authentication failed: {response.status_code}")
print(f"Response: {response.text}")
return False
except Exception as e:
print(f"❌ Authentication error: {e}")
return False
def get_auth_headers():
"""Get headers with authentication token"""
if SKIP_AUTH:
return {"Content-Type": "application/json"}
if AUTH_TOKEN:
return {
"Authorization": f"Bearer {AUTH_TOKEN}",
"Content-Type": "application/json"
}
return {"Content-Type": "application/json"}
# Global variable to store devices fetched from API
DEVICES = []
@@ -50,7 +103,12 @@ def fetch_devices():
"""Fetch active devices from the API"""
global DEVICES
try:
response = requests.get(f"{API_BASE_URL}/devices/map", verify=False)
if SKIP_AUTH:
# Try without authentication first for local testing
response = requests.get(f"{API_BASE_URL}/devices/map", verify=False)
else:
response = requests.get(f"{API_BASE_URL}/devices", headers=get_auth_headers(), verify=False)
if response.status_code == 200:
data = response.json()
api_devices = data.get('data', [])
@@ -274,7 +332,7 @@ class DroneSimulator:
def send_heartbeat(device_id):
"""Send heartbeat to keep device online"""
global DEBUG_MODE
url = f"{API_BASE_URL}/heartbeat"
url = f"{API_BASE_URL}/detectors"
headers = {
"Content-Type": "application/json"
}
@@ -314,7 +372,7 @@ def send_heartbeat(device_id):
def send_detection(detection_data):
"""Send detection data to the API"""
global DEBUG_MODE
url = f"{API_BASE_URL}/detections"
url = f"{API_BASE_URL}/detectors"
headers = {
"Content-Type": "application/json"
}
@@ -669,6 +727,19 @@ def main():
# Show current debug status
print(f"🐛 Debug mode status: {'ENABLED' if DEBUG_MODE else 'DISABLED'}")
if SKIP_AUTH:
print("⚠️ AUTHENTICATION DISABLED - Running in local test mode")
# Authenticate first (unless skipped)
if not SKIP_AUTH and not authenticate():
print("❌ Authentication failed. Cannot proceed with test.")
print("Please check:")
print("1. Is the server running?")
print("2. Are the credentials correct?")
print(f" Username: {USERNAME}")
print("3. Set TEST_USERNAME and TEST_PASSWORD environment variables if needed")
print("4. Or set SKIP_AUTH=true to skip authentication for local testing")
return
# Fetch devices from API first
print("📡 Fetching active devices from API...")

View File

@@ -27,9 +27,10 @@ warnings.filterwarnings('ignore', message='Unverified HTTPS request')
API_BASE_URL = os.getenv('API_BASE_URL', 'http://localhost:3002/api')
BASE_PATH = os.getenv('VITE_BASE_PATH', '').rstrip('/')
# Authentication configuration
# Authentication configuration - Optional for local testing
USERNAME = os.getenv('TEST_USERNAME', 'admin')
PASSWORD = os.getenv('TEST_PASSWORD', 'admin123')
SKIP_AUTH = os.getenv('SKIP_AUTH', 'false').lower() == 'true' # Set to 'true' to skip authentication
# If BASE_PATH is set, construct the full URL
if BASE_PATH and not API_BASE_URL.endswith('/api'):
@@ -74,6 +75,9 @@ def authenticate():
def get_auth_headers():
"""Get headers with authentication token"""
if SKIP_AUTH:
return {"Content-Type": "application/json"}
if AUTH_TOKEN:
return {
"Authorization": f"Bearer {AUTH_TOKEN}",
@@ -120,7 +124,12 @@ def is_detectable(distance_km):
def fetch_devices():
"""Fetch devices from API"""
try:
response = requests.get(f"{API_BASE_URL}/devices", headers=get_auth_headers(), verify=False)
if SKIP_AUTH:
# Try without authentication first for local testing
response = requests.get(f"{API_BASE_URL}/devices/map", verify=False)
else:
response = requests.get(f"{API_BASE_URL}/devices", headers=get_auth_headers(), verify=False)
if response.status_code == 200:
data = response.json()
return data.get('data', [])
@@ -181,16 +190,19 @@ def run_orlan_detection_test():
print("• End position: Directly overhead (0m)")
print("=" * 70)
print(f"🔗 API Endpoint: {API_BASE_URL}")
if SKIP_AUTH:
print("⚠️ AUTHENTICATION DISABLED - Running in local test mode")
print()
# Authenticate first
if not authenticate():
# Authenticate first (unless skipped)
if not SKIP_AUTH and not authenticate():
print("❌ Authentication failed. Cannot proceed with test.")
print("Please check:")
print("1. Is the server running?")
print("2. Are the credentials correct?")
print(f" Username: {USERNAME}")
print("3. Set TEST_USERNAME and TEST_PASSWORD environment variables if needed")
print("4. Or set SKIP_AUTH=true to skip authentication for local testing")
return
# Test API connectivity first

View File

@@ -21,9 +21,10 @@ warnings.filterwarnings('ignore', message='Unverified HTTPS request')
API_BASE_URL = os.getenv('API_BASE_URL', 'http://localhost:3002/api')
BASE_PATH = os.getenv('VITE_BASE_PATH', '').rstrip('/')
# Authentication configuration
# Authentication configuration - Optional for local testing
USERNAME = os.getenv('TEST_USERNAME', 'admin')
PASSWORD = os.getenv('TEST_PASSWORD', 'admin123')
SKIP_AUTH = os.getenv('SKIP_AUTH', 'false').lower() == 'true' # Set to 'true' to skip authentication
# If BASE_PATH is set, construct the full URL
if BASE_PATH and not API_BASE_URL.endswith('/api'):
@@ -68,6 +69,9 @@ def authenticate():
def get_auth_headers():
"""Get headers with authentication token"""
if SKIP_AUTH:
return {"Content-Type": "application/json"}
if AUTH_TOKEN:
return {
"Authorization": f"Bearer {AUTH_TOKEN}",