#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Firebase Authentication Test Script Tests sign-in and API access with Firebase ID tokens """ import requests import json import sys # Fix Windows console encoding 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 Configuration FIREBASE_WEB_API_KEY = "AIzaSyCPTqGkHtQmSkJiEjRHxmylBXJQGGEErG0" FIREBASE_AUTH_URL = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={FIREBASE_WEB_API_KEY}" # API Configuration API_BASE_URL = "https://logicgoinfotechspaces-face-swap-video.hf.space" # Test Credentials TEST_EMAIL = "itisha.logicgo@gmail.com" TEST_PASSWORD = "mBQ2KjjlioRbK7FFCUcbvGWqTf62" # NOTE: This might be a UID, not password. Use actual password if different. 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) # Try sign-in first id_token = sign_in_with_email_password(TEST_EMAIL, TEST_PASSWORD) # If sign-in fails, try sign-up (if user doesn't exist) if not id_token: print("\nāš ļø Sign-in failed. Trying sign-up...") id_token = test_sign_up(TEST_EMAIL, TEST_PASSWORD) # If still no token, try sign-in again (in case sign-up created the user) 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 endpoints 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()