forked from PedroBern/django-graphql-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutations.py
221 lines (169 loc) · 6.29 KB
/
mutations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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 (
RegisterMixin,
VerifyAccountMixin,
ResendActivationEmailMixin,
SendPasswordResetEmailMixin,
PasswordSetMixin,
PasswordResetMixin,
ObtainJSONWebTokenMixin,
ArchiveAccountMixin,
DeleteAccountMixin,
PasswordChangeMixin,
UpdateAccountMixin,
VerifyOrRefreshOrRevokeTokenMixin,
SendSecondaryEmailActivationMixin,
VerifySecondaryEmailMixin,
SwapEmailsMixin,
RemoveSecondaryEmailMixin,
)
from .utils import normalize_fields
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):
__doc__ = RegisterMixin.__doc__
password_fields = (
[]
if app_settings.ALLOW_PASSWORDLESS_REGISTRATION
else ["password1", "password2"]
)
_required_args = normalize_fields(
app_settings.REGISTER_MUTATION_FIELDS, password_fields
)
_args = app_settings.REGISTER_MUTATION_FIELDS_OPTIONAL
class VerifyAccount(
MutationMixin, DynamicArgsMixin, VerifyAccountMixin, graphene.Mutation
):
__doc__ = VerifyAccountMixin.__doc__
_required_args = ["token"]
class ResendActivationEmail(
MutationMixin, DynamicArgsMixin, ResendActivationEmailMixin, graphene.Mutation
):
__doc__ = ResendActivationEmailMixin.__doc__
_required_args = ["email"]
class SendPasswordResetEmail(
MutationMixin, DynamicArgsMixin, SendPasswordResetEmailMixin, graphene.Mutation
):
__doc__ = SendPasswordResetEmailMixin.__doc__
_required_args = ["email"]
class SendSecondaryEmailActivation(
MutationMixin,
DynamicArgsMixin,
SendSecondaryEmailActivationMixin,
graphene.Mutation,
):
__doc__ = SendSecondaryEmailActivationMixin.__doc__
_required_args = ["email", "password"]
class VerifySecondaryEmail(
MutationMixin, DynamicArgsMixin, VerifySecondaryEmailMixin, graphene.Mutation
):
__doc__ = VerifySecondaryEmailMixin.__doc__
_required_args = ["token"]
class SwapEmails(MutationMixin, DynamicArgsMixin, SwapEmailsMixin, graphene.Mutation):
__doc__ = SwapEmailsMixin.__doc__
_required_args = ["password"]
class RemoveSecondaryEmail(
MutationMixin, DynamicArgsMixin, RemoveSecondaryEmailMixin, graphene.Mutation
):
__doc__ = RemoveSecondaryEmailMixin.__doc__
_required_args = ["password"]
class PasswordSet(MutationMixin, PasswordSetMixin, DynamicArgsMixin, graphene.Mutation):
__doc__ = PasswordSetMixin.__doc__
_required_args = ["token", "new_password1", "new_password2"]
class PasswordReset(
MutationMixin, DynamicArgsMixin, PasswordResetMixin, graphene.Mutation
):
__doc__ = PasswordResetMixin.__doc__
_required_args = ["token", "new_password1", "new_password2"]
class ObtainJSONWebToken(
MutationMixin, ObtainJSONWebTokenMixin, JSONWebTokenMutation
):
__doc__ = ObtainJSONWebTokenMixin.__doc__
user = graphene.Field(UserNode)
unarchiving = graphene.Boolean(default_value=False)
class ArchiveAccount(
MutationMixin, ArchiveAccountMixin, DynamicArgsMixin, graphene.Mutation
):
__doc__ = ArchiveAccountMixin.__doc__
_required_args = ["password"]
class DeleteAccount(
MutationMixin, DeleteAccountMixin, DynamicArgsMixin, graphene.Mutation
):
__doc__ = DeleteAccountMixin.__doc__
_required_args = ["password"]
class PasswordChange(
MutationMixin, PasswordChangeMixin, DynamicArgsMixin, graphene.Mutation
):
__doc__ = PasswordChangeMixin.__doc__
_required_args = ["old_password", "new_password1", "new_password2"]
class UpdateAccount(
MutationMixin, DynamicArgsMixin, UpdateAccountMixin, graphene.Mutation
):
__doc__ = UpdateAccountMixin.__doc__
_args = app_settings.UPDATE_MUTATION_FIELDS
class VerifyToken(MutationMixin, VerifyOrRefreshOrRevokeTokenMixin, graphql_jwt.Verify):
__doc__ = VerifyOrRefreshOrRevokeTokenMixin.__doc__
class RefreshToken(
MutationMixin, VerifyOrRefreshOrRevokeTokenMixin, CustomRefresh
):
__doc__ = VerifyOrRefreshOrRevokeTokenMixin.__doc__
class RevokeToken(MutationMixin, VerifyOrRefreshOrRevokeTokenMixin, graphql_jwt.Revoke):
__doc__ = VerifyOrRefreshOrRevokeTokenMixin.__doc__