GithubHelp home page GithubHelp logo

Comments (11)

tutturen avatar tutturen commented on May 24, 2024 3

This issue contains helpful documentation, can I suggest that you add it to the main readme? ✍️ 😬

from django-graphql-jwt.

mongkok avatar mongkok commented on May 24, 2024 2

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.

maarcingebala avatar maarcingebala commented on May 24, 2024 2

@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.

mongkok avatar mongkok commented on May 24, 2024 2

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.

mongkok avatar mongkok commented on May 24, 2024

Hi @CBinyenya,
do you have any question?,
can I close the ticket?

from django-graphql-jwt.

felipemfp avatar felipemfp commented on May 24, 2024

@mongkok, maybe is a good idea include this decorators in the package

from django-graphql-jwt.

mongkok avatar mongkok commented on May 24, 2024

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.

mongkok avatar mongkok commented on May 24, 2024

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.

mongkok avatar mongkok commented on May 24, 2024

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.

IdemenB avatar IdemenB commented on May 24, 2024

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.

abdulhafeez1724 avatar abdulhafeez1724 commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.