|
1 | 1 | import secrets |
2 | 2 | from typing import Optional |
3 | 3 |
|
4 | | -from fastapi import Depends, HTTPException, Request |
5 | | -from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer |
6 | | -from loguru import logger |
| 4 | +from fastapi import Request |
7 | 5 | from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint |
8 | 6 | from starlette.responses import JSONResponse, Response |
9 | 7 | from starlette.types import ASGIApp |
|
14 | 12 | class PasswordAuthMiddleware(BaseHTTPMiddleware): |
15 | 13 | """ |
16 | 14 | Middleware to check password authentication for all API requests. |
17 | | - Always active with default password if OPEN_NOTEBOOK_PASSWORD is not set. |
| 15 | + Auth is fully disabled (no hardcoded default password) if |
| 16 | + OPEN_NOTEBOOK_PASSWORD is not set. |
18 | 17 | Supports Docker secrets via OPEN_NOTEBOOK_PASSWORD_FILE. |
19 | 18 | """ |
20 | 19 |
|
@@ -81,44 +80,3 @@ async def dispatch( |
81 | 80 | # Password is correct, proceed with the request |
82 | 81 | response = await call_next(request) |
83 | 82 | return response |
84 | | - |
85 | | - |
86 | | -# Optional: HTTPBearer security scheme for OpenAPI documentation |
87 | | -security = HTTPBearer(auto_error=False) |
88 | | - |
89 | | - |
90 | | -def check_api_password( |
91 | | - credentials: Optional[HTTPAuthorizationCredentials] = Depends(security), |
92 | | -) -> bool: |
93 | | - """ |
94 | | - Utility function to check API password. |
95 | | - Can be used as a dependency in individual routes if needed. |
96 | | - Supports Docker secrets via OPEN_NOTEBOOK_PASSWORD_FILE. |
97 | | - Returns True without checking credentials if OPEN_NOTEBOOK_PASSWORD is not configured. |
98 | | - Raises 401 if credentials are missing or don't match the configured password. |
99 | | - """ |
100 | | - password = get_secret_from_env("OPEN_NOTEBOOK_PASSWORD") |
101 | | - |
102 | | - # No password configured - skip authentication |
103 | | - if not password: |
104 | | - return True |
105 | | - |
106 | | - # No credentials provided |
107 | | - if not credentials: |
108 | | - raise HTTPException( |
109 | | - status_code=401, |
110 | | - detail="Missing authorization", |
111 | | - headers={"WWW-Authenticate": "Bearer"}, |
112 | | - ) |
113 | | - |
114 | | - # Check password (constant-time to avoid a timing side-channel) |
115 | | - if not secrets.compare_digest( |
116 | | - credentials.credentials.encode("utf-8"), password.encode("utf-8") |
117 | | - ): |
118 | | - raise HTTPException( |
119 | | - status_code=401, |
120 | | - detail="Invalid password", |
121 | | - headers={"WWW-Authenticate": "Bearer"}, |
122 | | - ) |
123 | | - |
124 | | - return True |
0 commit comments