-
Notifications
You must be signed in to change notification settings - Fork 177
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
JSONWebTokenMiddleware always returns Anonymous User - Possible Bug & Fix #291
Comments
I think there's a little more to this, but I haven't quite been able to figure out what's going on. I forked this repo and made the change to allow_any I suggested above, thinking that would fix the problem. It did not. When I installed from that repo instead of your package, I still kept getting messages that I wasn't authorized. I did some more digging and it appears under the following conditions I can get everything to work:
This fixes the issue and no change was needed to allow_any. I got confused when I was trying to bug trace because I had to create local copies to drop in the print statements, and somehow doing that is solving my issue. At first glance, my backend.py and JSONWebTokenBackend module is identical to the one your library ships with save for the print statements I'm using. I'm having trouble figuring out why the above combination of steps appear to fix things. I'll keep digging here, but wanted to report this in the hopes it's helpful to others in the interim. |
Ok, this got even stranger... I've further narrowed down the behavior here:
It seems like something about this bug is triggered in connection with allow_any. Anyone else have any insights here? |
I'm still not sure why I'm seeing this behavior, but it is NOT due to the your package. I started with a fresh Django project and your package works flawlessly as documented. I haven't figured out why I'm seeing the weird behavior I documented, but I am rebuilding my project one step at a time to see if it comes up again. If I manage to isolate the cause, I'll let you folks know. |
We had the same issue with graphene v3. As a workaround, we copy-pasted the
|
@msimav, thanks for confirming I'm not crazy here! Maybe it's the graphene version that's causing this. I have to check what versions pip installed, but I have two graphene projects - one I originally opened this issue for and another personal project. I rebuilt the project I opened the issue for was able to use the default django-graphql-jwt configurations with no problems at all. At that point, I closed this issue. My other stack, however, still refused to return anything other than an AnonymousUser. I solved that by modifying allow_any and setting it as the
|
Today I created a new project from scratch to see if the problem is related to our codebase. ProblemIn our codebase, we named our query class RootQuery(
AQueryMixin,
BQueryMixin,
CQueryMixin,
graphene.ObjectType,
):
pass
schema = graphene.Schema(query=RootQuery, mutation=RootMutation) But def allow_any(info, **kwargs):
operation_name = get_operation_name(info.operation.operation).title()
# operation_name == "Query"
field = info.schema.get_type(operation_name).fields.get(info.field_name)
# info.schema.get_type("Query") == None
# so info.schema.get_type("Query").fields raises an error
# It should have been info.schema.get_type("RootQuery") SolutionRenaming |
@msimav, I'll try to give it a shot this weekend. |
Finally had some time to work on this again... So, first off, my root Query is called Query, so I don't think I have the same root cause as you, @msimav. This bug is definitely caused somehow by the operation names, however, as this is my stack trace when I tried the other fix you suggested using from #2f670d7:
This is the error I was seeing when I first opened this issue. The fix that works for me (and which, as I said, I am a little uncomfortable with as I don't really understand the root cause here and I'm using a naive try / except) is the allow_any method I suggested above:
|
Any idea on what is the purpose of checking if the fields attribute exists in the first place? |
I don't understand what's happening, but renaming my query from |
is it renaming all root queries in the files to Query that fixed it ? |
This worked for me, thanks. |
18 months later, I lost a half day trying to work out why a code refactor had broken JWT authentication.....and it was exactly this. Great post! |
Hi Folks,
LOVE this project and its amazing contributions to the Graphene / GraphQL ecosystem in Python. I've been running into a weird bug that I think I finally traced and seems to be in your allow_any method in your JSONWebTokenMiddleware. I keep trying to authenticate with a JWT token and keep getting an error that my user is not authenticated. I had a JWT token and it was valid, so I tried to trace where this was coming from in your JSONWebTokenMiddleware with this modified code:
Turns out the user keeps coming back as AnonymousUser:
I traced the code and found your allow_any method appears to be throwing an unexpected error which is handled by the Middleware as if the User is invalid.
This line in allow_any:
field = info.schema.get_type(operation_name).fields.get(info.field_name)
Results in this error:
"'NoneType' object has no attribute 'fields'"
It looks like, when I send a query for a model called corpus, for example, the operation_name argument resolves to "Query". When I look at info.schema.types, however, Query is not one of the types. The CorpusNode name is in there, however. So, when I change the field = call to look to resolve to get_type('CorpusNode')...
field = info.schema.get_type(info.return_type.graphene_type._meta.node._meta.name).fields.get(info.field_name)
It works (with the same JWT token that said I was unauthorized before)!
It looks like others have run into similar-ish sounding problems (see recent issue #289 where someone notes there is a problem in the Middleware), so possibly this is unexpected behavior? Not sure why more people aren't running into this if it's a bug though. If it helps, here's the current list of graphene-related packages I'm using.
The text was updated successfully, but these errors were encountered: