Spaces:
Sleeping
Sleeping
Patryk Studzinski
commited on
Commit
·
ff33042
1
Parent(s):
3297dba
using a placeholder auth
Browse files- app/auth/__init__.py +7 -0
- app/auth/placeholder_auth.py +85 -0
app/auth/__init__.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Authentication module placeholder.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from .placeholder_auth import get_authenticated_user, get_optional_user
|
| 6 |
+
|
| 7 |
+
__all__ = ["get_authenticated_user", "get_optional_user"]
|
app/auth/placeholder_auth.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Simple token-based authentication module.
|
| 3 |
+
Uses a secret API token stored as environment variable.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from typing import Optional
|
| 8 |
+
from fastapi import Depends, HTTPException, status
|
| 9 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 10 |
+
|
| 11 |
+
# Security scheme - auto_error=False allows unauthenticated requests to pass through
|
| 12 |
+
security = HTTPBearer(auto_error=False)
|
| 13 |
+
|
| 14 |
+
# Get API token from environment variable (set as HuggingFace secret)
|
| 15 |
+
API_SECRET_TOKEN = os.getenv("API_SECRET_TOKEN", None)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
async def get_authenticated_user(
|
| 19 |
+
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security)
|
| 20 |
+
) -> dict:
|
| 21 |
+
"""
|
| 22 |
+
Simple token-based authentication.
|
| 23 |
+
|
| 24 |
+
If API_SECRET_TOKEN is set:
|
| 25 |
+
- Requires valid Bearer token matching the secret
|
| 26 |
+
If API_SECRET_TOKEN is not set:
|
| 27 |
+
- Allows all requests (development mode)
|
| 28 |
+
|
| 29 |
+
Usage:
|
| 30 |
+
1. Set API_SECRET_TOKEN as a HuggingFace Space secret
|
| 31 |
+
2. Send requests with header: Authorization: Bearer <your-token>
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
# If no secret is configured, allow all requests (dev mode)
|
| 35 |
+
if not API_SECRET_TOKEN:
|
| 36 |
+
return {
|
| 37 |
+
"user_id": "anonymous",
|
| 38 |
+
"email": "anonymous@example.com",
|
| 39 |
+
"name": "Anonymous User",
|
| 40 |
+
"authenticated": False
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Secret is configured - require valid token
|
| 44 |
+
if not credentials:
|
| 45 |
+
raise HTTPException(
|
| 46 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 47 |
+
detail="Authentication required. Provide Bearer token.",
|
| 48 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Validate token
|
| 52 |
+
if credentials.credentials != API_SECRET_TOKEN:
|
| 53 |
+
raise HTTPException(
|
| 54 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 55 |
+
detail="Invalid authentication token",
|
| 56 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Token is valid
|
| 60 |
+
return {
|
| 61 |
+
"user_id": "api_user",
|
| 62 |
+
"email": "api@example.com",
|
| 63 |
+
"name": "API User",
|
| 64 |
+
"authenticated": True
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
async def get_optional_user(
|
| 69 |
+
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security)
|
| 70 |
+
) -> Optional[dict]:
|
| 71 |
+
"""
|
| 72 |
+
Optional authentication - doesn't require credentials.
|
| 73 |
+
Returns user info if authenticated, None otherwise.
|
| 74 |
+
"""
|
| 75 |
+
if not API_SECRET_TOKEN:
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
if credentials and credentials.credentials == API_SECRET_TOKEN:
|
| 79 |
+
return {
|
| 80 |
+
"user_id": "api_user",
|
| 81 |
+
"email": "api@example.com",
|
| 82 |
+
"name": "API User",
|
| 83 |
+
"authenticated": True
|
| 84 |
+
}
|
| 85 |
+
return None
|