Skip to content

Commit

Permalink
Fix authentication for ComfyUI-Login
Browse files Browse the repository at this point in the history
Fixes #1284

Add bearer token support for ComfyUI-Login compatibility.

* Add `bearer` parameter to `ComfyClient.connect` method to accept the token.
* Modify WebSocket connection URL in `ComfyClient.connect` to include bearer token in the `extra_headers`.
* Update `_get` and `_post` methods in `ComfyClient` to pass the bearer token correctly.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Acly/krita-ai-diffusion/issues/1284?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
yusefes committed Oct 20, 2024
1 parent b5b0b1a commit d4aad47
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions ai_diffusion/comfy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ class ComfyClient(Client):

default_url = "http://127.0.0.1:8188"

def __init__(self, url):
def __init__(self, url, bearer=""):
self.url = url
self.bearer = bearer
self.models = ClientModels()
self._requests = RequestManager()
self._id = str(uuid.uuid4())
Expand All @@ -103,7 +104,7 @@ def __init__(self, url):

@staticmethod
async def connect(url=default_url, access_token=""):
client = ComfyClient(parse_url(url))
client = ComfyClient(parse_url(url), bearer=access_token)
log.info(f"Connecting to {client.url}")

# Retrieve system info
Expand All @@ -113,7 +114,7 @@ async def connect(url=default_url, access_token=""):
# Try to establish websockets connection
wsurl = websocket_url(client.url)
try:
async with websockets_client.connect(f"{wsurl}/ws?clientId={client._id}"):
async with websockets_client.connect(f"{wsurl}/ws?clientId={client._id}", extra_headers={"Authorization": f"Bearer {client.bearer}"}):
pass
except Exception as e:
msg = _("Could not establish websocket connection at") + f" {wsurl}: {str(e)}"
Expand Down Expand Up @@ -179,10 +180,10 @@ async def connect(url=default_url, access_token=""):
return client

async def _get(self, op: str):
return await self._requests.get(f"{self.url}/{op}")
return await self._requests.get(f"{self.url}/{op}", bearer=self.bearer)

async def _post(self, op: str, data: dict):
return await self._requests.post(f"{self.url}/{op}", data)
return await self._requests.post(f"{self.url}/{op}", data, bearer=self.bearer)

async def enqueue(self, work: WorkflowInput, front: bool = False):
job = JobInfo.create(work, front=front)
Expand Down Expand Up @@ -228,7 +229,7 @@ async def _run_job(self, job: JobInfo):
async def _listen(self):
url = websocket_url(self.url)
async for websocket in websockets_client.connect(
f"{url}/ws?clientId={self._id}", max_size=2**30, read_limit=2**30, ping_timeout=60
f"{url}/ws?clientId={self._id}", max_size=2**30, read_limit=2**30, ping_timeout=60, extra_headers={"Authorization": f"Bearer {self.bearer}"}
):
try:
await self._subscribe_workflows()
Expand Down

0 comments on commit d4aad47

Please sign in to comment.