Browse Source

changed API config so it accepts API keys

Apostol Mihai 2 years ago
parent
commit
2152bd0ce0

+ 1 - 0
elo_api/api_src/admin.py

@@ -0,0 +1 @@
+from django.contrib import admin

+ 4 - 2
elo_api/api_src/views.py

@@ -2,6 +2,8 @@ from django.contrib.auth.models import User, Group
 from rest_framework import viewsets
 from rest_framework import permissions
 from .serializers import UserSerializer, GroupSerializer
+from rest_framework.views import APIView
+from rest_framework_api_key.permissions import HasAPIKey
 
 
 class UserViewSet(viewsets.ModelViewSet):
@@ -10,7 +12,7 @@ class UserViewSet(viewsets.ModelViewSet):
     """
     queryset = User.objects.all().order_by('-date_joined')
     serializer_class = UserSerializer
-    permission_classes = [permissions.IsAuthenticated]
+    permission_classes = [HasAPIKey]
 
 
 class GroupViewSet(viewsets.ModelViewSet):
@@ -19,4 +21,4 @@ class GroupViewSet(viewsets.ModelViewSet):
     """
     queryset = Group.objects.all()
     serializer_class = GroupSerializer
-    permission_classes = [permissions.IsAuthenticated]
+    permission_classes = [HasAPIKey]

BIN
elo_api/db.sqlite3


+ 4 - 1
elo_api/elo_api/settings.py

@@ -38,6 +38,7 @@ INSTALLED_APPS = [
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'rest_framework',
+    "rest_framework_api_key",
     'drf_yasg',
 ]
 
@@ -45,10 +46,12 @@ REST_FRAMEWORK = {
     # Use Django's standard `django.contrib.auth` permissions,
     # or allow read-only access for unauthenticated users.
     'DEFAULT_PERMISSION_CLASSES': [
-        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
+        "rest_framework_api_key.permissions.HasAPIKey"
+        #'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
     ],
     'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
 }
+API_KEY_CUSTOM_HEADER = "HTTP_X_API_KEY"
 
 MIDDLEWARE = [
     'django.middleware.security.SecurityMiddleware',

+ 1 - 0
elo_api/elo_api/urls.py

@@ -43,6 +43,7 @@ router.register(r'groups', views.GroupViewSet)
 
 
 urlpatterns = [
+    path('admin/', admin.site.urls),
     re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
     re_path(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
     re_path(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),