Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions comfy_api/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ class FeatureFlagInfo(TypedDict):
"default": True,
"description": "Gate the local Run button behind sign-in when the graph contains partner nodes",
},
"partner_node_token": {
"type": "bool",
"default": False,
"description": "Queue Local API-node prompts with a renewable partner-node token instead of the workspace token",
},
"local_web_sso": {
"type": "bool",
"default": False,
"description": "Offer Sign in with browser (Cloud OAuth + PKCE) on Local ComfyUI opened at localhost or 127.0.0.1",
},
}


Expand Down
2 changes: 2 additions & 0 deletions comfy_api_nodes/nodes_sonilo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get_comfy_api_headers,
get_node_id,
is_processing_interrupted,
refresh_partner_token,
)
from comfy_api_nodes.util.common_exceptions import ProcessingInterrupted
from server import PromptServer
Expand Down Expand Up @@ -165,6 +166,7 @@ async def _stream_sonilo_music(
"""POST ``form`` to Sonilo, read the NDJSON stream, and return the first stream's audio bytes."""
url = urljoin(default_base_url().rstrip("/") + "/", endpoint.path.lstrip("/"))

await refresh_partner_token(cls)
headers = get_comfy_api_headers(cls)
headers.update(endpoint.headers)

Expand Down
165 changes: 162 additions & 3 deletions comfy_api_nodes/util/_helpers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import asyncio
import base64
import binascii
import contextlib
import json
import logging
import os
import re
import time
from collections.abc import Callable
from datetime import datetime, timezone
from email.utils import parsedate_to_datetime
from io import BytesIO
from urllib.parse import urlparse
from urllib.parse import urljoin, urlparse

import aiohttp
from aiohttp.client_exceptions import ClientError
Expand Down Expand Up @@ -37,9 +41,164 @@ def get_node_id(node_cls: type[IO.ComfyNode]) -> str:
return node_cls.hidden.unique_id


PARTNER_NODE_TOKEN_AUDIENCE = "comfy-partner-node"
PARTNER_NODE_TOKEN_RENEW_PATH = "/auth/partner-node/renew"
_PARTNER_NODE_TOKEN_RENEW_MARGIN = 300.0 # renew a partner-node token this many seconds before it expires
_PARTNER_NODE_TOKEN_RENEW_RETRY_DELAYS = (1.0, 2.0) # pauses between renewal retries

_partner_token_lineages: dict[str, str] = {}
"""Renewed partner-node token -> the token it descends from. Only tokens comfy-api issued on renewal are entered."""
_renewed_partner_tokens: dict[str, str] = {}
"""Newest renewed partner-node token per lineage. In memory only; renewal does not rotate the token."""
_partner_token_renewals: dict[str, asyncio.Task] = {}
_capped_partner_token_lineages: set[str] = set()
"""Lineages whose last renewed token did not extend ``exp`` and is already inside the renew margin (session end reached): no pre-emptive renewal, only on a 401."""
_partner_token_renew_not_before: dict[str, float] = {}
"""Lineage -> wall-clock time before which comfy-api asked (HTTP 429) not to be sent another renewal."""
_rejected_partner_tokens: set[str] = set()
"""Tokens comfy-api answered with a 401: a renewal replaces them even when it expires sooner."""


def _partner_token_claims(token: str) -> dict | None:
"""Unverified JWT payload of a partner-node token, or None for any other credential.

The signature is not checked: the claims only decide when to renew, comfy-api still validates the token.
"""
parts = token.split(".")
if len(parts) != 3:
return None
try:
claims = json.loads(base64.urlsafe_b64decode(parts[1] + "=" * (-len(parts[1]) % 4)))
except (ValueError, binascii.Error):
return None
if not isinstance(claims, dict) or claims.get("aud") not in (PARTNER_NODE_TOKEN_AUDIENCE, [PARTNER_NODE_TOKEN_AUDIENCE]):
return None
if not isinstance(claims.get("sid"), str) or not isinstance(claims.get("exp"), (int, float)):
return None
return claims


def _partner_token_lineage(token: str) -> str:
"""The token a credential was first presented as.

Unverified claims such as ``sid`` can be forged, so a cached renewal is only shared with the token it was renewed from.
"""
return _partner_token_lineages.get(token, token)


def _newest_partner_token(token: str, claims: dict) -> tuple[str, float]:
renewed = _renewed_partner_tokens.get(_partner_token_lineage(token))
if renewed is not None:
renewed_exp = _partner_token_claims(renewed)["exp"]
if renewed_exp >= claims["exp"] or token in _rejected_partner_tokens:
return renewed, renewed_exp
return token, claims["exp"]


async def _request_partner_token_renewal(lineage: str, sid: str, token: str) -> str | None:
url = urljoin(default_base_url().rstrip("/") + "/", PARTNER_NODE_TOKEN_RENEW_PATH.lstrip("/"))
body = error = None
retry_delays = _PARTNER_NODE_TOKEN_RENEW_RETRY_DELAYS
if token not in _rejected_partner_tokens and _partner_token_claims(token)["exp"] > time.time():
retry_delays = ()
deadline = time.monotonic() + 30.0
for retry_delay in (*retry_delays, None):
if is_processing_interrupted():
return None
try:
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=max(deadline - time.monotonic(), 1.0))) as session:
async with session.post(url, headers={"Authorization": f"Bearer {token}"}) as resp:
if 400 <= resp.status < 500:
logging.warning("Comfy API refused to renew the sign-in token (HTTP %s).", resp.status)
if resp.status in (401, 403):
_renewed_partner_tokens.pop(lineage, None)
if resp.status == 429:
wait = _retry_after_wait(resp.headers.get("Retry-After"), 60.0, 600.0)
_partner_token_renew_not_before[lineage] = time.time() + wait
return None
if resp.status < 400:
body = await resp.json(content_type=None)
break
error = f"HTTP {resp.status}"
except (ValueError, asyncio.TimeoutError) as e:
logging.warning("Could not renew the sign-in token: %s", type(e).__name__)
return None
except (ClientError, OSError) as e:
error = type(e).__name__
if retry_delay is None or time.monotonic() + 2 * retry_delay > deadline or is_processing_interrupted():
logging.warning("Could not renew the sign-in token: %s", error)
return None
await asyncio.sleep(retry_delay)
renewed = body.get("token") if isinstance(body, dict) else None
claims = _partner_token_claims(renewed) if isinstance(renewed, str) else None
if claims is None or claims["sid"] != sid:
logging.warning("Comfy API returned an unusable renewed sign-in token.")
return None
if claims["exp"] <= _partner_token_claims(token)["exp"] and claims["exp"] - time.time() < _PARTNER_NODE_TOKEN_RENEW_MARGIN:
_capped_partner_token_lineages.add(lineage)
else:
_capped_partner_token_lineages.discard(lineage)
_partner_token_lineages[renewed] = lineage
_renewed_partner_tokens[lineage] = renewed
return renewed


async def _renew_partner_token(token: str, claims: dict) -> str | None:
"""Renew once per lineage at a time; concurrent callers share the in-flight renewal."""
lineage = _partner_token_lineage(token)
if time.time() < _partner_token_renew_not_before.get(lineage, 0.0):
return None
task = _partner_token_renewals.get(lineage)
if task is None or task.done() or task.get_loop() is not asyncio.get_running_loop():
task = asyncio.create_task(_request_partner_token_renewal(lineage, claims["sid"], token))
_partner_token_renewals[lineage] = task
task.add_done_callback(
lambda t: _partner_token_renewals.pop(lineage, None) if _partner_token_renewals.get(lineage) is t else None
)
while True:
if is_processing_interrupted():
raise ProcessingInterrupted("Task cancelled")
if task.done():
return task.result()
await asyncio.wait({task}, timeout=1.0)


async def refresh_partner_token(node_cls: type[IO.ComfyNode]) -> None:
"""Renew the node's partner-node token if it expires soon. Any other credential is left untouched."""
token = node_cls.hidden.auth_token_comfy_org
claims = _partner_token_claims(token) if token else None
if claims is None or _partner_token_lineage(token) in _capped_partner_token_lineages or is_processing_interrupted():
return
newest, exp = _newest_partner_token(token, claims)
if exp - time.time() < _PARTNER_NODE_TOKEN_RENEW_MARGIN:
await _renew_partner_token(newest, claims)


async def renew_rejected_partner_token(authorization: str | None) -> bool:
"""After comfy-api rejected ``authorization`` with a 401, make a fresh partner-node token current.

Returns True when a resend would carry a different, fresh token.
"""
if not authorization or not authorization.startswith("Bearer "):
return False
rejected = authorization[len("Bearer "):]
claims = _partner_token_claims(rejected)
if claims is None:
return False
_rejected_partner_tokens.add(rejected)
newest, exp = _newest_partner_token(rejected, claims)
if newest != rejected and exp - time.time() >= _PARTNER_NODE_TOKEN_RENEW_MARGIN:
return True
return await _renew_partner_token(newest, claims) is not None


def get_auth_header(node_cls: type[IO.ComfyNode]) -> dict[str, str]:
if node_cls.hidden.auth_token_comfy_org:
return {"Authorization": f"Bearer {node_cls.hidden.auth_token_comfy_org}"}
token = node_cls.hidden.auth_token_comfy_org
if token:
claims = _partner_token_claims(token)
if claims is not None:
token = _newest_partner_token(token, claims)[0]
return {"Authorization": f"Bearer {token}"}
if node_cls.hidden.api_key_comfy_org:
return {"X-API-KEY": node_cls.hidden.api_key_comfy_org}
return {}
Expand Down
22 changes: 22 additions & 0 deletions comfy_api_nodes/util/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
get_comfy_api_headers,
get_node_id,
is_processing_interrupted,
refresh_partner_token,
renew_rejected_partner_token,
sleep_with_interrupt,
)
from .common_exceptions import ApiServerError, LocalNetworkError, ProcessingInterrupted
Expand Down Expand Up @@ -897,6 +899,7 @@ async def _monitor(stop_evt: asyncio.Event, start_ts: float):
rate_limit_attempts = 0
rate_limit_delay = cfg.retry_delay
in_flight_waits = 0
auth_renewed = False
operation_succeeded: bool = False
final_elapsed_seconds: int | None = None
extracted_price: float | None = None
Expand All @@ -912,6 +915,7 @@ async def _monitor(stop_evt: asyncio.Event, start_ts: float):

payload_headers = {"Accept": "*/*"} if expect_binary else {"Accept": "application/json"}
if is_comfy_api_request:
await refresh_partner_token(cfg.node_cls)
payload_headers.update(get_comfy_api_headers(cfg.node_cls))
if cfg.endpoint.headers:
payload_headers.update(cfg.endpoint.headers)
Expand Down Expand Up @@ -993,6 +997,24 @@ async def _monitor(stop_evt: asyncio.Event, start_ts: float):
error_message=msg,
)
raise Exception(msg)
# A request rejected by comfy-api authentication never reached a provider, so resending it is safe.
if (
resp.status == 401
and is_comfy_api_request
and not auth_renewed
and await renew_rejected_partner_token(payload_headers.get("Authorization"))
):
auth_renewed = True
request_logger.log_request_response(
operation_id=operation_id,
request_method=method,
request_url=url,
response_status_code=resp.status,
response_headers=dict(resp.headers),
response_content=body,
error_message="HTTP 401 (sign-in token renewed, resending once)",
)
continue
should_retry = False
in_flight = False
wait_time = 0.0
Expand Down
5 changes: 5 additions & 0 deletions comfy_api_nodes/util/download_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
diagnose_connectivity,
get_comfy_api_headers,
is_processing_interrupted,
refresh_partner_token,
sleep_with_interrupt,
to_aiohttp_url,
)
Expand Down Expand Up @@ -69,6 +70,10 @@ async def download_url_to_bytesio(
if cls is None:
raise ValueError("For relative 'cloud' paths, the `cls` parameter is required.")
url = urljoin(default_base_url().rstrip("/") + "/", url.lstrip("/"))
try:
await refresh_partner_token(cls)
except asyncio.CancelledError:
raise ProcessingInterrupted("Task cancelled") from None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
headers = get_comfy_api_headers(cls)

while True:
Expand Down
Loading
Loading