fanout / django-eventstream

Server-Sent Events for Django
MIT License
664 stars 88 forks source link

Headers are missing in django-eventstream url patterns #130

Closed whatthehellisgoinon closed 1 year ago

whatthehellisgoinon commented 1 year ago

I'm trying to put Auth on django-eventstream urls but it seems im not able to get the appropriate headers to work with.

what have i tried is:-

settings.py

 EVENTSTREAM_ALLOW_CREDENTIALS = True
EVENTSTREAM_ALLOW_ORIGIN = "http://localhost:5173" 
EVENTSTREAM_ALLOW_HEADERS = "authorization"

CORS_ALLOW_HEADERS = [
    "accept",
    "authorization",
    "content-type",
    "user-agent",
    "x-csrftoken",
    "x-requested-with",
    "baggage",
    "sentry-trace",
]

asgi.py

application = ProtocolTypeRouter(
    {
        "http": 
            URLRouter(
                [
                    path(
                        "chat/events/<obj>",
                        TokenAuthMiddlewareStack(URLRouter(django_eventstream.routing.urlpatterns)),
                        {"format-channels": ["test-{obj}"]},
                    ),
                    re_path(r"", get_asgi_application()),
                ]
            ),
        }
    )

middelware.py

# Handy shortcut for applying all three layers at once
def TokenAuthMiddlewareStack(inner):
    """
    middleware to support websocket ssh connection
    from both session or by queries
    """
    return AuthMiddlewareStack(TokenAuthMiddleware(inner))

class TokenAuthMiddleware:
    def __init__(self, app):
        self.app = app

    async def __call__(self, scope, receive, send):
        print(scope) <------------ when i try to see then nothing is presented token, session-id, and csrf(idk if csrf should be present!)
        user = await get_user_from_headers_or_queries(scope)
        if user is not None:
            scope["user"] = user
        return await self.app(scope, receive, send)

any help ?

whatthehellisgoinon commented 1 year ago

NVM..

i need to use withCredentials: true.. which i was not using since past two days!

es = new EventSource(path, {withCredentials: true,})