Skip to content

Commit 521498e

Browse files
Merge pull request #1 from ekonstantinidis/setup-project
Setup Project
2 parents deab572 + 4268887 commit 521498e

22 files changed

+488
-1
lines changed

.travis.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
language: python
2+
3+
sudo: false
4+
5+
python:
6+
- "3.5"
7+
8+
env:
9+
- TOX_ENV=py27-django18
10+
- TOX_ENV=py34-django18
11+
- TOX_ENV=py35-django18
12+
13+
- TOX_ENV=py27-django19
14+
- TOX_ENV=py34-django19
15+
- TOX_ENV=py35-django19
16+
17+
- TOX_ENV=py27-django110
18+
- TOX_ENV=py34-django110
19+
- TOX_ENV=py35-django110
20+
21+
- TOX_ENV=py27-djangomaster
22+
- TOX_ENV=py34-djangomaster
23+
- TOX_ENV=py35-djangomaster
24+
25+
cache:
26+
- pip
27+
28+
install:
29+
- pip install tox
30+
- pip install codecov
31+
32+
script:
33+
- tox -e $TOX_ENV
34+
35+
after_success:
36+
- codecov

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright (c) 2016 Emmanouil Konstantinidis
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

MANIFEST.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include README.md
2+
include LICENSE
3+
include MANIFEST.in
4+
5+
global-exclude __pycache__
6+
global-exclude *.py[co]
7+
8+
prune docs

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,71 @@
1-
# django-rest-framework-api-key
1+
# django-rest-framework-api-key [![travis][travis-image]][travis-url] [![codecov][codecov-image]][codecov-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+
Finally set the django-rest-framework permissions under your django settings:
26+
27+
REST_FRAMEWORK = {
28+
'DEFAULT_PERMISSION_CLASSES': (
29+
'rest_framework_api_key.permissions.HasAPIAccess',
30+
)
31+
}
32+
33+
34+
### Example Request
35+
36+
```python
37+
response = requests.get(
38+
url="http://0.0.0.0:8000/api/login",
39+
headers={
40+
"Api-Key": "fd8b4a98c8f53035aeab410258430e2d86079c93",
41+
},
42+
)
43+
```
44+
45+
46+
### Tests
47+
48+
pyvenv env
49+
source env/bin/activate
50+
pip install -r requirements/requirements-testing.txt
51+
python runtests.py
52+
53+
54+
### Contributing
55+
56+
1. Fork it!
57+
2. Create your feature branch: `git checkout -b my-new-feature`
58+
3. Commit your changes: `git commit -am 'Add some feature'`
59+
4. Push to the branch: `git push origin my-new-feature`
60+
5. Submit a pull request
61+
6. Make sure tests are passing
62+
63+
64+
[travis-image]: https://travis-ci.org/ekonstantinidis/django-rest-framework-api-key.svg?branch=master
65+
[travis-url]: https://travis-ci.org/ekonstantinidis/django-rest-framework-api-key
66+
67+
[codecov-image]: https://codecov.io/github/ekonstantinidis/django-rest-framework-api-key/coverage.svg?branch=master
68+
[codecov-url]:https://codecov.io/github/ekonstantinidis/django-rest-framework-api-key?branch=master
69+
70+
[pypi-image]: https://badge.fury.io/py/drfapikey.svg
71+
[pypi-url]: https://pypi.python.org/pypi/drfapikey/

codecov.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
coverage:
2+
precision: 2
3+
round: down
4+
range: "70...100"
5+
6+
status:
7+
project: false
8+
patch: false
9+
changes: false
10+
11+
comment: off

requirements/requirements-base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
djangorestframework>=3.3
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-r requirements-base.txt
2+
3+
# PyTest for running the tests.
4+
pytest==3.0.0
5+
pytest-cov==2.3.1
6+
pytest-django==3.0.0
7+
8+
flake8==3.0.4

rest_framework_api_key/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "0.0.1"

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()

0 commit comments

Comments
 (0)