Authenticate Django Views with HAWK
Project description
Django Hawk
This package can be used to help create HAWK Authenticated views.
Installation
pip install django-hawk
Add the following to your Django Settings:
DJANGO_HAWK = {
"HAWK_INCOMING_ACCESS_KEY": "xxx",
"HAWK_INCOMING_SECRET_KEY": "xxx",
}
Example Usage
To use the HAWK Authentication, we need to do 2 things:
- Make sure the
HawkResponseMiddleware
runs - Check the authentication
Add the HawkResponseMiddleware
to the MIDDLEWARE
setting in your project like so:
MIDDLEWARE = [
...
"django_hawk.middleware.HawkResponseMiddleware",
...
]
To check the authentication you can call django_hawk.utils.authenticate_request
, if an exception isn't raised then you know that the request is authenticated, see below for examples.
from django.http import HttpResponse
from django_hawk.utils import DjangoHawkAuthenticationFailed, authenticate_request
def simple_view(request):
# Try to authenticate with HAWK
try:
authenticate_request(request=request)
except DjangoHawkAuthenticationFailed as e:
return HttpResponse(status=401)
# Continue with normal View code...
return HttpResponse("This is a simple view")
Testing
Tests belong in the /django_hawk/tests/
directory. You can run the tests by installing the requirements like so:
make setup
Now you can run the tests using the following command:
poetry run python manage.py test
Tox tests
We use tox to test compatibility across different Django versions.
To run these tests with tox, just run the following:
make tox
Pushing to PyPI
Running make build-package
will build the package into the dist/
directory
Running make push-pypi-test
will push the built package to Test PyPI
Running make push-pypi
will push the built package to PyPI
Setting up poetry for pushing to PyPI
First you will need to add the test pypy repository to your poetry config:
poetry config repositories.test-pypi https://test.pypi.org/legacy/
Then go to https://test.pypi.org/manage/account/token/ and generate a token.
Then add it to your poetry config:
poetry config pypi-token.test-pypi XXXXXXXX
Then you also need to go to https://pypi.org/manage/account/token/ to generate a token for the real PyPI.
Then add it to your poetry config:
poetry config pypi-token.pypi XXXXXXXX
Now the make commands should work as expected.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_hawk-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf764f0b6bdb0c4a94ad8476533ef09c53782752dec85ed538a357c9b7c902bf |
|
MD5 | 5255a91c6d6b4be907e6c230f47a2840 |
|
BLAKE2b-256 | 05b2414e5a391ea0b249d41edbc134e3faad85d9f1f8a9e13b2b8dbe039d706c |