# 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:
```python
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:
1. Make sure the `HawkResponseMiddleware` runs
2. 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.
```python
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](https://pypi.org/project/tox/) to test compatibility across different Django versions.
To run these tests with tox, just run the following:
```
make tox
```
## Pushing to PyPI
- [PyPI Package](https://pypi.org/project/django-hawk/)
- [Test PyPI Package](https://test.pypi.org/project/django-hawk/)
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.
Raw data
{
"_id": null,
"home_page": "https://github.com/uktrade/django-hawk",
"name": "django-hawk",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "django",
"author": "Cameron Lamb",
"author_email": "live.services@digital.trade.gov.uk",
"download_url": "https://files.pythonhosted.org/packages/16/fe/d4c89e08890f79385b5686037c0f5ba5c08baa9663c476b38d0871dc0700/django_hawk-1.2.2.tar.gz",
"platform": null,
"description": "# Django Hawk\n\nThis package can be used to help create HAWK Authenticated views.\n\n## Installation\n\n```\npip install django-hawk\n```\n\nAdd the following to your Django Settings:\n\n```python\nDJANGO_HAWK = {\n \"HAWK_INCOMING_ACCESS_KEY\": \"xxx\",\n \"HAWK_INCOMING_SECRET_KEY\": \"xxx\",\n}\n```\n\n## Example Usage\n\nTo use the HAWK Authentication, we need to do 2 things:\n\n1. Make sure the `HawkResponseMiddleware` runs\n2. Check the authentication\n\nAdd the `HawkResponseMiddleware` to the `MIDDLEWARE` setting in your project like so:\n\n```\nMIDDLEWARE = [\n ...\n \"django_hawk.middleware.HawkResponseMiddleware\",\n ...\n]\n```\n\nTo 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.\n\n```python\nfrom django.http import HttpResponse\n\nfrom django_hawk.utils import DjangoHawkAuthenticationFailed, authenticate_request\n\ndef simple_view(request):\n # Try to authenticate with HAWK\n try:\n authenticate_request(request=request)\n except DjangoHawkAuthenticationFailed as e:\n return HttpResponse(status=401)\n\n # Continue with normal View code...\n return HttpResponse(\"This is a simple view\")\n```\n\n## Testing\n\nTests belong in the `/django_hawk/tests/` directory. You can run the tests by installing the requirements like so:\n\n```\nmake setup\n```\n\nNow you can run the tests using the following command:\n\n```\npoetry run python manage.py test\n```\n\n### Tox tests\n\nWe use [tox](https://pypi.org/project/tox/) to test compatibility across different Django versions.\n\nTo run these tests with tox, just run the following:\n\n```\nmake tox\n```\n\n## Pushing to PyPI\n\n- [PyPI Package](https://pypi.org/project/django-hawk/)\n- [Test PyPI Package](https://test.pypi.org/project/django-hawk/)\n\nRunning `make build-package` will build the package into the `dist/` directory.\n\nRunning `make push-pypi-test` will push the built package to Test PyPI.\n\nRunning `make push-pypi` will push the built package to PyPI.\n\n### Setting up poetry for pushing to PyPI\n\nFirst you will need to add the test pypy repository to your poetry config:\n\n```\npoetry config repositories.test-pypi https://test.pypi.org/legacy/\n```\n\nThen go to https://test.pypi.org/manage/account/token/ and generate a token.\n\nThen add it to your poetry config:\n\n```\npoetry config pypi-token.test-pypi XXXXXXXX\n```\n\nThen you also need to go to https://pypi.org/manage/account/token/ to generate a token for the real PyPI.\n\nThen add it to your poetry config:\n\n```\npoetry config pypi-token.pypi XXXXXXXX\n```\n\nNow the make commands should work as expected.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Authenticate Django Views with HAWK",
"version": "1.2.2",
"project_urls": {
"Homepage": "https://github.com/uktrade/django-hawk"
},
"split_keywords": [
"django"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "977188b74fac1b9ba759b3ed72cb980aaaf56df70defb8c4b50339716e64e1c1",
"md5": "71d7612935f0d7808f993244bd96e3a6",
"sha256": "91c4e8176cad0978c613ee03c827ff9a90b275ee5e8194e2f404f169574bf014"
},
"downloads": -1,
"filename": "django_hawk-1.2.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "71d7612935f0d7808f993244bd96e3a6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 9480,
"upload_time": "2023-12-05T16:52:40",
"upload_time_iso_8601": "2023-12-05T16:52:40.156667Z",
"url": "https://files.pythonhosted.org/packages/97/71/88b74fac1b9ba759b3ed72cb980aaaf56df70defb8c4b50339716e64e1c1/django_hawk-1.2.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "16fed4c89e08890f79385b5686037c0f5ba5c08baa9663c476b38d0871dc0700",
"md5": "e4668521312a37e055fe214517ab81c5",
"sha256": "0ee44b4578e80b75266f3b6693ce2a7e3a02eccfe6666797bc055c354a0b211f"
},
"downloads": -1,
"filename": "django_hawk-1.2.2.tar.gz",
"has_sig": false,
"md5_digest": "e4668521312a37e055fe214517ab81c5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 7047,
"upload_time": "2023-12-05T16:52:41",
"upload_time_iso_8601": "2023-12-05T16:52:41.909764Z",
"url": "https://files.pythonhosted.org/packages/16/fe/d4c89e08890f79385b5686037c0f5ba5c08baa9663c476b38d0871dc0700/django_hawk-1.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-05 16:52:41",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "uktrade",
"github_project": "django-hawk",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "django-hawk"
}