Skip to content
Closed
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
153 changes: 153 additions & 0 deletions comfy_api/credential_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import threading
import time
from dataclasses import dataclass, field


DEFAULT_CREDENTIAL_TTL = 2 * 60 * 60
DEFAULT_PROMPT_TTL = 24 * 60 * 60


@dataclass(frozen=True)
class Credential:
token: str | None = field(repr=False)
generation: int


@dataclass
class _ClientCredential:
token: str | None = field(repr=False)
generation: int
updated_at: float
connected: bool = False


@dataclass
class _PromptClient:
client_id: str
updated_at: float


class CredentialRegistry:
def __init__(
self,
*,
credential_ttl: float = DEFAULT_CREDENTIAL_TTL,
prompt_ttl: float = DEFAULT_PROMPT_TTL,
clock=time.monotonic,
):
self.credential_ttl = credential_ttl
self.prompt_ttl = prompt_ttl
self._clock = clock
self._lock = threading.RLock()
self._clients: dict[str, _ClientCredential] = {}
self._prompts: dict[str, _PromptClient] = {}

def update(self, client_id: str, token: str | None) -> int:
now = self._clock()
with self._lock:
self._cleanup(now)
current = self._clients.get(client_id)
if current is None:
generation = 1
elif current.token != token:
generation = current.generation + 1
else:
current.updated_at = now
return current.generation
if current is None:
self._clients[client_id] = _ClientCredential(token, generation, now)
else:
current.token = token
current.generation = generation
current.updated_at = now
return generation

def bind_prompt(self, prompt_id: str, client_id: str) -> bool:
now = self._clock()
with self._lock:
self._cleanup(now)
if prompt_id in self._prompts:
return False
self._prompts[prompt_id] = _PromptClient(client_id, now)
return True

def connect(self, client_id: str) -> None:
now = self._clock()
with self._lock:
self._cleanup(now)
current = self._clients.get(client_id)
if current is None:
self._clients[client_id] = _ClientCredential(None, 0, now, connected=True)
else:
current.connected = True

def get_for_prompt(self, prompt_id: str) -> Credential | None:
now = self._clock()
with self._lock:
self._cleanup(now)
prompt = self._prompts.get(prompt_id)
if prompt is None:
return None
prompt.updated_at = now
current = self._clients.get(prompt.client_id)
if current is None:
return None
return Credential(current.token, current.generation)

def release_prompt(self, prompt_id: str) -> str | None:
with self._lock:
prompt = self._prompts.pop(prompt_id, None)
if prompt is None:
return None
current = self._clients.get(prompt.client_id)
if current is not None and not current.connected and not self._has_prompts(prompt.client_id):
self._clients.pop(prompt.client_id, None)
return prompt.client_id
if current is None and not self._has_prompts(prompt.client_id):
return prompt.client_id
return None

def disconnect(self, client_id: str) -> bool:
with self._lock:
current = self._clients.get(client_id)
if current is None:
return self._has_prompts(client_id)
current.connected = False
if not self._has_prompts(client_id):
self._clients.pop(client_id, None)
return False
return True

def is_protected(self, client_id: str) -> bool:
with self._lock:
self._cleanup(self._clock())
return client_id in self._clients or self._has_prompts(client_id)

def clear(self) -> None:
with self._lock:
self._clients.clear()
self._prompts.clear()

def _has_prompts(self, client_id: str) -> bool:
return any(prompt.client_id == client_id for prompt in self._prompts.values())

def _cleanup(self, now: float) -> None:
expired_prompts = [
prompt_id for prompt_id, prompt in self._prompts.items() if now - prompt.updated_at >= self.prompt_ttl
]
for prompt_id in expired_prompts:
self._prompts.pop(prompt_id, None)

active_clients = {prompt.client_id for prompt in self._prompts.values()}
expired_clients = [
client_id
for client_id, credential in self._clients.items()
if not credential.connected
and client_id not in active_clients
and now - credential.updated_at >= self.credential_ttl
]
for client_id in expired_clients:
self._clients.pop(client_id, None)


api_node_credentials = CredentialRegistry()
12 changes: 10 additions & 2 deletions comfy_api/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ def _parse_cli_feature_flags() -> dict[str, Any]:
_CORE_FEATURE_FLAGS: dict[str, Any] = {
"supports_preview_metadata": True,
"supports_model_type_tags": True,
"comfy_api_credentials": {
"version": 1,
"endpoint": "/api/credentials",
"websocket_auth_message": "credential_auth",
},
"max_upload_size": args.max_upload_size * 1024 * 1024, # Convert MB to bytes
"extension": {"manager": {"supports_v4": True}},
"node_replacements": True,
Expand Down Expand Up @@ -161,11 +166,14 @@ def supports_feature(
return get_connection_feature(sockets_metadata, sid, feature_name, False) is True


def get_server_features() -> dict[str, Any]:
def get_server_features(*, include_credentials: bool = True) -> dict[str, Any]:
"""
Get the server's feature flags.

Returns:
Dictionary of server feature flags
"""
return SERVER_FEATURE_FLAGS.copy()
features = SERVER_FEATURE_FLAGS.copy()
if not include_credentials:
features.pop("comfy_api_credentials", None)
return features
30 changes: 24 additions & 6 deletions comfy_api_nodes/util/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from comfy.comfy_api_env import normalize_comfy_api_base
from comfy.deploy_environment import get_deploy_environment
from comfy.model_management import processing_interrupted
from comfy_api.credential_registry import api_node_credentials
from comfy_api.latest import IO
from comfy_execution.utils import get_executing_context
from comfyui_version import __version__ as comfyui_version
Expand All @@ -33,12 +34,24 @@ def get_node_id(node_cls: type[IO.ComfyNode]) -> str:
return node_cls.hidden.unique_id


def get_auth_header(node_cls: type[IO.ComfyNode]) -> dict[str, str]:
def get_auth_header_with_generation(node_cls: type[IO.ComfyNode]) -> tuple[dict[str, str], int | None]:
ctx = get_executing_context()
if ctx is not None and not node_cls.hidden.api_key_comfy_org:
credential = api_node_credentials.get_for_prompt(ctx.prompt_id)
if credential is not None:
if credential.token:
return {"Authorization": f"Bearer {credential.token}"}, credential.generation
return {}, credential.generation

if node_cls.hidden.auth_token_comfy_org:
return {"Authorization": f"Bearer {node_cls.hidden.auth_token_comfy_org}"}
return {"Authorization": f"Bearer {node_cls.hidden.auth_token_comfy_org}"}, None
if node_cls.hidden.api_key_comfy_org:
return {"X-API-KEY": node_cls.hidden.api_key_comfy_org}
return {}
return {"X-API-KEY": node_cls.hidden.api_key_comfy_org}, None
return {}, None


def get_auth_header(node_cls: type[IO.ComfyNode]) -> dict[str, str]:
return get_auth_header_with_generation(node_cls)[0]


def get_usage_source(node_cls: type[IO.ComfyNode]) -> str:
Expand All @@ -58,16 +71,21 @@ def get_comfy_api_headers(node_cls: type[IO.ComfyNode]) -> dict[str, str]:
relative/cloud URLs resolved against ``default_base_url()``; because the result
includes auth, callers must not attach it to arbitrary absolute/presigned URLs.
"""
return get_comfy_api_headers_with_generation(node_cls)[0]


def get_comfy_api_headers_with_generation(node_cls: type[IO.ComfyNode]) -> tuple[dict[str, str], int | None]:
auth_headers, generation = get_auth_header_with_generation(node_cls)
headers = {
**get_auth_header(node_cls),
**auth_headers,
"Comfy-Env": get_deploy_environment(),
"Comfy-Usage-Source": get_usage_source(node_cls),
"Comfy-Core-Version": comfyui_version,
}
ctx = get_executing_context()
if ctx is not None:
headers["Comfy-Job-Id"] = ctx.prompt_id
return headers
return headers, generation


def default_base_url() -> str:
Expand Down
Loading
Loading