Skip to content

Commit

Permalink
Reconfigure API authentication classes
Browse files Browse the repository at this point in the history
Configure the ApiKeyAuthentication via DEFAULT_AUTHENTICATION_CLASSES
setting and only set permission classes per API views.  This allows
having SessionAuthentication and BasicAuthentication in the development
environment (in DEBUG mode), which helps the development.
  • Loading branch information
suutari-ai committed Feb 12, 2018
1 parent 531869b commit 0c30425
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
4 changes: 1 addition & 3 deletions parkings/api/enforcement/operator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from rest_framework import permissions, serializers, viewsets

from ...authentication import ApiKeyAuthentication
from ...models import Operator


Expand All @@ -16,7 +15,6 @@ class Meta:


class OperatorViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [permissions.IsAdminUser]
queryset = Operator.objects.order_by('name')
serializer_class = OperatorSerializer
authentication_classes = [ApiKeyAuthentication]
permission_classes = [permissions.IsAdminUser]
4 changes: 1 addition & 3 deletions parkings/api/enforcement/valid_parking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from django.utils.translation import gettext_lazy as _
from rest_framework import permissions, serializers, viewsets

from ...authentication import ApiKeyAuthentication
from ...models import Parking


Expand Down Expand Up @@ -90,8 +89,7 @@ def get_time_old_parkings_visible(default=datetime.timedelta(minutes=15)):


class ValidParkingViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [permissions.IsAdminUser]
queryset = Parking.objects.order_by('-time_end')
serializer_class = ValidParkingSerializer
filter_class = ValidParkingFilter
authentication_classes = [ApiKeyAuthentication]
permission_classes = [permissions.IsAdminUser]
4 changes: 1 addition & 3 deletions parkings/api/operator/parking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.utils.translation import ugettext_lazy as _
from rest_framework import mixins, permissions, serializers, viewsets

from parkings.authentication import ApiKeyAuthentication
from parkings.models import Operator, Parking

from ..common import ParkingException
Expand Down Expand Up @@ -87,10 +86,9 @@ def has_object_permission(self, request, view, obj):

class OperatorAPIParkingViewSet(mixins.CreateModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin,
viewsets.GenericViewSet):
permission_classes = [OperatorAPIParkingPermission]
queryset = Parking.objects.order_by('time_start')
serializer_class = OperatorAPIParkingSerializer
authentication_classes = (ApiKeyAuthentication,)
permission_classes = (OperatorAPIParkingPermission,)

def perform_create(self, serializer):
serializer.save(operator=self.request.user.operator)
Expand Down
3 changes: 2 additions & 1 deletion parkings/api/public/parking_area.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rest_framework import viewsets
from rest_framework import permissions, viewsets
from rest_framework_gis.pagination import GeoJsonPagination
from rest_framework_gis.serializers import (
GeoFeatureModelSerializer, GeometrySerializerMethodField)
Expand All @@ -24,6 +24,7 @@ class Meta:


class PublicAPIParkingAreaViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [permissions.AllowAny]
queryset = ParkingArea.objects.order_by('origin_id')
serializer_class = ParkingAreaSerializer
pagination_class = GeoJsonPagination
Expand Down
3 changes: 2 additions & 1 deletion parkings/api/public/parking_area_statistics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db.models import Case, Count, Q, When
from django.utils import timezone
from rest_framework import serializers, viewsets
from rest_framework import permissions, serializers, viewsets

from parkings.models import ParkingArea
from parkings.pagination import PublicAPIPagination
Expand Down Expand Up @@ -33,6 +33,7 @@ class Meta:


class PublicAPIParkingAreaStatisticsViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [permissions.AllowAny]
queryset = ParkingArea.objects.all()
serializer_class = ParkingAreaStatisticsSerializer
pagination_class = PublicAPIPagination
Expand Down
14 changes: 12 additions & 2 deletions parkings/api/public/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
from django.conf.urls import include, url
from rest_framework.routers import DefaultRouter
from rest_framework import permissions
from rest_framework.routers import APIRootView, DefaultRouter

from .parking_area import PublicAPIParkingAreaViewSet
from .parking_area_statistics import PublicAPIParkingAreaStatisticsViewSet

router = DefaultRouter()

class PublicApiRootView(APIRootView):
permission_classes = [permissions.AllowAny]


class Router(DefaultRouter):
APIRootView = PublicApiRootView


router = Router()
router.register(r'parking_area', PublicAPIParkingAreaViewSet, base_name='parkingarea')
router.register(r'parking_area_statistics', PublicAPIParkingAreaStatisticsViewSet, base_name='parkingareastatistics')

Expand Down
14 changes: 13 additions & 1 deletion parkkihubi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Django core settings #
########################
DEBUG = env.bool('DEBUG', default=False)
TIER = env.str('TIER', default='dev')
SECRET_KEY = env.str('SECRET_KEY', default=('' if not DEBUG else 'xxx'))
ALLOWED_HOSTS = ['*']

Expand Down Expand Up @@ -80,7 +81,7 @@
'parkings',
]

if DEBUG:
if DEBUG and TIER == 'dev':
# shell_plus and other goodies
INSTALLED_APPS.append("django_extensions")

Expand Down Expand Up @@ -157,6 +158,17 @@
# Django REST Framework #
#########################
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
# Make nothing accessible to non-admins by default. Viewsets
# should specify permission_classes to override permissions.
'rest_framework.permissions.IsAdminUser',
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'parkings.authentication.ApiKeyAuthentication',
] + ([ # Following two are only for DEBUG mode in dev environment:
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
] if (DEBUG and TIER == 'dev') else []),
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning',
'ALLOWED_VERSIONS': ('v1',),
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
Expand Down

0 comments on commit 0c30425

Please sign in to comment.