|
1
|
|
|
from django.contrib.auth import authenticate, get_user_model |
|
|
|
|
|
|
2
|
|
|
from django.contrib.auth.password_validation import validate_password |
|
|
|
|
|
|
3
|
|
|
from django.core import exceptions as django_exceptions |
|
|
|
|
|
|
4
|
|
|
from django.db import IntegrityError, transaction |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
from rest_framework import exceptions, serializers |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
from djoser import constants, utils |
|
9
|
|
|
from djoser.compat import get_user_email, get_user_email_field_name |
|
10
|
|
|
from djoser.conf import settings |
|
11
|
|
|
|
|
12
|
|
|
User = get_user_model() |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class UserSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
16
|
|
|
class Meta: |
|
|
|
|
|
|
17
|
|
|
model = User |
|
18
|
|
|
fields = tuple(User.REQUIRED_FIELDS) + ( |
|
19
|
|
|
User._meta.pk.name, |
|
|
|
|
|
|
20
|
|
|
User.USERNAME_FIELD, |
|
21
|
|
|
) |
|
22
|
|
|
read_only_fields = (User.USERNAME_FIELD,) |
|
23
|
|
|
|
|
24
|
|
|
def update(self, instance, validated_data): |
|
|
|
|
|
|
25
|
|
|
email_field = get_user_email_field_name(User) |
|
26
|
|
|
if settings.SEND_ACTIVATION_EMAIL and email_field in validated_data: |
|
27
|
|
|
instance_email = get_user_email(instance) |
|
28
|
|
|
if instance_email != validated_data[email_field]: |
|
29
|
|
|
instance.is_active = False |
|
30
|
|
|
instance.save(update_fields=['is_active']) |
|
31
|
|
|
return super(UserSerializer, self).update(instance, validated_data) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class UserCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
35
|
|
|
password = serializers.CharField( |
|
36
|
|
|
style={'input_type': 'password'}, |
|
37
|
|
|
write_only=True |
|
38
|
|
|
) |
|
39
|
|
|
|
|
40
|
|
|
default_error_messages = { |
|
41
|
|
|
'cannot_create_user': constants.CANNOT_CREATE_USER_ERROR, |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
class Meta: |
|
|
|
|
|
|
45
|
|
|
model = User |
|
46
|
|
|
fields = tuple(User.REQUIRED_FIELDS) + ( |
|
47
|
|
|
User.USERNAME_FIELD, User._meta.pk.name, 'password', |
|
|
|
|
|
|
48
|
|
|
) |
|
49
|
|
|
|
|
50
|
|
|
def validate(self, attrs): |
|
|
|
|
|
|
51
|
|
|
user = User(**attrs) |
|
52
|
|
|
password = attrs.get('password') |
|
53
|
|
|
|
|
54
|
|
|
try: |
|
55
|
|
|
validate_password(password, user) |
|
56
|
|
|
except django_exceptions.ValidationError as e: |
|
|
|
|
|
|
57
|
|
|
raise serializers.ValidationError({'password': list(e.messages)}) |
|
58
|
|
|
|
|
59
|
|
|
return attrs |
|
60
|
|
|
|
|
61
|
|
|
def create(self, validated_data): |
|
|
|
|
|
|
62
|
|
|
try: |
|
63
|
|
|
user = self.perform_create(validated_data) |
|
64
|
|
|
except IntegrityError: |
|
65
|
|
|
self.fail('cannot_create_user') |
|
66
|
|
|
|
|
67
|
|
|
return user |
|
68
|
|
|
|
|
69
|
|
|
def perform_create(self, validated_data): |
|
|
|
|
|
|
70
|
|
|
with transaction.atomic(): |
|
71
|
|
|
user = User.objects.create_user(**validated_data) |
|
72
|
|
|
if settings.SEND_ACTIVATION_EMAIL: |
|
73
|
|
|
user.is_active = False |
|
74
|
|
|
user.save(update_fields=['is_active']) |
|
75
|
|
|
return user |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
class TokenCreateSerializer(serializers.Serializer): |
|
|
|
|
|
|
79
|
|
|
password = serializers.CharField( |
|
80
|
|
|
required=False, style={'input_type': 'password'} |
|
81
|
|
|
) |
|
82
|
|
|
|
|
83
|
|
|
default_error_messages = { |
|
84
|
|
|
'invalid_credentials': constants.INVALID_CREDENTIALS_ERROR, |
|
85
|
|
|
'inactive_account': constants.INACTIVE_ACCOUNT_ERROR, |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
def __init__(self, *args, **kwargs): |
|
89
|
|
|
super(TokenCreateSerializer, self).__init__(*args, **kwargs) |
|
90
|
|
|
self.user = None |
|
91
|
|
|
self.fields[User.USERNAME_FIELD] = serializers.CharField( |
|
92
|
|
|
required=False |
|
93
|
|
|
) |
|
94
|
|
|
|
|
95
|
|
|
def validate(self, attrs): |
|
|
|
|
|
|
96
|
|
|
self.user = authenticate( |
|
97
|
|
|
username=attrs.get(User.USERNAME_FIELD), |
|
98
|
|
|
password=attrs.get('password') |
|
99
|
|
|
) |
|
100
|
|
|
|
|
101
|
|
|
self._validate_user_exists(self.user) |
|
102
|
|
|
self._validate_user_is_active(self.user) |
|
103
|
|
|
return attrs |
|
104
|
|
|
|
|
105
|
|
|
def _validate_user_exists(self, user): |
|
106
|
|
|
if not user: |
|
107
|
|
|
self.fail('invalid_credentials') |
|
108
|
|
|
|
|
109
|
|
|
def _validate_user_is_active(self, user): |
|
110
|
|
|
if not user.is_active: |
|
111
|
|
|
self.fail('inactive_account') |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
class PasswordResetSerializer(serializers.Serializer): |
|
|
|
|
|
|
115
|
|
|
email = serializers.EmailField() |
|
116
|
|
|
|
|
117
|
|
|
default_error_messages = {'email_not_found': constants.EMAIL_NOT_FOUND} |
|
118
|
|
|
|
|
119
|
|
|
def validate_email(self, value): |
|
|
|
|
|
|
120
|
|
|
users = self.context['view'].get_users(value) |
|
121
|
|
|
if settings.PASSWORD_RESET_SHOW_EMAIL_NOT_FOUND and not users: |
|
122
|
|
|
self.fail('email_not_found') |
|
123
|
|
|
else: |
|
124
|
|
|
return value |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
class UidAndTokenSerializer(serializers.Serializer): |
|
|
|
|
|
|
128
|
|
|
uid = serializers.CharField() |
|
129
|
|
|
token = serializers.CharField() |
|
130
|
|
|
|
|
131
|
|
|
default_error_messages = { |
|
132
|
|
|
'invalid_token': constants.INVALID_TOKEN_ERROR, |
|
133
|
|
|
'invalid_uid': constants.INVALID_UID_ERROR, |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
def validate_uid(self, value): |
|
|
|
|
|
|
137
|
|
|
try: |
|
138
|
|
|
uid = utils.decode_uid(value) |
|
139
|
|
|
self.user = User.objects.get(pk=uid) |
|
|
|
|
|
|
140
|
|
|
except (User.DoesNotExist, ValueError, TypeError, OverflowError): |
|
141
|
|
|
self.fail('invalid_uid') |
|
142
|
|
|
|
|
143
|
|
|
return value |
|
144
|
|
|
|
|
145
|
|
|
def validate(self, attrs): |
|
|
|
|
|
|
146
|
|
|
attrs = super(UidAndTokenSerializer, self).validate(attrs) |
|
147
|
|
|
is_token_valid = self.context['view'].token_generator.check_token( |
|
148
|
|
|
self.user, attrs['token'] |
|
149
|
|
|
) |
|
150
|
|
|
if is_token_valid: |
|
151
|
|
|
return attrs |
|
152
|
|
|
else: |
|
153
|
|
|
self.fail('invalid_token') |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
class ActivationSerializer(UidAndTokenSerializer): |
|
|
|
|
|
|
157
|
|
|
default_error_messages = {'stale_token': constants.STALE_TOKEN_ERROR} |
|
158
|
|
|
|
|
159
|
|
|
def validate(self, attrs): |
|
160
|
|
|
attrs = super(ActivationSerializer, self).validate(attrs) |
|
161
|
|
|
if not self.user.is_active: |
|
162
|
|
|
return attrs |
|
163
|
|
|
raise exceptions.PermissionDenied(self.error_messages['stale_token']) |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
class PasswordSerializer(serializers.Serializer): |
|
|
|
|
|
|
167
|
|
|
new_password = serializers.CharField(style={'input_type': 'password'}) |
|
168
|
|
|
|
|
169
|
|
|
def validate(self, attrs): |
|
|
|
|
|
|
170
|
|
|
user = self.context['request'].user or self.user |
|
171
|
|
|
assert user is not None |
|
172
|
|
|
|
|
173
|
|
|
try: |
|
174
|
|
|
validate_password(attrs['new_password'], user) |
|
175
|
|
|
except django_exceptions.ValidationError as e: |
|
|
|
|
|
|
176
|
|
|
raise serializers.ValidationError({ |
|
177
|
|
|
'new_password': list(e.messages) |
|
178
|
|
|
}) |
|
179
|
|
|
return super(PasswordSerializer, self).validate(attrs) |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
class PasswordRetypeSerializer(PasswordSerializer): |
|
|
|
|
|
|
183
|
|
|
re_new_password = serializers.CharField(style={'input_type': 'password'}) |
|
184
|
|
|
|
|
185
|
|
|
default_error_messages = { |
|
186
|
|
|
'password_mismatch': constants.PASSWORD_MISMATCH_ERROR, |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
def validate(self, attrs): |
|
190
|
|
|
attrs = super(PasswordRetypeSerializer, self).validate(attrs) |
|
191
|
|
|
if attrs['new_password'] == attrs['re_new_password']: |
|
192
|
|
|
return attrs |
|
193
|
|
|
else: |
|
194
|
|
|
self.fail('password_mismatch') |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
class CurrentPasswordSerializer(serializers.Serializer): |
|
|
|
|
|
|
198
|
|
|
current_password = serializers.CharField(style={'input_type': 'password'}) |
|
199
|
|
|
|
|
200
|
|
|
default_error_messages = { |
|
201
|
|
|
'invalid_password': constants.INVALID_PASSWORD_ERROR, |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
def validate_current_password(self, value): |
|
|
|
|
|
|
205
|
|
|
is_password_valid = self.context['request'].user.check_password(value) |
|
206
|
|
|
if is_password_valid: |
|
207
|
|
|
return value |
|
208
|
|
|
else: |
|
209
|
|
|
self.fail('invalid_password') |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
class SetPasswordSerializer(PasswordSerializer, CurrentPasswordSerializer): |
|
|
|
|
|
|
213
|
|
|
pass |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
class SetPasswordRetypeSerializer(PasswordRetypeSerializer, |
|
|
|
|
|
|
217
|
|
|
CurrentPasswordSerializer): |
|
218
|
|
|
pass |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
class PasswordResetConfirmSerializer(UidAndTokenSerializer, |
|
|
|
|
|
|
222
|
|
|
PasswordSerializer): |
|
223
|
|
|
pass |
|
224
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
class PasswordResetConfirmRetypeSerializer(UidAndTokenSerializer, |
|
|
|
|
|
|
227
|
|
|
PasswordRetypeSerializer): |
|
228
|
|
|
pass |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
class UserDeleteSerializer(CurrentPasswordSerializer): |
|
|
|
|
|
|
232
|
|
|
pass |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
class SetUsernameSerializer(serializers.ModelSerializer, |
|
|
|
|
|
|
236
|
|
|
CurrentPasswordSerializer): |
|
237
|
|
|
|
|
238
|
|
|
class Meta(object): |
|
|
|
|
|
|
239
|
|
|
model = User |
|
240
|
|
|
fields = (User.USERNAME_FIELD, 'current_password') |
|
241
|
|
|
|
|
242
|
|
|
def __init__(self, *args, **kwargs): |
|
243
|
|
|
""" |
|
244
|
|
|
This method should probably be replaced by a better solution. |
|
245
|
|
|
Its purpose is to replace USERNAME_FIELD with 'new_' + USERNAME_FIELD |
|
246
|
|
|
so that the new field is being assigned a field for USERNAME_FIELD |
|
247
|
|
|
""" |
|
248
|
|
|
super(SetUsernameSerializer, self).__init__(*args, **kwargs) |
|
249
|
|
|
username_field = User.USERNAME_FIELD |
|
250
|
|
|
self.fields['new_' + username_field] = self.fields.pop(username_field) |
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
class SetUsernameRetypeSerializer(SetUsernameSerializer): |
|
|
|
|
|
|
254
|
|
|
default_error_messages = { |
|
255
|
|
|
'username_mismatch': constants.USERNAME_MISMATCH_ERROR.format( |
|
256
|
|
|
User.USERNAME_FIELD |
|
257
|
|
|
), |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
def __init__(self, *args, **kwargs): |
|
261
|
|
|
super(SetUsernameRetypeSerializer, self).__init__(*args, **kwargs) |
|
262
|
|
|
self.fields['re_new_' + User.USERNAME_FIELD] = serializers.CharField() |
|
263
|
|
|
|
|
264
|
|
|
def validate(self, attrs): |
|
|
|
|
|
|
265
|
|
|
attrs = super(SetUsernameRetypeSerializer, self).validate(attrs) |
|
266
|
|
|
new_username = attrs[User.USERNAME_FIELD] |
|
267
|
|
|
if new_username != attrs['re_new_' + User.USERNAME_FIELD]: |
|
268
|
|
|
self.fail('username_mismatch') |
|
269
|
|
|
else: |
|
270
|
|
|
return attrs |
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
class TokenSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
274
|
|
|
auth_token = serializers.CharField(source='key') |
|
275
|
|
|
|
|
276
|
|
|
class Meta: |
|
|
|
|
|
|
277
|
|
|
model = settings.TOKEN_MODEL |
|
278
|
|
|
fields = ('auth_token',) |
|
279
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.