Fred808 commited on
Commit
73bdc1f
·
verified ·
1 Parent(s): 010faf0

Update app/core/auth.py

Browse files
Files changed (1) hide show
  1. app/core/auth.py +25 -0
app/core/auth.py CHANGED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime
2
+ from fastapi import HTTPException, Security, status
3
+ from fastapi.security import APIKeyHeader
4
+ from ..core.config import settings
5
+
6
+ # API Key header scheme for service token
7
+ SERVICE_TOKEN_HEADER = APIKeyHeader(name="X-Service-Token", auto_error=False)
8
+
9
+ async def verify_service_token(token: str = Security(SERVICE_TOKEN_HEADER)) -> bool:
10
+ """Verify the service token for internal service-to-service communication."""
11
+ if not token:
12
+ raise HTTPException(
13
+ status_code=status.HTTP_401_UNAUTHORIZED,
14
+ detail="Service token is required",
15
+ headers={"WWW-Authenticate": "Bearer"},
16
+ )
17
+
18
+ if token != settings.SERVICE_TOKEN:
19
+ raise HTTPException(
20
+ status_code=status.HTTP_401_UNAUTHORIZED,
21
+ detail="Invalid service token",
22
+ headers={"WWW-Authenticate": "Bearer"},
23
+ )
24
+
25
+ return True