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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
"""

Authentication Middleware for AegisLM



Provides FastAPI middleware for authentication and authorization.

"""

import hashlib
import jwt
import uuid
from datetime import datetime, timedelta
from typing import Optional
from dataclasses import dataclass

from fastapi import Depends, HTTPException, Request, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from backend.db.models import User, Tenant, APIKey
from backend.db.session import get_db_session
from security.rbac import RBACContext, Role
from security.tenant_scope import TenantScope, set_tenant_scope


# Security scheme
security = HTTPBearer()

# JWT configuration - loaded from secret manager
def _get_jwt_secret() -> str:
    """Get JWT secret from secret manager."""
    from security.secret_manager import get_jwt_secret
    return get_jwt_secret()

def _get_jwt_algorithm() -> str:
    """Get JWT algorithm from secret manager."""
    from security.secret_manager import get_secret_manager
    return get_secret_manager().get_jwt_algorithm()

def _get_jwt_expiration_hours() -> int:
    """Get JWT expiration hours from secret manager."""
    from security.secret_manager import get_secret_manager
    return get_secret_manager().get_jwt_expiration_hours()


@dataclass
class AuthenticatedUser:
    """Represents an authenticated user."""
    user_id: uuid.UUID
    tenant_id: uuid.UUID
    email: str
    role: Role
    is_api_client: bool = False


def hash_api_key(api_key: str) -> str:
    """Hash an API key for storage/comparison."""
    return hashlib.sha256(api_key.encode()).hexdigest()


def create_jwt_token(

    user_id: uuid.UUID,

    tenant_id: uuid.UUID,

    email: str,

    role: str,

    expires_delta: Optional[timedelta] = None,

) -> str:
    """

    Create a JWT token for a user.

    

    Args:

        user_id: User ID

        tenant_id: Tenant ID

        email: User email

        role: User role

        expires_delta: Token expiration time delta

        

    Returns:

        JWT token string

    """
    if expires_delta is None:
        expires_delta = timedelta(hours=_get_jwt_expiration_hours())
    
    expire = datetime.utcnow() + expires_delta
    
    payload = {
        "sub": str(user_id),
        "tenant_id": str(tenant_id),
        "email": email,
        "role": role,
        "exp": expire,
        "iat": datetime.utcnow(),
    }
    
    return jwt.encode(payload, _get_jwt_secret(), algorithm=_get_jwt_algorithm())


def decode_jwt_token(token: str) -> dict:
    """

    Decode and validate a JWT token.

    

    Args:

        token: JWT token string

        

    Returns:

        Decoded token payload

        

    Raises:

        HTTPException: If token is invalid or expired

    """
    try:
        payload = jwt.decode(
            token, 
            _get_jwt_secret(), 
            algorithms=[_get_jwt_algorithm()]
        )
        return payload
    except jwt.ExpiredSignatureError:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Token has expired",
            headers={"WWW-Authenticate": "Bearer"},
        )
    except jwt.InvalidTokenError:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid token",
            headers={"WWW-Authenticate": "Bearer"},
        )


async def get_db() -> AsyncSession:
    """Get database session."""
    async for session in get_db_session():
        yield session


async def get_current_user_from_token(

    token: str,

) -> tuple[uuid.UUID, uuid.UUID, str, Role]:
    """

    Validate JWT token and extract user info WITHOUT database access.

    

    This function validates the token signature and expiration ONLY.

    It does NOT query the database.

    

    Returns:

        Tuple of (user_id, tenant_id, email, role)

        

    Raises:

        HTTPException: If token is invalid or expired

    """
    # Step 1: Validate JWT signature and expiration (NO DB ACCESS)
    payload = decode_jwt_token(token)
    
    # Step 2: Extract claims from validated token
    user_id = uuid.UUID(payload["sub"])
    tenant_id = uuid.UUID(payload["tenant_id"])
    email = payload["email"]
    role = Role(payload["role"])
    
    return user_id, tenant_id, email, role


async def get_current_user(

    credentials: HTTPAuthorizationCredentials = Depends(security),

    db: AsyncSession = Depends(get_db),

) -> AuthenticatedUser:
    """

    FastAPI dependency to get the current authenticated user.

    

    CRITICAL: This validates the JWT signature FIRST (no DB), 

    then only queries DB if token is valid.

    

    This ensures 401 is returned BEFORE any database access for 

    unauthenticated requests.

    """
    # Step 1: Validate token WITHOUT database (fails fast for invalid tokens)
    token = credentials.credentials
    user_id, tenant_id, email, role = await get_current_user_from_token(token)
    
    # Step 2: Only query DB AFTER token validation succeeds
    query = select(User).where(
        User.id == user_id,
        User.tenant_id == tenant_id,
        User.active == True,
    )
    result = await db.execute(query)
    user = result.scalar_one_or_none()
    
    if user is None:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="User not found or inactive",
            headers={"WWW-Authenticate": "Bearer"},
        )
    
    return AuthenticatedUser(
        user_id=user.id,
        tenant_id=user.tenant_id,
        email=user.email,
        role=Role(user.role),
        is_api_client=False,
    )


async def get_current_user_optional(

    request: Request,

    db: AsyncSession = Depends(get_db),

) -> Optional[AuthenticatedUser]:
    """

    FastAPI dependency to get the current authenticated user, optionally.

    

    Returns None if no valid authentication is provided.

    """
    # Check for Bearer token
    auth_header = request.headers.get("Authorization")
    if auth_header and auth_header.startswith("Bearer "):
        token = auth_header[7:]
        try:
            payload = decode_jwt_token(token)
            user_id = uuid.UUID(payload["sub"])
            tenant_id = uuid.UUID(payload["tenant_id"])
            
            query = select(User).where(
                User.id == user_id,
                User.tenant_id == tenant_id,
                User.active == True,
            )
            result = await db.execute(query)
            user = result.scalar_one_or_none()
            
            if user:
                return AuthenticatedUser(
                    user_id=user.id,
                    tenant_id=user.tenant_id,
                    email=user.email,
                    role=Role(user.role),
                    is_api_client=False,
                )
        except Exception:
            pass
    
    # Check for API key
    api_key = request.headers.get("X-API-Key")
    if api_key:
        return await verify_api_key(db, api_key)
    
    return None


async def verify_api_key(

    db: AsyncSession,

    api_key: str,

) -> Optional[AuthenticatedUser]:
    """

    Verify an API key and return the associated user.

    

    Args:

        db: Database session

        api_key: API key to verify

        

    Returns:

        AuthenticatedUser if valid, None otherwise

    """
    key_hash = hash_api_key(api_key)
    
    query = select(APIKey).where(
        APIKey.key_hash == key_hash,
        APIKey.active == True,
    )
    result = await db.execute(query)
    api_key_obj = result.scalar_one_or_none()
    
    if api_key_obj is None:
        return None
    
    # Update last used
    api_key_obj.last_used = datetime.utcnow()
    await db.commit()
    
    # Get the tenant
    query = select(Tenant).where(Tenant.id == api_key_obj.tenant_id)
    result = await db.execute(query)
    tenant = result.scalar_one_or_none()
    
    if tenant is None or not tenant.active:
        return None
    
    return AuthenticatedUser(
        user_id=api_key_obj.id,  # Use API key ID as user_id for API clients
        tenant_id=api_key_obj.tenant_id,
        email=f"api:{api_key_obj.owner}",
        role=Role.API_CLIENT,
        is_api_client=True,
    )


async def get_current_tenant(

    user: AuthenticatedUser = Depends(get_current_user),

) -> uuid.UUID:
    """Get the current tenant ID from the authenticated user."""
    return user.tenant_id


class AuthMiddleware:
    """

    Authentication middleware for FastAPI.

    

    Provides request authentication and sets up tenant context.

    """
    
    @staticmethod
    def hash_api_key(api_key: str) -> str:
        """Hash an API key for storage/comparison."""
        return hashlib.sha256(api_key.encode()).hexdigest()
    
    @staticmethod
    async def verify_api_key(db: AsyncSession, api_key: str) -> Optional[AuthenticatedUser]:
        """

        Verify an API key and return the associated user.

        """
        return await verify_api_key(db, api_key)
    
    @staticmethod
    async def authenticate_request(

        request: Request,

        db: AsyncSession,

    ) -> Optional[AuthenticatedUser]:
        """

        Authenticate a request using either JWT or API key.

        

        Checks Authorization header for Bearer token or X-API-Key.

        """
        # Check for Bearer token
        auth_header = request.headers.get("Authorization")
        if auth_header and auth_header.startswith("Bearer "):
            token = auth_header[7:]
            try:
                payload = decode_jwt_token(token)
                user_id = uuid.UUID(payload["sub"])
                tenant_id = uuid.UUID(payload["tenant_id"])
                
                query = select(User).where(
                    User.id == user_id,
                    User.tenant_id == tenant_id,
                    User.active == True,
                )
                result = await db.execute(query)
                user = result.scalar_one_or_none()
                
                if user:
                    return AuthenticatedUser(
                        user_id=user.id,
                        tenant_id=user.tenant_id,
                        email=user.email,
                        role=Role(user.role),
                        is_api_client=False,
                    )
            except Exception:
                pass
        
        # Check for API key
        api_key = request.headers.get("X-API-Key")
        if api_key:
            return await verify_api_key(db, api_key)
        
        return None


class TenantContextMiddleware:
    """

    Middleware to set up tenant context for each request.

    

    This ensures all database queries are properly scoped to the

    current tenant.

    """
    
    def __init__(self, app):
        self.app = app
    
    async def __call__(self, scope, receive, send):
        if scope["type"] != "http":
            await self.app(scope, receive, send)
            return
        
        # TODO: Extract tenant from request and set context
        # This would typically be done after authentication
        
        await self.app(scope, receive, send)


async def require_role(required_role: Role):
    """

    FastAPI dependency to require a specific role.

    

    Usage:

        @router.get("/admin/users")

        async def list_users(user: AuthenticatedUser = Depends(require_role(Role.ADMIN))):

            ...

    """
    async def role_checker(user: AuthenticatedUser = Depends(get_current_user)):
        if user.role != required_role:
            raise HTTPException(
                status_code=status.HTTP_403_FORBIDDEN,
                detail=f"Requires {required_role.value} role"
            )
        return user
    return role_checker


async def require_permission(permission: str):
    """

    FastAPI dependency to require a specific permission.

    

    Usage:

        @router.post("/jobs")

        async def create_job(

            user: AuthenticatedUser = Depends(require_permission("create_job"))

        ):

            ...

    """
    async def permission_checker(user: AuthenticatedUser = Depends(get_current_user)):
        # Import here to avoid circular imports
        from security.rbac import RBAC, Permission
        
        # Convert string permission to enum
        try:
            perm = Permission(permission)
        except ValueError:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=f"Invalid permission: {permission}"
            )
        
        # Check if user has permission
        if not RBAC.has_permission(user.role, perm):
            raise HTTPException(
                status_code=status.HTTP_403_FORBIDDEN,
                detail=f"Requires {permission} permission"
            )
        
        return user
    return permission_checker


def create_rbac_context(user: AuthenticatedUser) -> RBACContext:
    """

    Create an RBAC context from an authenticated user.

    

    This can be stored in request state for easy access.

    """
    return RBACContext(
        user_id=user.user_id,
        tenant_id=user.tenant_id,
        role=user.role,
    )


async def setup_tenant_context(

    user: AuthenticatedUser = Depends(get_current_user),

) -> TenantScope:
    """

    Set up tenant context for the current request.

    

    This ensures all subsequent database queries are scoped to the tenant.

    """
    scope = TenantScope(tenant_id=user.tenant_id)
    set_tenant_scope(scope)
    return scope