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
File details
Details for the file django_hawk-1.2.2.tar.gz
.
File metadata
- Download URL: django_hawk-1.2.2.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.6 Darwin/23.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ee44b4578e80b75266f3b6693ce2a7e3a02eccfe6666797bc055c354a0b211f |
|
MD5 | e4668521312a37e055fe214517ab81c5 |
|
BLAKE2b-256 | 16fed4c89e08890f79385b5686037c0f5ba5c08baa9663c476b38d0871dc0700 |
File details
Details for the file django_hawk-1.2.2-py3-none-any.whl
.
File metadata
- Download URL: django_hawk-1.2.2-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.6 Darwin/23.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91c4e8176cad0978c613ee03c827ff9a90b275ee5e8194e2f404f169574bf014 |
|
MD5 | 71d7612935f0d7808f993244bd96e3a6 |
|
BLAKE2b-256 | 977188b74fac1b9ba759b3ed72cb980aaaf56df70defb8c4b50339716e64e1c1 |