File size: 3,166 Bytes
c622e4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Firebase Authentication utilities.
"""
import os
import json
import firebase_admin
from firebase_admin import auth, credentials
from fastapi import HTTPException

# Initialize Firebase Admin SDK
_firebase_initialized = False

def initialize_firebase():
    """Initialize Firebase Admin SDK."""
    global _firebase_initialized
    
    if _firebase_initialized:
        return
    
    if not firebase_admin._apps:
        # Try to get service account from environment variable (JSON string)
        service_account_json = os.environ.get("FIREBASE_SERVICE_ACCOUNT_JSON")
        
        if service_account_json:
            try:
                service_account_info = json.loads(service_account_json)
                cred = credentials.Certificate(service_account_info)
                firebase_admin.initialize_app(cred)
                _firebase_initialized = True
                print("[INFO] Firebase Admin SDK initialized from environment variable")
                return
            except json.JSONDecodeError:
                print("[WARNING] Failed to parse FIREBASE_SERVICE_ACCOUNT_JSON")
        
        # Try to get service account from file path
        service_account_path = os.environ.get("FIREBASE_SERVICE_ACCOUNT_KEY")
        if service_account_path and os.path.exists(service_account_path):
            cred = credentials.Certificate(service_account_path)
            firebase_admin.initialize_app(cred)
            _firebase_initialized = True
            print(f"[INFO] Firebase Admin SDK initialized from file: {service_account_path}")
            return
        
        # Try to use default credentials (for Google Cloud environments)
        try:
            firebase_admin.initialize_app()
            _firebase_initialized = True
            print("[INFO] Firebase Admin SDK initialized with default credentials")
            return
        except Exception as e:
            print(f"[WARNING] Firebase initialization failed: {e}")
            raise HTTPException(
                status_code=500,
                detail="Firebase not configured. Please set FIREBASE_SERVICE_ACCOUNT_JSON or FIREBASE_SERVICE_ACCOUNT_KEY environment variable."
            )


async def verify_firebase_token(id_token: str) -> dict:
    """
    Verify Firebase ID token and return user info.
    
    Args:
        id_token: Firebase ID token from client
        
    Returns:
        Dictionary with user information (uid, email, name, picture)
        
    Raises:
        HTTPException: If token is invalid
    """
    initialize_firebase()
    
    try:
        decoded_token = auth.verify_id_token(id_token)
        
        return {
            'uid': decoded_token['uid'],
            'email': decoded_token.get('email'),
            'name': decoded_token.get('name'),
            'picture': decoded_token.get('picture'),
        }
    except ValueError as e:
        raise HTTPException(
            status_code=401,
            detail=f"Invalid Firebase token: {str(e)}"
        )
    except Exception as e:
        raise HTTPException(
            status_code=401,
            detail=f"Firebase authentication failed: {str(e)}"
        )