Skip to content

Commit 55da88e

Browse files
author
Emmanouil Konstantinidis
committed
Init README and library
1 parent af9ce80 commit 55da88e

File tree

7 files changed

+79
-3
lines changed

7 files changed

+79
-3
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
1-
# django-rest-framework-api-key
1+
# django-rest-framework-api-key [![travis][travis-image]][travis-url] [![pypi][pypi-image]][pypi-url]
22
Authenticate Web APIs made with Django REST Framework
3+
4+
5+
### Supports
6+
7+
- Python (2.7, 3.3, 3.4, 3.5)
8+
- Django (1.8, 1.9, 1.10)
9+
- Django Rest Framework (3+)
10+
11+
12+
### Installation
13+
14+
Install using pip:
15+
16+
pip install drfapikey
17+
18+
Add 'rest_framework_api_key' to your `INSTALLED_APPS` setting:
19+
20+
INSTALLED_APPS = (
21+
...
22+
'rest_framework_api_key',
23+
)
24+
25+
26+
[travis-image]: https://travis-ci.org/ekonstantinidis/django-rest-framework-api-key.svg?branch=master
27+
[travis-url]: https://travis-ci.org/ekonstantinidis/django-rest-framework-api-key
28+
29+
[pypi-image]: https://badge.fury.io/py/django-rest-framework-api-key.svg
30+
[pypi-url]: https://pypi.python.org/pypi/django-rest-framework-api-key/

rest_framework_api_key/admin.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from django.contrib import admin
2+
from django.contrib import messages
3+
from rest_framework_api_key.models import APIKey
4+
from rest_framework_api_key.helpers import generate_key
5+
6+
7+
class ApiKeyAdmin(admin.ModelAdmin):
8+
list_display = ('id', 'name', 'created', 'modified')
9+
10+
fieldsets = (
11+
('Required Information', {'fields': ('name',)}),
12+
('Additional Information', {'fields': ('key_message',)}),
13+
)
14+
readonly_fields = ('key_message',)
15+
16+
search_fields = ('id', 'name',)
17+
18+
def has_delete_permission(self, request, obj=None):
19+
return False
20+
21+
def key_message(self, obj):
22+
if obj.key:
23+
return "Hidden"
24+
return "The API Key will be generated once you click save."
25+
26+
def save_model(self, request, obj, form, change):
27+
if not obj.key:
28+
obj.key = generate_key()
29+
messages.add_message(request, messages.WARNING, ('The API Key for %s is %s. Please note it since you will not be able to see it again.' % (obj.name, obj.key)))
30+
obj.save()
31+
32+
admin.site.register(APIKey, ApiKeyAdmin)

rest_framework_api_key/helpers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import binascii
2+
import os
3+
4+
5+
def generate_key():
6+
return binascii.hexlify(os.urandom(20)).decode()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from rest_framework import permissions
2+
from rest_framework_api_key.models import APIKey
3+
4+
5+
class HasAPIAccess(permissions.BasePermission):
6+
message = 'Invalid or missing API Key.'
7+
8+
def has_permission(self, request, view):
9+
api_key = request.META.get('HTTP_API_KEY', '')
10+
return APIKey.objects.filter(key=api_key).exists()

runtests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
FLAKE8_ARGS = ['rest_framework_api_key', 'tests/', '--ignore=E501']
8-
PYTEST_ARGS = ['tests', '--tb=short', '-s', '-rw']
8+
PYTEST_ARGS = ['tests', '--cov=rest_framework_api_key', '--tb=short']
99

1010

1111
def exit_on_failure(command, message=None):
File renamed without changes.

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[pytest]
2-
DJANGO_SETTINGS_MODULE=tests.test_settings
2+
DJANGO_SETTINGS_MODULE=tests.settings
33

44
[tox]
55
envlist =

0 commit comments

Comments
 (0)