Skip to main content

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:

  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.

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

django_hawk-1.2.5.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_hawk-1.2.5-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file django_hawk-1.2.5.tar.gz.

File metadata

  • Download URL: django_hawk-1.2.5.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/24.5.0

File hashes

Hashes for django_hawk-1.2.5.tar.gz
Algorithm Hash digest
SHA256 d7bd479590a6fb5ea5fb2c4b13bbff14708c9a10a1aa4e7b05bd9f27cfed3e64
MD5 7e3eb84995147b32dcda4bb58b755a4e
BLAKE2b-256 8b1c5e20dbcaee3d3d3ecafbc17c264b8d2a2dd6d0f8df1f9999344649f0d079

See more details on using hashes here.

File details

Details for the file django_hawk-1.2.5-py3-none-any.whl.

File metadata

  • Download URL: django_hawk-1.2.5-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/24.5.0

File hashes

Hashes for django_hawk-1.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a491d318db150d0f24a41d3730f8f1c792255dd99912565de8d83491c5156b93
MD5 6f168c1fdf11d7a237bd1bb68f944263
BLAKE2b-256 bf1e6cf6f2bfd5f526535234ed6879fc38225259664eeb4a7a766288c55a54f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page