diff --git a/rest_framework_jwt/compat.py b/rest_framework_jwt/compat.py index 241513ac..9d6d8b8b 100644 --- a/rest_framework_jwt/compat.py +++ b/rest_framework_jwt/compat.py @@ -1,16 +1,24 @@ import rest_framework - +from django.forms import widgets from distutils.version import StrictVersion if StrictVersion(rest_framework.VERSION) < StrictVersion('3.0.0'): - from rest_framework.serializers import Serializer + from rest_framework.serializers import Serializer, CharField + + class PasswordField(CharField): + widget = widgets.PasswordInput else: class Serializer(rest_framework.serializers.Serializer): @property def object(self): return self.validated_data + class PasswordField(rest_framework.serializers.CharField): + style = { + 'input_type': 'password' + } + def get_user_model(): try: diff --git a/rest_framework_jwt/serializers.py b/rest_framework_jwt/serializers.py index 5fc7174a..2164bcad 100644 --- a/rest_framework_jwt/serializers.py +++ b/rest_framework_jwt/serializers.py @@ -8,8 +8,10 @@ from rest_framework import serializers from .compat import Serializer -from rest_framework_jwt.compat import get_user_model, get_username_field from rest_framework_jwt.settings import api_settings +from rest_framework_jwt.compat import ( + get_user_model, get_username_field, PasswordField +) User = get_user_model() @@ -34,9 +36,7 @@ def __init__(self, *args, **kwargs): super(JSONWebTokenSerializer, self).__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField() - self.fields['password'] = serializers.CharField( - style={'input_type': 'password'} - ) + self.fields['password'] = PasswordField(write_only=True) @property def username_field(self): diff --git a/tests/test_serializers.py b/tests/test_serializers.py index 46b4dda9..d6b79ce0 100644 --- a/tests/test_serializers.py +++ b/tests/test_serializers.py @@ -40,8 +40,7 @@ def test_empty_drf2(self): def test_empty_drf3(self): serializer = JSONWebTokenSerializer() expected = { - 'username': '', - 'password': '', + 'username': '' } self.assertEqual(serializer.data, expected)