fix: auto-inject internal Basic Auth to eliminate 401 Basic Realm prompt
Browse files
app.py
CHANGED
|
@@ -4,12 +4,14 @@ Features:
|
|
| 4 |
- Pure Full-Screen UI: Proxies directly to native OpenCode web UI with zero redundant sidebars.
|
| 5 |
- Strict Security Gate: Unauthenticated requests see a locked authentication challenge.
|
| 6 |
- Multi-factor support: Accepts valid dynamic 8-digit PIN and/or Master Authorization Token.
|
|
|
|
| 7 |
- Frame Embedding: Strips X-Frame-Options and restrictive CSP so authorized parent apps embed cleanly.
|
| 8 |
- Full WebSocket & Streaming Support.
|
| 9 |
- Bucket persistence to kacapower/ghar-storage.
|
| 10 |
"""
|
| 11 |
|
| 12 |
import asyncio
|
|
|
|
| 13 |
import hashlib
|
| 14 |
import hmac
|
| 15 |
import json
|
|
@@ -42,6 +44,11 @@ ROOTS = {
|
|
| 42 |
"config": Path(os.getenv("OPENCODE_CONFIG_DIR", HOME / ".config/opencode")),
|
| 43 |
}
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# Security Configuration
|
| 46 |
UNLOCK_SECRET = os.getenv("UNLOCK_SECRET") or os.getenv("VAULT_SECRET", "Ghar777").strip()
|
| 47 |
ACCESS_PIN = os.getenv("ACCESS_PIN", "").strip()
|
|
@@ -62,6 +69,7 @@ STRIP_HEADERS = {
|
|
| 62 |
"x-frame-options",
|
| 63 |
"content-security-policy",
|
| 64 |
"content-security-policy-report-only",
|
|
|
|
| 65 |
"date",
|
| 66 |
}
|
| 67 |
|
|
@@ -170,7 +178,7 @@ async def health(request: Request):
|
|
| 170 |
ok = True
|
| 171 |
try:
|
| 172 |
async with httpx.AsyncClient(timeout=2.0) as c:
|
| 173 |
-
await c.get(UPSTREAM + "/")
|
| 174 |
except Exception:
|
| 175 |
ok = False
|
| 176 |
|
|
@@ -185,7 +193,10 @@ async def health(request: Request):
|
|
| 185 |
# ---------------- PROXY TO OPENCODE ----------------
|
| 186 |
|
| 187 |
def _proxy_headers(request: Request) -> dict:
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
|
| 191 |
def _response_headers(upstream: httpx.Response) -> dict:
|
|
@@ -203,7 +214,6 @@ def _response_headers(upstream: httpx.Response) -> dict:
|
|
| 203 |
async def proxy(path: str, request: Request):
|
| 204 |
# Verify authentication
|
| 205 |
if not is_auth_valid(request):
|
| 206 |
-
# If user opened in browser directly without PIN, show lock page
|
| 207 |
if request.method == "GET" and "text/html" in request.headers.get("accept", ""):
|
| 208 |
err = "Invalid PIN" if request.query_params.get("pin") else ""
|
| 209 |
return HTMLResponse(get_lock_html(err), status_code=401 if err else 200)
|
|
@@ -264,8 +274,11 @@ async def ws_proxy(websocket: WebSocket, path: str):
|
|
| 264 |
target = f"{UPSTREAM_WS}/{path}" + (f"?{query}" if query else "")
|
| 265 |
subprotocols = [p.strip() for p in protocol.split(",")] if protocol else None
|
| 266 |
|
|
|
|
|
|
|
|
|
|
| 267 |
try:
|
| 268 |
-
async with websockets.connect(target, subprotocols=subprotocols, max_size=None) as upstream:
|
| 269 |
|
| 270 |
async def pump_upstream():
|
| 271 |
try:
|
|
|
|
| 4 |
- Pure Full-Screen UI: Proxies directly to native OpenCode web UI with zero redundant sidebars.
|
| 5 |
- Strict Security Gate: Unauthenticated requests see a locked authentication challenge.
|
| 6 |
- Multi-factor support: Accepts valid dynamic 8-digit PIN and/or Master Authorization Token.
|
| 7 |
+
- Internal loopback Basic Auth automated injection: Eliminates 401 Basic Realm prompt.
|
| 8 |
- Frame Embedding: Strips X-Frame-Options and restrictive CSP so authorized parent apps embed cleanly.
|
| 9 |
- Full WebSocket & Streaming Support.
|
| 10 |
- Bucket persistence to kacapower/ghar-storage.
|
| 11 |
"""
|
| 12 |
|
| 13 |
import asyncio
|
| 14 |
+
import base64
|
| 15 |
import hashlib
|
| 16 |
import hmac
|
| 17 |
import json
|
|
|
|
| 44 |
"config": Path(os.getenv("OPENCODE_CONFIG_DIR", HOME / ".config/opencode")),
|
| 45 |
}
|
| 46 |
|
| 47 |
+
# Internal Loopback Basic Auth credentials
|
| 48 |
+
INTERNAL_USER = os.getenv("OPENCODE_SERVER_USERNAME", "opencode")
|
| 49 |
+
INTERNAL_PASS = os.getenv("OPENCODE_SERVER_PASSWORD", "loopback_internal_pass_777")
|
| 50 |
+
INTERNAL_BASIC_AUTH = "Basic " + base64.b64encode(f"{INTERNAL_USER}:{INTERNAL_PASS}".encode()).decode()
|
| 51 |
+
|
| 52 |
# Security Configuration
|
| 53 |
UNLOCK_SECRET = os.getenv("UNLOCK_SECRET") or os.getenv("VAULT_SECRET", "Ghar777").strip()
|
| 54 |
ACCESS_PIN = os.getenv("ACCESS_PIN", "").strip()
|
|
|
|
| 69 |
"x-frame-options",
|
| 70 |
"content-security-policy",
|
| 71 |
"content-security-policy-report-only",
|
| 72 |
+
"www-authenticate",
|
| 73 |
"date",
|
| 74 |
}
|
| 75 |
|
|
|
|
| 178 |
ok = True
|
| 179 |
try:
|
| 180 |
async with httpx.AsyncClient(timeout=2.0) as c:
|
| 181 |
+
await c.get(UPSTREAM + "/", headers={"authorization": INTERNAL_BASIC_AUTH})
|
| 182 |
except Exception:
|
| 183 |
ok = False
|
| 184 |
|
|
|
|
| 193 |
# ---------------- PROXY TO OPENCODE ----------------
|
| 194 |
|
| 195 |
def _proxy_headers(request: Request) -> dict:
|
| 196 |
+
headers = {k: v for k, v in request.headers.items() if k.lower() not in HOP_HEADERS}
|
| 197 |
+
# Inject internal Basic Auth so OpenCode server on 127.0.0.1 never rejects with 401
|
| 198 |
+
headers["authorization"] = INTERNAL_BASIC_AUTH
|
| 199 |
+
return headers
|
| 200 |
|
| 201 |
|
| 202 |
def _response_headers(upstream: httpx.Response) -> dict:
|
|
|
|
| 214 |
async def proxy(path: str, request: Request):
|
| 215 |
# Verify authentication
|
| 216 |
if not is_auth_valid(request):
|
|
|
|
| 217 |
if request.method == "GET" and "text/html" in request.headers.get("accept", ""):
|
| 218 |
err = "Invalid PIN" if request.query_params.get("pin") else ""
|
| 219 |
return HTMLResponse(get_lock_html(err), status_code=401 if err else 200)
|
|
|
|
| 274 |
target = f"{UPSTREAM_WS}/{path}" + (f"?{query}" if query else "")
|
| 275 |
subprotocols = [p.strip() for p in protocol.split(",")] if protocol else None
|
| 276 |
|
| 277 |
+
# Forward with Basic Auth headers
|
| 278 |
+
extra_headers = {"Authorization": INTERNAL_BASIC_AUTH}
|
| 279 |
+
|
| 280 |
try:
|
| 281 |
+
async with websockets.connect(target, subprotocols=subprotocols, extra_headers=extra_headers, max_size=None) as upstream:
|
| 282 |
|
| 283 |
async def pump_upstream():
|
| 284 |
try:
|
start.sh
CHANGED
|
@@ -3,6 +3,9 @@ set -uo pipefail
|
|
| 3 |
|
| 4 |
cd /home/node || exit 1
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
echo "[start] hydrating from bucket"
|
| 7 |
python3 /home/node/sync.py --pull || echo "[start] pull failed - starting with local files"
|
| 8 |
|
|
|
|
| 3 |
|
| 4 |
cd /home/node || exit 1
|
| 5 |
|
| 6 |
+
export OPENCODE_SERVER_USERNAME="opencode"
|
| 7 |
+
export OPENCODE_SERVER_PASSWORD="loopback_internal_pass_777"
|
| 8 |
+
|
| 9 |
echo "[start] hydrating from bucket"
|
| 10 |
python3 /home/node/sync.py --pull || echo "[start] pull failed - starting with local files"
|
| 11 |
|