Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Authentication and Permissions with Django REST...

Authentication and Permissions with Django REST framework

An overview of the authentication and permissions management with Django REST framework.

Avatar for Ordoquy Xavier - Linovia

Ordoquy Xavier - Linovia

October 16, 2016
Tweet

More Decks by Ordoquy Xavier - Linovia

Other Decks in Programming

Transcript

  1. SITE vs API { "count": 2, "next": null, "previous": null,

    "results": [ { "email": "[email protected]", "groups": [], "url": "http://127.0.0.1:8000/users/1/", "username": "admin" }, { "email": "[email protected]", "groups": [ ], "url": "http://127.0.0.1:8000/users/2/", "username": "tom" } ] }
  2. 3RD PARTIES • Django OAuth2 Consumer • JSON Web Token

    Authentication • Hawk HTTP Authentication • HTTP Signature Authentication • django-rest-framework-social-oauth2 • Digest
 Authentication • Djoser • django-rest-auth • django-rest-knox
  3. 401 Unauthorized The request was not successfully authenticated, and the

    highest priority authentication class does use "WWW-Authenticate" headers
  4. 403 Forbidden The request was not successfully authenticated, and the

    highest priority authentication class does not use "WWW-Authenticate" headers. > SessionAuthentication <
  5. • AllowAny • IsAuthenticated • IsAdminUser • IsAuthenticatedOrReadOnly • DjangoModelPermissions

    • DjangoModelPermissionsOrAnonReadOnly • DjangoObjectPermissions INCLUS
  6. PERSONALISATION GET OPTIONS HEAD POST…………………….. PUT………………….. PATCH………………. DELETE……………….. <app label>.add_<model

    name> <app label>.change_<model name> <app label>.change_<model name> <app label>.delete_<model name> perms_map
  7. class CustomObjectPermissions(DjangoObjectPermissions): perms_map = { 'GET': ['%(app_label)s.view_%(model_name)s'], 'OPTIONS': ['%(app_label)s.view_%(model_name)s'], 'HEAD':

    ['%(app_label)s.view_%(model_name)s'], 'POST': ['%(app_label)s.add_%(model_name)s'], 'PUT': ['%(app_label)s.change_%(model_name)s'], 'PATCH': ['%(app_label)s.change_%(model_name)s'], 'DELETE': ['%(app_label)s.delete_%(model_name)s'], } class EventViewSet(viewsets.ModelViewSet): queryset = Event.objects.all() serializer_class = EventSerializer filter_backends = [filters.DjangoObjectPermissionsFilter] permission_classes = [CustomObjectPermissions]
  8. SURCHAGE view.get_object def get_object(self): queryset = self.filter_queryset(self.get_queryset()) filter_kwargs = ###

    obj = get_object_or_404(queryset, **filter_kwargs) self.check_object_permissions(self.request, obj) return obj
  9. from django.contrib.auth.models import User from rest_framework import authentication from rest_framework

    import exceptions class ExampleAuthentication(authentication.BaseAuthentication): def authenticate(self, request): username = request.META.get('X_USERNAME') if not username: return None try: user = User.objects.get(username=username) except User.DoesNotExist: raise exceptions.AuthenticationFailed('No such user') return (user, None)
  10. from rest_framework import permissions class BlacklistPermission(permissions.BasePermission): def has_permission(self, request, view):

    ip_addr = request.META['REMOTE_ADDR'] blacklisted = Blacklist.objects.filter( ip_addr=ip_addr).exists() return not blacklisted
  11. As a direct result of a successful Mozilla grant application,

    I will be leaving my current role at DabApps, and attempting to secure a sustainable business model for REST framework development. I need your help in order to make this work. -Tom Christie