Skip to content
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

add graphene3 support #1

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions graphql_auth/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
else:
async_email_func = None

def add_dynamic_fields(cls):
setattr(cls, "token", graphene.Field(graphene.String))
if using_refresh_tokens():
setattr(cls, "refresh_token", graphene.Field(graphene.String))
return cls

@add_dynamic_fields
class RegisterMixin(Output):
"""
Register user with fields defined in the settings.
Expand Down Expand Up @@ -70,14 +76,6 @@ class RegisterMixin(Output):
else RegisterForm
)

@classmethod
def Field(cls, *args, **kwargs):
if app_settings.ALLOW_LOGIN_NOT_VERIFIED:
if using_refresh_tokens():
cls._meta.fields["refresh_token"] = graphene.Field(graphene.String)
cls._meta.fields["token"] = graphene.Field(graphene.String)
return super().Field(*args, **kwargs)

@classmethod
@token_auth
def login_on_register(cls, root, info, **kwargs):
Expand Down Expand Up @@ -477,7 +475,7 @@ def resolve_action(cls, user, *args, **kwargs):
user.save(update_fields=["is_active"])
revoke_user_refresh_token(user=user)


@add_dynamic_fields
class PasswordChangeMixin(Output):
"""
Change account password when user knows the old password.
Expand All @@ -487,13 +485,6 @@ class PasswordChangeMixin(Output):

form = PasswordChangeForm

@classmethod
def Field(cls, *args, **kwargs):
if using_refresh_tokens():
cls._meta.fields["refresh_token"] = graphene.Field(graphene.String)
cls._meta.fields["token"] = graphene.Field(graphene.String)
return super().Field(*args, **kwargs)

@classmethod
@token_auth
def login_on_password_change(cls, root, info, **kwargs):
Expand Down
80 changes: 71 additions & 9 deletions graphql_auth/mutations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import graphene
import graphql_jwt
from django.contrib.auth import get_user_model
from graphql_jwt.decorators import token_auth
from graphene.types.generic import GenericScalar
from graphql_jwt.settings import jwt_settings
from graphql_jwt.mixins import RefreshTokenMixin, KeepAliveRefreshMixin

from .bases import MutationMixin, DynamicArgsMixin
from .mixins import (
Expand All @@ -24,6 +29,70 @@
from .settings import graphql_auth_settings as app_settings
from .schema import UserNode

class JSONWebTokenMixin:
payload = GenericScalar()
refresh_expires_in = graphene.Int()

@classmethod
def Field(cls, *args, **kwargs):
if not jwt_settings.JWT_HIDE_TOKEN_FIELDS:
cls._meta.fields["token"] = graphene.Field(graphene.String)

if jwt_settings.JWT_LONG_RUNNING_REFRESH_TOKEN:
cls._meta.fields["refresh_token"] = graphene.Field(
graphene.String,
)

return super().Field(*args, **kwargs)

class CustomObtainJSONWebTokenMixin(JSONWebTokenMixin):
@classmethod
def __init_subclass_with_meta__(cls, name=None, **options):
assert getattr(cls, "resolve", None), (
f"{name or cls.__name__}.resolve "
"method is required in a JSONWebTokenMutation."
)

super().__init_subclass_with_meta__(name=name, **options)

class JSONWebTokenMutation(CustomObtainJSONWebTokenMixin, graphene.Mutation):
class Meta:
abstract = True

@classmethod
def Field(cls, *args, **kwargs):
cls._meta.arguments.update(
{
get_user_model().USERNAME_FIELD: graphene.String(required=True),
"password": graphene.String(required=True),
},
)
return super().Field(*args, **kwargs)

@classmethod
@token_auth
def mutate(cls, root, info, **kwargs):
return cls.resolve(root, info, **kwargs)

class RefreshMixin(
(
RefreshTokenMixin
if jwt_settings.JWT_LONG_RUNNING_REFRESH_TOKEN
else KeepAliveRefreshMixin
),
JSONWebTokenMixin,
):
"""RefreshMixin"""


class CustomRefresh(RefreshMixin, graphene.Mutation):
class Arguments(RefreshMixin.Fields):
"""Refresh Arguments"""

@classmethod
def mutate(cls, *arg, **kwargs):
return cls.refresh(*arg, **kwargs)


class Register(MutationMixin, DynamicArgsMixin, RegisterMixin, graphene.Mutation):

Expand Down Expand Up @@ -103,19 +172,12 @@ class PasswordReset(


class ObtainJSONWebToken(
MutationMixin, ObtainJSONWebTokenMixin, graphql_jwt.JSONWebTokenMutation
MutationMixin, ObtainJSONWebTokenMixin, JSONWebTokenMutation
):
__doc__ = ObtainJSONWebTokenMixin.__doc__
user = graphene.Field(UserNode)
unarchiving = graphene.Boolean(default_value=False)

@classmethod
def Field(cls, *args, **kwargs):
cls._meta.arguments.update({"password": graphene.String(required=True)})
for field in app_settings.LOGIN_ALLOWED_FIELDS:
cls._meta.arguments.update({field: graphene.String()})
return super(graphql_jwt.JSONWebTokenMutation, cls).Field(*args, **kwargs)


class ArchiveAccount(
MutationMixin, ArchiveAccountMixin, DynamicArgsMixin, graphene.Mutation
Expand Down Expand Up @@ -150,7 +212,7 @@ class VerifyToken(MutationMixin, VerifyOrRefreshOrRevokeTokenMixin, graphql_jwt.


class RefreshToken(
MutationMixin, VerifyOrRefreshOrRevokeTokenMixin, graphql_jwt.Refresh
MutationMixin, VerifyOrRefreshOrRevokeTokenMixin, CustomRefresh
):
__doc__ = VerifyOrRefreshOrRevokeTokenMixin.__doc__

Expand Down
8 changes: 2 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ def get_version(package):
),
packages=find_packages(exclude=["tests*"]),
install_requires=[
"Django>=2.2.0",
"django-graphql-jwt==0.3.0",
"django-filter>=2.2.0",
"graphene_django>=2.1.8",
"graphene>=2.1.8",
"PyJWT<2.0.0",
"django-graphql-jwt==0.4.0",
"PyJWT==2.8.0",
],
tests_require=tests_require,
classifiers=[
Expand Down