Spaces:
Sleeping
Sleeping
File size: 1,837 Bytes
f20b5f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import requests
import sys
BASE_URL = "http://localhost:7860/v1"
def debug_admin_auth():
print("Debugging Admin Auth...")
# 1. Login
login_data = {"username": "admin_test", "password": "password123"}
resp = requests.post(f"{BASE_URL}/admin/auth/login", json=login_data)
if resp.status_code != 200:
# Try registering if login fails
print("Login failed, trying registration...")
reg_data = {
"username": "admin_test",
"email": "admin@test.com",
"password": "password123",
"registration_secret": "change-this-secret-in-production"
}
requests.post(f"{BASE_URL}/admin/auth/register", json=reg_data)
resp = requests.post(f"{BASE_URL}/admin/auth/login", json=login_data)
if resp.status_code != 200:
print(f"FAILED: Could not login or register admin. Status: {resp.status_code}")
print(resp.text)
return
data = resp.json()
token = data["access_token"]
print(f"SUCCESS: Got token starting with: {token[:20]}...")
# 2. Call /me
headers = {"Authorization": f"Bearer {token}"}
print(f"Calling /me with headers: {headers}")
resp = requests.get(f"{BASE_URL}/admin/auth/me", headers=headers)
print(f"RESULT: Status {resp.status_code}")
print(f"BODY: {resp.text}")
if resp.status_code == 200:
print("✓ SUCCESS: /me authenticated correctly")
elif resp.status_code == 403:
print("✗ FAILURE: Got 403 'Not authenticated' (Header issue?)")
elif resp.status_code == 401:
print("✗ FAILURE: Got 401 'Invalid token' (JWT decode issue?)")
else:
print(f"✗ FAILURE: Got unexpected status {resp.status_code}")
if __name__ == "__main__":
debug_admin_auth()
|