Skip to main content

HTTP Signature support for Django REST framework

Project description


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.


Installing the package via the repository:

pip install djangorestframework-httpsignature

The current implementation depends on http_signature by David Lehn, who has updated the original code to match the revised spec. This dependency is reflected in the REQUIREMENTS.txt file, and pip will pull the code from David’s repository.


It has been reported that some newer versions of pip might download the requiered package http_signature from PyPI instead of the repository listed in REQUIREMENTS.txt. If this is your case, a workaround is:

  1. git clone
  2. cd django-rest-framework-httpsignature
  3. remove the whole line in REQUIREMENTS.txt that references py-http-signature
  4. pip install the entire line (including the “-e” at the beginning)
  5. pip install -r REQUIREMENTS.txt

Those steps make explicity what should be automatic. Thanks Alberto Gragera for both spotting the problem and providing a solution.

Running the tests

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

python test


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 INSTALLED_APPS.

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

    # my_api/
    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:
                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/
    # ...
    # The above will force HTTP signature for all requests.
    # ...


  • 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 with much less pain, using the modules requests and http_signature:

import requests
from http_signature.requests_auth import HTTPSignatureAuth

API_KEY_ID = 'su-key'
SECRET = 'my secret string'

signature_headers = ['request-line', 'accept', 'date', 'host']
headers = {
    'Host': 'localhost:8000',
    'Accept': 'application/json',
    'X-Api-Key': API_KEY_ID,

# We omit the "Date" header, so http_signature adds it.
auth = HTTPSignatureAuth(key_id=API_KEY_ID, secret=SECRET,
req = requests.get('http://localhost:8000/resource/',
                   auth=auth, headers=headers)
print req.content

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
djangorestframework-httpsignature-0.1.5.tar.gz (8.3 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page