Comments (11)
This issue contains helpful documentation, can I suggest that you add it to the main readme? ✍️ 😬
from django-graphql-jwt.
Thanks for the feedback!
Of course, you can protect your queries and mutations verifying the info.context.user
variable:
from django.contrib.auth import get_user_model
import graphene
class Query(graphene.ObjectType):
me = graphene.Field(UserType)
users = graphene.List(UserType)
def resolve_me(self, info, **kwargs):
user = info.context.user
if user.is_anonymous:
raise Exception('Authentication credentials were not provided')
return user
def resolve_users(self, info, **kwargs):
user = info.context.user
if not user.is_active or not user.is_staff:
raise Exception('You do not have permission to perform this action')
return get_user_model().objects.all()
As a shortcut, you can implement a @login_required
and @staff_member_required
decorators:
from functools import wraps
def context(f):
def _context(func):
def wrapper(*args, **kwargs):
info = args[f.__code__.co_varnames.index('info')]
return func(info.context, *args, **kwargs)
return wrapper
return _context
def login_required(f):
@wraps(f)
@context(f)
def wrapper(context, *args, **kwargs):
if context.user.is_anonymous:
raise Exception('Authentication credentials were not provided')
return f(*args, **kwargs)
return wrapper
def staff_member_required(f):
@wraps(f)
@context(f)
def wrapper(context, *args, **kwargs):
user = context.user
if user.is_active and user.is_staff:
return f(*args, **kwargs)
raise Exception('You do not have permission to perform this action')
return wrapper
Using decorators...
from django.contrib.auth import get_user_model
import graphene
class Query(graphene.ObjectType):
me = graphene.Field(UserType)
users = graphene.List(UserType)
@login_required
def resolve_me(self, info, **kwargs):
return info.context.user
@staff_member_required
def resolve_users(self, info, **kwargs):
return get_user_model().objects.all()
The same for mutations.
from django-graphql-jwt.
@mongkok Shouldn't these decorators be a part of this package? I guess most people who use this package will need them anyway. Also, in the README there is a reference to login_requried
, but it doesn't say to which one - Django's or a custom one. My intuition was to look for these decorators inside this package.
Aside from that, thanks for a very useful piece of code, it does exactly what I was looking for.
from django-graphql-jwt.
I've included the auth decorators, you can find a full list of them and examples on the documentation.
Thanks for all your comments.
from django-graphql-jwt.
Hi @CBinyenya,
do you have any question?,
can I close the ticket?
from django-graphql-jwt.
@mongkok, maybe is a good idea include this decorators in the package
from django-graphql-jwt.
Hi @felipemfp,
decorators authenticate using any backend included in the AUTHENTICATION_BACKENDS
settings, I think it could be added in the Graphene Django framework, there is a related issue.
from django-graphql-jwt.
Yes, you are quite right, I should include it in the README :)
@tutturen @felipemfp, in case it can help you, I developed a package with these decorators.
https://github.com/flavors/django-graphql-extensions
from django-graphql-jwt.
Thanks @elwoodxblues,
there is a link to the [wiki] at the beginning, it might not be seen.
In my opinion, auth decorators are for general use, it can also be used for any backend included in the AUTHENTICATION_BACKENDS
, that's why I think it should be included in the graphene-django framework.
On the other hand I have my doubts, to include the decorators in this library would be of great help for all of us.
At the moment I reopen the issue.
from django-graphql-jwt.
Hi @mongkok, thanks a lot for making so many people's lives easier with your effort.
I'd like to have creation/verification/refreshing of tokens unprotected and all others that can be reached through the global schema protected. Can I ask what is the proper way of protecting the whole schema with the JWT using the lib? What I mean is, something not repeated rather than defininq @login_required for each Query and Mutation separately.
from django-graphql-jwt.
This is a helpful project, I just have one question, is there a way to protect mutations and queries from unauthorized uses?
Hi can you help me how to put token in header ?
from django-graphql-jwt.
Related Issues (20)
- Is it possible to turn the token field names configurable by settings?
- Understanding jwt_payload util method
- modulenotfounderror: no module named 'graphql_jwt'
- How to delete a Token from the server-side (mutation). when a user gives a logout the token will be destroyed from server-side by using mutation? is it possible? If so. please explain. HOT 2
- How can I create/set JWT Token in Django view and then redirect to Frontend? HOT 1
- fails if root object types not named Query and Mutation HOT 3
- Cant Delete Cookies With Same Settings with tokenAuth Mutation
- Implement a log out HOT 1
- TypeError: Signal.__init__() got an unexpected keyword argument 'providing_args' HOT 1
- Selective application of JWT expiration check for specific operations
- import error from strawberry_django.utlis
- Tests fails in year 2038
- Graphene 3 support? HOT 2
- TypeError: JSONWebTokenMiddleware.__init__() takes 1 positional argument but 2 were given
- from django.utils.translation import ugettext as _
- Add custom key-value pair in JWT payload.
- cannot import name 'ugettext' from 'django.utils.translation' HOT 4
- Add fingerprint to token and cookies to prevent sidejacking
- Case sensitive Username
- TypeError: Signal.__init__() got an unexpected keyword argument 'providing_args' HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-graphql-jwt.