Skip to main content

django-rest-framework-httpsignature

https://travis-ci.org/etoccalino/django-rest-framework-httpsignature.png?branch=master

Overview

Provides HTTP Signature support for Django REST framework. The HTTP Signature package provides a way to achieve origin authentication and message integrity for HTTP messages. Similar to Amazon’s HTTP Signature scheme, used by many of its services. The HTTP Signature specification is currently an IETF draft.

Installation

Installing the package via the repository:

pip install djangorestframework-httpsignature

Older version of pip don’t support the Wheel format (which is how httpsig is distributed). The problem manifests when installing the requirements, pip will complain that it cannot find a httpsig. In such cases, pip needs to be upgraded:

pip install --upgrade pip

Another possible problem: while installing via python setup.py install you may encounter:

No local packages or download links found for httpsig
error: Could not find suitable distribution for Requirement.parse('httpsig')

If that is the case, use pip install httpsig to install the httpsig package and retry python setup.py install.

Running the tests

To run the tests for the packages, use the following command on the repository root directory:

python manage.py test

Usage

To authenticate HTTP requests via HTTP signature, you need to:

  1. Install this package in your Django project, as instructed in Installation.

  2. Add rest_framework_httpsignature to your settings.py INSTALLED_APPS.

  3. In your app code, extend the SignatureAuthentication class, as follows:

    # my_api/auth.py
    
    from rest_framework_httpsignature.authentication import SignatureAuthentication
    
    class MyAPISignatureAuthentication(SignatureAuthentication):
        # The HTTP header used to pass the consumer key ID.
        # Defaults to 'X-Api-Key'.
        API_KEY_HEADER = 'X-Api-Key'
    
        # A method to fetch (User instance, user_secret_string) from the
        # consumer key ID, or None in case it is not found.
        def fetch_user_data(self, api_key):
            # ...
            # example implementation:
            try:
                user = User.objects.get(api_key=api_key)
                return (user, user.secret)
            except User.DoesNotExist:
                return None
  4. Configure Django REST framework to use you authentication class; e.g.:

    # my_project/settings.py
    
    # ...
    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
           'my_api.auth.MyAPISignatureAuthentication',
        ),
        'DEFAULT_PERMISSION_CLASSES': (
            'rest_framework.permissions.IsAuthenticated',
        )
    }
    # The above will force HTTP signature for all requests.
    # ...

Roadmap

  • Currently, the library only support HMAC SHA256 for signing.

  • The REQUIREMENTS.txt file is fairly strict. It is very possible that previous versions of Django and Django REST framework are supported.

  • Since HTTP Signature uses a HTTP header for the request date and time, the authentication class could deal with request expiry.

Example usage & session w/cURL

Assuming the setup detailed in Usage, a project running on localhost:8000 could be probed with cURL as follows:

~$ SSS=Base64(Hmac(SECRET, "Date: Mon, 17 Feb 2014 06:11:05 GMT", SHA256))
~$ curl -v -H 'Date: "Mon, 17 Feb 2014 06:11:05 GMT"' -H 'Authorization: Signature keyId="my-key",algorithm="hmac-sha256",headers="date",signature="SSS"'

And for a much less painful example, check out the httpsig package documentation to use requests and httpsig.

Metadata

Release files for djangorestframework-httpsignature 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djangorestframework-httpsignature 1.0.0
File Size Uploaded
djangorestframework-httpsignature-1.0.0.tar.gz 8.0 kB Details

Release files / djangorestframework-httpsignature-1.0.0.tar.gz

Download URL djangorestframework-httpsignature-1.0.0.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5006ab9cda6891cf030b27c92badf22c460a3eb1a8d11b13eac8f95da9c26b06
BLAKE2b-256 checksum
How to use checksums
3d99965607b9309ee15225dd6d4635efc3ff80bf5269853a1f90d4fdbce4f8bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page