diff --git a/README.md b/README.md index 6e17fe9..0995ef4 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,15 @@ response = requests.get( ``` +### Settings + +```python +API_KEY_CONFIG = { + 'REQUEST_META_KEY': 'HTTP_API_KEY' +} +``` + + ### Tests pyvenv env diff --git a/rest_framework_api_key/permissions.py b/rest_framework_api_key/permissions.py index 75a432f..5c7b0da 100644 --- a/rest_framework_api_key/permissions.py +++ b/rest_framework_api_key/permissions.py @@ -1,10 +1,11 @@ from rest_framework import permissions from rest_framework_api_key.models import APIKey +from .settings import api_settings class HasAPIAccess(permissions.BasePermission): message = 'Invalid or missing API Key.' def has_permission(self, request, view): - api_key = request.META.get('HTTP_API_KEY', '') + api_key = request.META.get(api_settings.REQUEST_META_KEY, '') return APIKey.objects.filter(key=api_key).exists() diff --git a/rest_framework_api_key/settings.py b/rest_framework_api_key/settings.py new file mode 100644 index 0000000..b360c6c --- /dev/null +++ b/rest_framework_api_key/settings.py @@ -0,0 +1,11 @@ +from django.conf import settings +from rest_framework.settings import APISettings + + +USER_SETTINGS = getattr(settings, 'API_KEY_CONFIG', None) + +DEFAULTS = { + 'REQUEST_META_KEY': 'HTTP_API_KEY', +} + +api_settings = APISettings(USER_SETTINGS, DEFAULTS) diff --git a/tests/settings.py b/tests/settings.py index 43710a0..712c4e2 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -63,3 +63,7 @@ }, }, ] + +API_KEY_CONFIG = { + 'REQUEST_META_KEY': 'HTTP_API_KEY' +} diff --git a/tests/test_admin.py b/tests/test_admin.py index 8c7ab76..1bace58 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -3,6 +3,7 @@ from django.test import TestCase from rest_framework_api_key.models import APIKey from rest_framework_api_key.helpers import generate_key +from .settings import API_KEY_CONFIG class LoggedInAdminTestCase(TestCase): @@ -26,7 +27,7 @@ class APIAuthenticatedTestCase(TestCase): def setUp(self): self.app_key = APIKey.objects.create(name=self.APP_NAME, key=generate_key()) - self.header = {'HTTP_API_KEY': self.app_key.key} + self.header = {API_KEY_CONFIG['REQUEST_META_KEY']: self.app_key.key} class AdminTestCase(LoggedInAdminTestCase, APIAuthenticatedTestCase):