|
|
|
|
|
|
|
|
""" |
|
|
Firebase Authentication Test Script |
|
|
Tests sign-in and API access with Firebase ID tokens |
|
|
""" |
|
|
|
|
|
import requests |
|
|
import json |
|
|
import sys |
|
|
|
|
|
|
|
|
if sys.platform == 'win32': |
|
|
import codecs |
|
|
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict') |
|
|
sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict') |
|
|
|
|
|
|
|
|
FIREBASE_WEB_API_KEY = "AIzaSyCPTqGkHtQmSkJiEjRHxmylBXJQGGEErG0" |
|
|
FIREBASE_AUTH_URL = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={FIREBASE_WEB_API_KEY}" |
|
|
|
|
|
|
|
|
API_BASE_URL = "https://logicgoinfotechspaces-face-swap-video.hf.space" |
|
|
|
|
|
|
|
|
TEST_EMAIL = "itisha.logicgo@gmail.com" |
|
|
TEST_PASSWORD = "mBQ2KjjlioRbK7FFCUcbvGWqTf62" |
|
|
|
|
|
def sign_in_with_email_password(email: str, password: str): |
|
|
"""Sign in with Firebase Auth and get ID token""" |
|
|
print(f"\n{'='*60}") |
|
|
print("Signing in to Firebase...") |
|
|
print(f"Email: {email}") |
|
|
|
|
|
payload = { |
|
|
"email": email, |
|
|
"password": password, |
|
|
"returnSecureToken": True |
|
|
} |
|
|
|
|
|
try: |
|
|
response = requests.post(FIREBASE_AUTH_URL, json=payload) |
|
|
response.raise_for_status() |
|
|
data = response.json() |
|
|
|
|
|
if "idToken" in data: |
|
|
print("β
Sign-in successful!") |
|
|
print(f"User ID: {data.get('localId')}") |
|
|
print(f"Email: {data.get('email')}") |
|
|
print(f"ID Token (first 50 chars): {data['idToken'][:50]}...") |
|
|
return data["idToken"] |
|
|
else: |
|
|
print(f"β Sign-in failed: {data}") |
|
|
return None |
|
|
|
|
|
except requests.exceptions.HTTPError as e: |
|
|
error_data = e.response.json() if e.response.text else {} |
|
|
error_message = error_data.get("error", {}).get("message", str(e)) |
|
|
print(f"β Sign-in error: {error_message}") |
|
|
|
|
|
if "INVALID_PASSWORD" in error_message or "EMAIL_NOT_FOUND" in error_message: |
|
|
print("\nπ‘ Note: If this is a new account, you may need to:") |
|
|
print(" 1. Create the user in Firebase Console β Authentication β Users") |
|
|
print(" 2. Or use signUpWithPassword endpoint first") |
|
|
|
|
|
return None |
|
|
except Exception as e: |
|
|
print(f"β Error: {str(e)}") |
|
|
return None |
|
|
|
|
|
def test_api_with_token(id_token: str): |
|
|
"""Test API endpoints with Firebase ID token""" |
|
|
headers = { |
|
|
"Authorization": f"Bearer {id_token}" |
|
|
} |
|
|
|
|
|
endpoints = [ |
|
|
("GET", "/api/me", "Get current user info"), |
|
|
("GET", "/api/health", "Health check"), |
|
|
("GET", "/api/source-images", "List source images"), |
|
|
("GET", "/api/target-videos", "List target videos"), |
|
|
] |
|
|
|
|
|
print(f"\n{'='*60}") |
|
|
print("Testing API Endpoints with Firebase ID Token") |
|
|
print(f"{'='*60}") |
|
|
|
|
|
for method, endpoint, description in endpoints: |
|
|
print(f"\n[TEST] {description}") |
|
|
print(f" {method} {endpoint}") |
|
|
|
|
|
try: |
|
|
url = f"{API_BASE_URL}{endpoint}" |
|
|
response = requests.request(method, url, headers=headers, timeout=10) |
|
|
|
|
|
print(f" Status: {response.status_code}") |
|
|
|
|
|
if response.status_code == 200: |
|
|
try: |
|
|
data = response.json() |
|
|
print(f" β
Success!") |
|
|
if endpoint == "/api/me": |
|
|
print(f" Auth Mode: {data.get('auth_mode')}") |
|
|
if data.get('auth_mode') == 'firebase': |
|
|
print(f" User ID: {data.get('user_id')}") |
|
|
print(f" Email: {data.get('email')}") |
|
|
except: |
|
|
print(f" β
Success (non-JSON response)") |
|
|
else: |
|
|
print(f" β Failed") |
|
|
try: |
|
|
error = response.json() |
|
|
print(f" Error: {error.get('detail', 'Unknown error')}") |
|
|
except: |
|
|
print(f" Error: {response.text[:100]}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f" β Exception: {str(e)}") |
|
|
|
|
|
def test_sign_up(email: str, password: str): |
|
|
"""Try to sign up a new user (if needed)""" |
|
|
print(f"\n{'='*60}") |
|
|
print("Attempting to sign up (if user doesn't exist)...") |
|
|
|
|
|
signup_url = f"https://identitytoolkit.googleapis.com/v1/accounts:signUp?key={FIREBASE_WEB_API_KEY}" |
|
|
payload = { |
|
|
"email": email, |
|
|
"password": password, |
|
|
"returnSecureToken": True |
|
|
} |
|
|
|
|
|
try: |
|
|
response = requests.post(signup_url, json=payload) |
|
|
response.raise_for_status() |
|
|
data = response.json() |
|
|
|
|
|
if "idToken" in data: |
|
|
print("β
Sign-up successful!") |
|
|
print(f"User ID: {data.get('localId')}") |
|
|
return data["idToken"] |
|
|
else: |
|
|
print(f"β Sign-up failed: {data}") |
|
|
return None |
|
|
except requests.exceptions.HTTPError as e: |
|
|
error_data = e.response.json() if e.response.text else {} |
|
|
error_message = error_data.get("error", {}).get("message", str(e)) |
|
|
print(f"β Sign-up error: {error_message}") |
|
|
|
|
|
if "EMAIL_EXISTS" in error_message: |
|
|
print("βΉοΈ User already exists, proceeding with sign-in...") |
|
|
return None |
|
|
return None |
|
|
except Exception as e: |
|
|
print(f"β Error: {str(e)}") |
|
|
return None |
|
|
|
|
|
def main(): |
|
|
print("="*60) |
|
|
print("Firebase Authentication Test Script") |
|
|
print("="*60) |
|
|
|
|
|
|
|
|
id_token = sign_in_with_email_password(TEST_EMAIL, TEST_PASSWORD) |
|
|
|
|
|
|
|
|
if not id_token: |
|
|
print("\nβ οΈ Sign-in failed. Trying sign-up...") |
|
|
id_token = test_sign_up(TEST_EMAIL, TEST_PASSWORD) |
|
|
|
|
|
|
|
|
if not id_token: |
|
|
print("\nβ οΈ Sign-up failed or user already exists. Retrying sign-in...") |
|
|
id_token = sign_in_with_email_password(TEST_EMAIL, TEST_PASSWORD) |
|
|
|
|
|
if id_token: |
|
|
|
|
|
test_api_with_token(id_token) |
|
|
|
|
|
print(f"\n{'='*60}") |
|
|
print("β
Testing Complete!") |
|
|
print(f"{'='*60}") |
|
|
print("\nπ To use this token in curl/Postman:") |
|
|
print(f" Authorization: Bearer {id_token[:50]}...") |
|
|
else: |
|
|
print(f"\n{'='*60}") |
|
|
print("β Could not obtain Firebase ID token") |
|
|
print(f"{'='*60}") |
|
|
print("\nπ‘ Troubleshooting:") |
|
|
print(" 1. Verify email/password in Firebase Console") |
|
|
print(" 2. Check that Email/Password auth is enabled") |
|
|
print(" 3. Create user manually in Firebase Console if needed") |
|
|
print(" 4. Verify Web API key is correct") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|
|
|
|