Repository navigation
Expand file tree
/
Copy pathauth.py
More file actions
203 lines (176 loc) · 6.66 KB
/
Copy pathauth.py
File metadata and controls
203 lines (176 loc) · 6.66 KB
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
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
import os
import secrets
import logging
from typing import Optional, Any
logger = logging.getLogger("auth")
def _load_env_file() -> None:
env_path = os.path.join(os.path.dirname(__file__), ".env")
if os.path.exists(env_path):
try:
with open(env_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#") and "=" in line:
k, v = line.split("=", 1)
k = k.strip()
v = v.strip().strip("'\"")
if k and k not in os.environ:
os.environ[k] = v
except Exception as e:
logger.warning(f"Failed to load .env file: {e}")
_load_env_file()
try:
from fastapi import Request, HTTPException, Security, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
security_bearer = HTTPBearer(auto_error=False)
except ImportError:
class HTTPException(Exception):
def __init__(self, status_code: int = 400, detail: str = "", headers: Optional[dict] = None):
self.status_code = status_code
self.detail = detail
self.headers = headers or {}
super().__init__(detail)
class Request:
pass
class HTTPAuthorizationCredentials:
credentials: str = ""
def Depends(fn):
return None
security_bearer = None
def get_api_token() -> Optional[str]:
"""
Returns the configured API token from environment variable OMNIDIDACT_API_TOKEN or API_TOKEN.
If not set or empty, authentication is disabled (local/development mode).
"""
token = os.getenv("OMNIDIDACT_API_TOKEN") or os.getenv("API_TOKEN")
if token:
token = token.strip()
return token if token else None
def is_auth_required() -> bool:
"""Returns whether API token authentication is currently required."""
return get_api_token() is not None
def verify_token_string(provided_token: Optional[str]) -> bool:
"""
Constant-time comparison between provided token and configured token.
"""
expected = get_api_token()
if not expected:
return True
if not provided_token:
return False
return secrets.compare_digest(provided_token.strip(), expected)
PUBLIC_HTML_PATHS = {
"/",
"/config",
"/chat",
"/study",
"/study/flashcards",
"/login",
"/auth",
}
def extract_token_from_request(request: Request) -> Optional[str]:
"""
Extracts the API token from Authorization header, X-API-Token, query parameters, or Cookies.
"""
# 1. Bearer token from Authorization header
auth_header = request.headers.get("Authorization") or request.headers.get("authorization")
if auth_header and auth_header.startswith("Bearer "):
val = auth_header[7:].strip()
if val:
return val
# 2. X-API-Token header
x_token = request.headers.get("X-API-Token") or request.headers.get("x-api-token")
if x_token and x_token.strip():
return x_token.strip()
# 3. Direct query parameter (for download / stream / initial links)
query_token = request.query_params.get("token") or request.query_params.get("api_token")
if query_token and query_token.strip():
return query_token.strip()
# 4. Secure Cookie (omnididact_token)
if hasattr(request, "cookies"):
cookie_token = (
request.cookies.get("omnididact_token")
or request.cookies.get("token")
or request.cookies.get("api_token")
)
if cookie_token and cookie_token.strip():
return cookie_token.strip()
return None
def is_request_authenticated(request: Request) -> bool:
"""
Returns True if authentication is disabled OR if the request presents a valid token.
"""
expected = get_api_token()
if not expected:
return True
token = extract_token_from_request(request)
return verify_token_string(token)
def set_secure_auth_cookie(
response: Any,
token: str,
remember: bool = True,
request: Optional[Request] = None
) -> None:
"""
Sets a hardened auth cookie for the user:
- HttpOnly: True (completely blocks JavaScript/XSS token theft)
- SameSite: "Lax" (defends against CSRF attacks while allowing top-level navigation)
- Secure: True when running on HTTPS or behind an SSL reverse proxy
- Path: "/" (valid for entire domain/origin)
- Max-Age: 30 days if remember else None (session cookie)
"""
is_secure = False
if request:
proto = request.headers.get("x-forwarded-proto", "").lower()
is_secure = (request.url.scheme == "https") or (proto == "https")
max_age = (30 * 86400) if remember else None
response.set_cookie(
key="omnididact_token",
value=token.strip(),
max_age=max_age,
path="/",
httponly=True,
samesite="lax",
secure=is_secure
)
def clear_auth_cookie(response: Any) -> None:
"""
Securely deletes the auth cookie.
"""
response.delete_cookie(
key="omnididact_token",
path="/",
httponly=True,
samesite="lax"
)
async def verify_api_token(
request: Request,
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security_bearer)
) -> None:
"""
FastAPI dependency that enforces API Token authentication when OMNIDIDACT_API_TOKEN is configured.
"""
expected_token = get_api_token()
if not expected_token:
# Auth not configured -> Allow anonymous access
return
# Whitelisted frontend entrypoints serve the HTML/SPA shell or redirect to /login
if hasattr(request, "url") and request.url.path in PUBLIC_HTML_PATHS:
return
# 1. Check Bearer token from Authorization header
token = credentials.credentials if credentials else None
# 2. Fallback check for X-API-Token or direct query parameter
if not token:
token = request.headers.get("X-API-Token") or request.headers.get("x-api-token")
if not token:
token = request.query_params.get("token") or request.query_params.get("api_token")
# 3. Fallback check for Cookie
if not token and hasattr(request, "cookies"):
token = request.cookies.get("omnididact_token") or request.cookies.get("token") or request.cookies.get("api_token")
if not token or not verify_token_string(token):
logger.warning(f"Unauthorized request to {request.url.path} from {request.client.host if request.client else 'unknown'}")
raise HTTPException(
status_code=401,
detail="Unauthorized: Invalid or missing API token",
headers={"WWW-Authenticate": "Bearer"}
)