Spaces:
Paused
Paused
Add app password protection
Browse files
README.md
CHANGED
|
@@ -71,6 +71,14 @@ provider-proxy routing, and response key names. They intentionally do not print
|
|
| 71 |
access tokens, session tokens, Authorization headers, proxy passwords, or full
|
| 72 |
payment/redirect URLs.
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
## API
|
| 75 |
|
| 76 |
The web page uses a background-task API so long provider extraction requests do
|
|
|
|
| 71 |
access tokens, session tokens, Authorization headers, proxy passwords, or full
|
| 72 |
payment/redirect URLs.
|
| 73 |
|
| 74 |
+
## App password
|
| 75 |
+
|
| 76 |
+
Set `APP_PASSWORD` to enable HTTP Basic Auth for the web UI and API. The default
|
| 77 |
+
username is `admin`; override it with `APP_USERNAME`.
|
| 78 |
+
|
| 79 |
+
For Hugging Face Spaces, store `APP_PASSWORD` as a Space Secret instead of
|
| 80 |
+
committing it to the repository.
|
| 81 |
+
|
| 82 |
## API
|
| 83 |
|
| 84 |
The web page uses a background-task API so long provider extraction requests do
|
app.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import contextvars
|
|
|
|
|
|
|
| 4 |
import json
|
| 5 |
import logging
|
| 6 |
import os
|
| 7 |
import random
|
| 8 |
import re
|
|
|
|
| 9 |
import time
|
| 10 |
import uuid
|
| 11 |
from pathlib import Path
|
|
@@ -14,8 +17,8 @@ from typing import Any
|
|
| 14 |
from urllib.parse import parse_qsl, quote, urlencode, urljoin, urlsplit, urlunsplit
|
| 15 |
|
| 16 |
import requests
|
| 17 |
-
from fastapi import FastAPI, HTTPException
|
| 18 |
-
from fastapi.responses import FileResponse
|
| 19 |
from fastapi.staticfiles import StaticFiles
|
| 20 |
from pydantic import BaseModel, ConfigDict, Field
|
| 21 |
|
|
@@ -41,6 +44,8 @@ logging.basicConfig(
|
|
| 41 |
)
|
| 42 |
LOG = logging.getLogger("openai_pay")
|
| 43 |
REQUEST_ID: contextvars.ContextVar[str] = contextvars.ContextVar("request_id", default="-")
|
|
|
|
|
|
|
| 44 |
DEFAULT_USER_AGENT = (
|
| 45 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 46 |
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36"
|
|
@@ -1291,6 +1296,41 @@ LATEST_RESULT: dict[str, Any] = {"task_id": "", "updated_at": 0.0, "result": Non
|
|
| 1291 |
TASK_RETENTION_SECONDS = 60 * 60
|
| 1292 |
|
| 1293 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1294 |
def long_link_response_to_dict(response: LongLinkResponse) -> dict[str, Any]:
|
| 1295 |
return response.model_dump()
|
| 1296 |
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import contextvars
|
| 4 |
+
import base64
|
| 5 |
+
import binascii
|
| 6 |
import json
|
| 7 |
import logging
|
| 8 |
import os
|
| 9 |
import random
|
| 10 |
import re
|
| 11 |
+
import secrets
|
| 12 |
import time
|
| 13 |
import uuid
|
| 14 |
from pathlib import Path
|
|
|
|
| 17 |
from urllib.parse import parse_qsl, quote, urlencode, urljoin, urlsplit, urlunsplit
|
| 18 |
|
| 19 |
import requests
|
| 20 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 21 |
+
from fastapi.responses import FileResponse, Response
|
| 22 |
from fastapi.staticfiles import StaticFiles
|
| 23 |
from pydantic import BaseModel, ConfigDict, Field
|
| 24 |
|
|
|
|
| 44 |
)
|
| 45 |
LOG = logging.getLogger("openai_pay")
|
| 46 |
REQUEST_ID: contextvars.ContextVar[str] = contextvars.ContextVar("request_id", default="-")
|
| 47 |
+
APP_USERNAME = os.getenv("APP_USERNAME", "admin")
|
| 48 |
+
APP_PASSWORD = os.getenv("APP_PASSWORD", "")
|
| 49 |
DEFAULT_USER_AGENT = (
|
| 50 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 51 |
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36"
|
|
|
|
| 1296 |
TASK_RETENTION_SECONDS = 60 * 60
|
| 1297 |
|
| 1298 |
|
| 1299 |
+
def app_password_enabled() -> bool:
|
| 1300 |
+
return bool(APP_PASSWORD)
|
| 1301 |
+
|
| 1302 |
+
|
| 1303 |
+
def unauthorized_response() -> Response:
|
| 1304 |
+
return Response(
|
| 1305 |
+
"Authentication required",
|
| 1306 |
+
status_code=401,
|
| 1307 |
+
headers={"WWW-Authenticate": 'Basic realm="OpenAI Pay PP Link", charset="UTF-8"'},
|
| 1308 |
+
)
|
| 1309 |
+
|
| 1310 |
+
|
| 1311 |
+
def valid_basic_auth(authorization: str) -> bool:
|
| 1312 |
+
if not app_password_enabled() or not authorization.startswith("Basic "):
|
| 1313 |
+
return False
|
| 1314 |
+
encoded = authorization.removeprefix("Basic ").strip()
|
| 1315 |
+
try:
|
| 1316 |
+
decoded = base64.b64decode(encoded, validate=True).decode("utf-8")
|
| 1317 |
+
except (binascii.Error, UnicodeDecodeError):
|
| 1318 |
+
return False
|
| 1319 |
+
username, sep, password = decoded.partition(":")
|
| 1320 |
+
if not sep:
|
| 1321 |
+
return False
|
| 1322 |
+
return secrets.compare_digest(username, APP_USERNAME) and secrets.compare_digest(password, APP_PASSWORD)
|
| 1323 |
+
|
| 1324 |
+
|
| 1325 |
+
@app.middleware("http")
|
| 1326 |
+
async def require_app_password(request: Request, call_next: Any):
|
| 1327 |
+
if request.url.path == "/api/health" or not app_password_enabled():
|
| 1328 |
+
return await call_next(request)
|
| 1329 |
+
if valid_basic_auth(request.headers.get("authorization", "")):
|
| 1330 |
+
return await call_next(request)
|
| 1331 |
+
return unauthorized_response()
|
| 1332 |
+
|
| 1333 |
+
|
| 1334 |
def long_link_response_to_dict(response: LongLinkResponse) -> dict[str, Any]:
|
| 1335 |
return response.model_dump()
|
| 1336 |
|