|
| 1 | +title: django.contrib.auth.hashers make_password Python Code Examples |
| 2 | +category: page |
| 3 | +slug: django-contrib-auth-hashers-make-password-examples |
| 4 | +sortorder: 50035 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.contrib.auth.hashers make_password |
| 7 | +meta: Python code examples for the Django function make_password from the django.contrib.auth.hashers module. |
| 8 | + |
| 9 | + |
| 10 | +[Django](/django.html)'s |
| 11 | +[make_password](https://docs.djangoproject.com/en/dev/topics/auth/passwords/#django.contrib.auth.hashers.make_password) |
| 12 | +([source code](https://github.com/django/django/blob/master/django/contrib/auth/hashers.py)) |
| 13 | +function converts a plain-text password into a hash that is appropriate |
| 14 | +for storing in a [persistent database](/databases.html). |
| 15 | + |
| 16 | +You definitely do not want to try to roll your own encryption and hashing |
| 17 | +functions for storing passwords when this function already exists. |
| 18 | + |
| 19 | + |
| 20 | +## Example 1 from gadget-board |
| 21 | +[gadget-board](https://github.com/mik4el/gadget-board) is a |
| 22 | +[Django](/django.html), |
| 23 | +[Django REST Framework (DRF)](/django-rest-framework-drf.html) and |
| 24 | +[Angular](/angular.html) web application that is open source under the |
| 25 | +[Apache2 license](https://github.com/mik4el/gadget-board/blob/master/LICENSE). |
| 26 | + |
| 27 | +[**gadget-board / web / authentication / views.py**](https://github.com/mik4el/gadget-board/blob/master/web/authentication/views.py) |
| 28 | + |
| 29 | +```python |
| 30 | +from rest_framework import permissions, viewsets, status |
| 31 | +from rest_framework.response import Response |
| 32 | +from rest_framework_jwt.settings import api_settings |
| 33 | +~~from django.contrib.auth.hashers import make_password |
| 34 | + |
| 35 | +from .models import Account |
| 36 | +from .permissions import IsAccountOwner |
| 37 | +from .serializers import AccountSerializer |
| 38 | + |
| 39 | + |
| 40 | +class AccountViewSet(viewsets.ModelViewSet): |
| 41 | + lookup_field = 'username' |
| 42 | + queryset = Account.objects.all() |
| 43 | + serializer_class = AccountSerializer |
| 44 | + |
| 45 | + def get_permissions(self): |
| 46 | + if self.request.method in permissions.SAFE_METHODS: |
| 47 | + # only logged in users can see accounts |
| 48 | + return (permissions.IsAuthenticated(),) |
| 49 | + |
| 50 | + if self.request.method == 'POST': |
| 51 | + return (permissions.AllowAny(),) |
| 52 | + |
| 53 | + return (permissions.IsAuthenticated(), IsAccountOwner(),) |
| 54 | + |
| 55 | + def create(self, request): |
| 56 | + serializer = self.serializer_class(data=request.data) |
| 57 | + |
| 58 | + if serializer.is_valid(): |
| 59 | + if 'password' not in serializer.validated_data: |
| 60 | + return Response({ |
| 61 | + 'error': 'Password required for creating account.' |
| 62 | + }, status=status.HTTP_400_BAD_REQUEST) |
| 63 | + |
| 64 | + account = Account.objects.\ |
| 65 | + create_account(**serializer.validated_data) |
| 66 | + |
| 67 | + # add JWT token to response |
| 68 | + jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER |
| 69 | + jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER |
| 70 | + |
| 71 | + payload = jwt_payload_handler(account) |
| 72 | + token = jwt_encode_handler(payload) |
| 73 | + |
| 74 | + serializer.validated_data['token'] = token |
| 75 | + |
| 76 | + return Response(serializer.validated_data, |
| 77 | + status=status.HTTP_201_CREATED) |
| 78 | + |
| 79 | + return Response({ |
| 80 | + 'error': 'Account could not be created with received data.' |
| 81 | + }, status=status.HTTP_400_BAD_REQUEST) |
| 82 | + |
| 83 | + def perform_create(self, serializer): |
| 84 | + # Hash password but passwords are not required |
| 85 | + if ('password' in self.request.data): |
| 86 | +~~ password = make_password(self.request.data['password']) |
| 87 | + serializer.save(password=password) |
| 88 | + else: |
| 89 | + serializer.save() |
| 90 | + |
| 91 | + def perform_update(self, serializer): |
| 92 | + # Hash password but passwords are not required |
| 93 | + if ('password' in self.request.data): |
| 94 | +~~ password = make_password(self.request.data['password']) |
| 95 | + serializer.save(password=password) |
| 96 | + else: |
| 97 | + serializer.save() |
| 98 | +``` |
0 commit comments