-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
AuthenticationMiddleware must be installed to access request.user #6
Comments
@erikdewildt can you share sample code to reproduce the error. |
@Usama0121 I'm getting the same thing that @erikdewildt is getting. At least for me I believe it has something to do with my asgi file. This is my resolver with login required.
In my asgi file I have tried
Both end up giving my the following error.
If I revert back to |
The following code resolves the AuthenticationMiddleware error but currently, ariadne_jwt does not support this middleware style import os
from ariadne.asgi import GraphQL
from channels.routing import URLRouter
from channels.auth import AuthMiddlewareStack
from django.core.asgi import get_asgi_application
from django.urls import path, re_path
from .schema import schema
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_1.settings')
application = (AuthMiddlewareStack(URLRouter([
path('graphql/', GraphQL(schema=schema), name='graphql'),
re_path(r"", get_asgi_application())
]))) |
Now I can't get a user token on my login mutation. Getting the error.
|
@sebastian-chang as I mentioned in my last comment jwt authentication is currently not supported. This is mainly because jwt authentication relies on request headers whereas channels use web socket which does not process headers in a supported way. A separate middleware is required to handle auth via channels |
Thanks for your answers, some pointers to a solution where given in this ticket: As @Usama0121 also mentioned some kind of custom middleware is required. I've tried to apply the AuthMiddlewareStack in in my routing.py:
This allows for me to access a user in the request scope. I've tried to define a simple permission_required mixin to do so (which now only prints the user):
When I apply this decorator to the subscription generator function a user can be resolved when I subscribe using the playground. However this is not the user for which the JWT token is set in the HTTP Headers as these are not used in the subscriptions. It seems to be the user which currently has a HTTP Session in the same browser towards the Django backend. If I did not open up a session first towards the Django backend a AnonymousUser is resolved. As a workaround I was thinking of defining a 'JWT' argument to the subscription and then resolve the user from the JWT token. When I have the user belonging to the token I can then validate on permissions. For me it would be acceptable to use a argument in the subscription to provide the JWT token. Any thoughts on that? Did not investigate further, will dive into it tomorrow again, but perhaps you have any pointers on how to resolve the JWT token back to a user instance? Thanks again, Regards, |
Hi,
First of all thanks for your package!
I can get the JTW token authorisation working with normal queries. However when decorate the resolver method of a subscription with
login_required
decorator i run into an error:The subscriptions is build using the Django-channels setup in the documentation. Both MIDDLEWARE and AUTHENTICATION_BACKENDS are properly configured.
Debugging shows that the code is trying to use
context.get('request').user
.When i don't use the
login_required
decorator and have a look at the info.context["request"] using a breakpoint in the resolver the request is astarlette.websockets.WebSocket
object.Accessing the
user
property of this WebSocket object results in the sameAssertionError: AuthenticationMiddleware must be installed to access request.user
error.Any advice on how to get the
login_required
decorator working with a subscription resolver method?Thanks in advance!
Regards,
Erik
The text was updated successfully, but these errors were encountered: