Ad

Password Not Encrypted During Save Update

I am trying to create a user using api. With my post/put request, my password is not saved in encrypted form. My code is as below.

views.py

from urllib import request
from rest_framework import viewsets, status
from django.contrib.auth.models import User
from atest.serializers import UserSerializer
from rest_framework import permissions
from atest.permissions import IsOwnerOrReadOnly
from rest_framework.decorators import action
from rest_framework.response import Response


class UserViewSet(viewsets.ModelViewSet):
    """
    This viewset provides operations on Users table to the same user.
    """

    # permission_classes = [IsOwnerOrReadOnly,]
    queryset = User.objects.all()
    serializer_class = UserSerializer

serializers.py

from rest_framework import serializers
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password


class UserSerializer(serializers.HyperlinkedModelSerializer):
    password = serializers.CharField(max_length=128, style={'input_type': 'password'}, write_only=True)

    class Meta:
        model = User
        fields = ('url', 'id', 'username', 'email', 'first_name', 'last_name', 'password')

        def create(self, validated_data):
            username = validated_data['username']
            email = validated_data['email']
            first_name = validated_data['first_name']
            last_name = validated_data['last_name']
            password = make_password(validated_data['password'])

        def update(self, instance, validated_data):
            instance.email = validated_data.get('email', instance.email)
            instance.username = validated_data.get('username', instance.username)
            instance.first_name = validated_data.get('first_name', instance.first_name)
            instance.last_name = validated_data.get('last_name', instance.last_name)
            instance.password = make_password(validated_data.get('password', instance.password))
            instance.save()
            return instance

Please help me identifying the solution. What am I doing wrong that my password is not getting encrypted?

Ad

Answer

You're just assigning things to a local variable in create; in addition, you shouldn't be calling make_password in update with instance.password; that way you'll be re-hashing the hashed password in the instance, making it unusable.

There happens to be an example (slightly roundabout) for this in the DRF documentation that might help you along:

class CreateUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('email', 'username', 'password')
        extra_kwargs = {'password': {'write_only': True}}

    def create(self, validated_data):
        user = User(
            email=validated_data['email'],
            username=validated_data['username']
        )
        user.set_password(validated_data['password'])
        user.save()
        return user
Ad
source: stackoverflow.com
Ad