Dmitry Beresnev commited on
Commit
0337912
·
1 Parent(s): f755f75

fix gateway token

Browse files
Files changed (1) hide show
  1. app.py +70 -1
app.py CHANGED
@@ -342,6 +342,15 @@ def with_token(url: str, token: str) -> str:
342
  return urlunsplit((parts.scheme, parts.netloc, parts.path, urlencode(query), parts.fragment))
343
 
344
 
 
 
 
 
 
 
 
 
 
345
  def resolve_gateway_token() -> str:
346
  if OPENCLAW_GATEWAY_TOKEN:
347
  return OPENCLAW_GATEWAY_TOKEN
@@ -356,6 +365,65 @@ def resolve_gateway_token() -> str:
356
  return ""
357
 
358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
  def build_strategy_frame(data: pd.DataFrame, fast_period: int, slow_period: int) -> pd.DataFrame:
360
  frame = pd.DataFrame(index=data.index)
361
  frame["Close"] = data["Close"]
@@ -471,7 +539,7 @@ with std_ui_status_col:
471
  proxy_ok = False
472
  local_status = "Unavailable"
473
  proxy_status = "Unavailable"
474
- gateway_token = resolve_gateway_token()
475
  std_ui_url = with_token(OPENCLAW_STANDARD_UI_URL, gateway_token)
476
  try:
477
  ui_resp = requests.get(std_ui_url, timeout=2, allow_redirects=True)
@@ -502,6 +570,7 @@ with std_ui_status_col:
502
  with std_ui_embed_col:
503
  public_ui_url = with_token(OPENCLAW_STANDARD_UI_PUBLIC_URL, gateway_token)
504
  if public_ui_url:
 
505
  components.iframe(public_ui_url, height=560, scrolling=True)
506
  st.markdown(f"[Open Standard UI]({public_ui_url})")
507
  else:
 
342
  return urlunsplit((parts.scheme, parts.netloc, parts.path, urlencode(query), parts.fragment))
343
 
344
 
345
+ def mask_token_in_url(url: str) -> str:
346
+ parts = urlsplit(url)
347
+ query = dict(parse_qsl(parts.query, keep_blank_values=True))
348
+ token = query.get("token", "")
349
+ if token:
350
+ query["token"] = f"{token[:4]}...{token[-4:]}" if len(token) > 8 else "***"
351
+ return urlunsplit((parts.scheme, parts.netloc, parts.path, urlencode(query), parts.fragment))
352
+
353
+
354
  def resolve_gateway_token() -> str:
355
  if OPENCLAW_GATEWAY_TOKEN:
356
  return OPENCLAW_GATEWAY_TOKEN
 
365
  return ""
366
 
367
 
368
+ def _collect_auth_tokens(obj: object) -> list[str]:
369
+ found: list[str] = []
370
+ if isinstance(obj, dict):
371
+ # Direct auth token object: {"auth": {"token": "..."}}
372
+ auth_obj = obj.get("auth")
373
+ if isinstance(auth_obj, dict):
374
+ tok = auth_obj.get("token")
375
+ if isinstance(tok, str) and tok.strip():
376
+ found.append(tok.strip())
377
+ for value in obj.values():
378
+ found.extend(_collect_auth_tokens(value))
379
+ elif isinstance(obj, list):
380
+ for item in obj:
381
+ found.extend(_collect_auth_tokens(item))
382
+ return found
383
+
384
+
385
+ def resolve_ui_token() -> str:
386
+ # 1) Explicit env override always wins.
387
+ if OPENCLAW_GATEWAY_TOKEN:
388
+ return OPENCLAW_GATEWAY_TOKEN
389
+
390
+ # 2) Collect candidate tokens from runtime state.
391
+ candidates: list[str] = []
392
+ try:
393
+ state_cfg = json.loads(OPENCLAW_STATE_CONFIG_PATH.read_text(encoding="utf-8"))
394
+ candidates.extend(_collect_auth_tokens(state_cfg))
395
+ gw_tok = (
396
+ state_cfg.get("gateway", {})
397
+ .get("auth", {})
398
+ .get("token", "")
399
+ )
400
+ if isinstance(gw_tok, str) and gw_tok.strip():
401
+ candidates.append(gw_tok.strip())
402
+ except Exception:
403
+ pass
404
+
405
+ # Deduplicate while preserving order.
406
+ deduped = []
407
+ seen = set()
408
+ for t in candidates:
409
+ if t not in seen:
410
+ seen.add(t)
411
+ deduped.append(t)
412
+
413
+ # 3) Validate token candidates against browser UI endpoint.
414
+ for token in deduped:
415
+ try:
416
+ probe_url = with_token(OPENCLAW_STANDARD_UI_URL, token)
417
+ resp = requests.get(probe_url, timeout=2, allow_redirects=True)
418
+ if resp.status_code < 400:
419
+ return token
420
+ except Exception:
421
+ continue
422
+
423
+ # 4) Fallback to legacy gateway token resolution.
424
+ return resolve_gateway_token()
425
+
426
+
427
  def build_strategy_frame(data: pd.DataFrame, fast_period: int, slow_period: int) -> pd.DataFrame:
428
  frame = pd.DataFrame(index=data.index)
429
  frame["Close"] = data["Close"]
 
539
  proxy_ok = False
540
  local_status = "Unavailable"
541
  proxy_status = "Unavailable"
542
+ gateway_token = resolve_ui_token()
543
  std_ui_url = with_token(OPENCLAW_STANDARD_UI_URL, gateway_token)
544
  try:
545
  ui_resp = requests.get(std_ui_url, timeout=2, allow_redirects=True)
 
570
  with std_ui_embed_col:
571
  public_ui_url = with_token(OPENCLAW_STANDARD_UI_PUBLIC_URL, gateway_token)
572
  if public_ui_url:
573
+ st.caption(f"Embedded URL: {mask_token_in_url(public_ui_url)}")
574
  components.iframe(public_ui_url, height=560, scrolling=True)
575
  st.markdown(f"[Open Standard UI]({public_ui_url})")
576
  else: