Skip to content

Api Key Config Default Settings #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ response = requests.get(
```


### Settings

```python
API_KEY_CONFIG = {
'REQUEST_META_KEY': 'HTTP_API_KEY'
}
```


### Tests

pyvenv env
Expand Down
3 changes: 2 additions & 1 deletion rest_framework_api_key/permissions.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 11 additions & 0 deletions rest_framework_api_key/settings.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@
},
},
]

API_KEY_CONFIG = {
'REQUEST_META_KEY': 'HTTP_API_KEY'
}
3 changes: 2 additions & 1 deletion tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down