File size: 3,755 Bytes
b996603
48eca97
b996603
 
 
 
48eca97
 
 
 
 
 
 
 
 
b996603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48eca97
b996603
48eca97
b996603
 
 
 
 
 
48eca97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b996603
48eca97
 
b996603
 
 
48eca97
 
 
 
 
 
b996603
48eca97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from passlib.context import CryptContext
from datetime import datetime, timedelta, timezone
from typing import Optional
from jose import JWTError, jwt
from app.config import settings
from fastapi import HTTPException, status
import logging
import json
import os

import firebase_admin
from firebase_admin import auth as firebase_auth, credentials

logger = logging.getLogger(__name__)
_firebase_initialized = False

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")


def hash_password(password: str) -> str:
    """Hash a password"""
    return pwd_context.hash(password)


def verify_password(plain_password: str, hashed_password: str) -> bool:
    """Verify a password against its hash"""
    return pwd_context.verify(plain_password, hashed_password)


def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
    """Create JWT access token"""
    to_encode = data.copy()
    
    if expires_delta:
        expire = datetime.now(timezone.utc) + expires_delta
    else:
        expire = datetime.now(timezone.utc) + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
    
    to_encode.update({"exp": expire})
    encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
    return encoded_jwt


def _init_firebase_if_needed() -> None:
    global _firebase_initialized

    if _firebase_initialized:
        return

    if firebase_admin._apps:
        _firebase_initialized = True
        return

    if settings.FIREBASE_SERVICE_ACCOUNT_JSON:
        try:
            account_info = json.loads(settings.FIREBASE_SERVICE_ACCOUNT_JSON)
            cred = credentials.Certificate(account_info)
            firebase_admin.initialize_app(cred)
            _firebase_initialized = True
            return
        except Exception as e:
            logger.warning("Firebase init from JSON failed: %s", str(e))

    if settings.FIREBASE_SERVICE_ACCOUNT_PATH:
        path = settings.FIREBASE_SERVICE_ACCOUNT_PATH
        if os.path.exists(path):
            try:
                cred = credentials.Certificate(path)
                firebase_admin.initialize_app(cred)
                _firebase_initialized = True
                return
            except Exception as e:
                logger.warning("Firebase init from service account path failed: %s", str(e))

    logger.warning("Firebase Auth is enabled but service account credentials are not configured correctly")


def decode_token(token: str):
    """Decode and validate JWT token (legacy local JWT + Firebase ID token)."""
    # 1) Legacy local JWT support
    try:
        payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
        email: str = payload.get("sub")
        if email:
            return {
                "email": email,
                "sub": payload.get("sub"),
                "name": payload.get("name"),
            }
    except JWTError:
        pass

    # 2) Firebase ID token support
    if settings.FIREBASE_AUTH_ENABLED:
        _init_firebase_if_needed()
        if _firebase_initialized:
            try:
                decoded = firebase_auth.verify_id_token(token)
                email = decoded.get("email")
                name = decoded.get("name")
                sub = decoded.get("uid") or decoded.get("sub")

                if email:
                    return {
                        "email": email,
                        "sub": sub,
                        "name": name,
                    }
            except Exception as e:
                logger.warning("Firebase token validation failed: %s", str(e))

    raise HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
    )