Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket Connect - Authentication Issue #1801

Open
smannathan opened this issue Jan 24, 2025 · 2 comments
Open

Websocket Connect - Authentication Issue #1801

smannathan opened this issue Jan 24, 2025 · 2 comments
Labels
auth Pertaining to authentication. backend Pertains to the Python backend.

Comments

@smannathan
Copy link

smannathan commented Jan 24, 2025

While running the Chainlit server on Kubernetes, the API can be successfully invoked, and custom authentication with cookies and auth tokens works as expected. However, when the react client application attempts to connect to the server via WebSocket, the connection fails with an authentication error.

2025-01-24 17:41:44 - Authentication failed in websocket connect.
message async handler error
Traceback (most recent call last):
  File "/app/.venv/lib/python3.12/site-packages/engineio/async_server.py", line 509, in run_async_handler
2025-01-24 17:41:44 - message async handler error
Traceback (most recent call last):
  File "/app/.venv/lib/python3.12/site-packages/engineio/async_server.py", line 509, in run_async_handler
    return await self.handlers[event](*args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/socketio/async_server.py", line 688, in _handle_eio_message
    await self._handle_connect(eio_sid, pkt.namespace, pkt.data)
  File "/app/.venv/lib/python3.12/site-packages/socketio/async_server.py", line 552, in _handle_connect
    success = await self._trigger_event(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/socketio/async_server.py", line 640, in _trigger_event
    ret = await handler(*args)
          ^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/chainlit/socket.py", line 120, in connect
    raise ConnectionRefusedError("authentication failed")
ConnectionRefusedError: authentication failed

Code:

from fastapi import FastAPI
from fastapi.responses import JSONResponse
from starlette.middleware.cors import CORSMiddleware
from chainlit.user import User
from chainlit.utils import mount_chainlit
from chainlit.server import _authenticate_user, _get_auth_response, set_auth_cookie
from utils.profiles import auth_user_profiles
from pydantic import BaseModel
from chainlit.auth import create_jwt, decode_jwt, get_configuration, get_current_user
from chainlit.user import User

class Credentials(BaseModel):
    username: str
    password: str

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/custom-auth-v2")
async def custom_auth_v2(credentials: Credentials):
    # Verify the user's identity with custom logic.
    user_profile = auth_user_profiles(credentials.username, credentials.password)

    user = User(identifier=user_profile['identifier'], metadata=user_profile['metadata'])

    access_token = create_jwt(user)
    response = _get_auth_response(access_token, False)
    set_auth_cookie(response, access_token)
    return response

@app.get("/health")
async def health_check():
    # Verify the user's identity with custom logic.
    
    return {"status": "healthy"}

mount_chainlit(app=app, target="./app.py", path="/chainlit")
@dosubot dosubot bot added auth Pertaining to authentication. backend Pertains to the Python backend. labels Jan 24, 2025
@willydouhard
Copy link
Collaborator

Here is a working example of custom authentication with custom frontend

@jmanhype
Copy link

jmanhype commented Jan 25, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth Pertaining to authentication. backend Pertains to the Python backend.
Projects
None yet
Development

No branches or pull requests

3 participants