Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

HTTP SHA1 HMAC authentication backend for Django

Project description

Django Fost Authentication

An authentication back-end for Django implementing request signing using strong cryptography. It is based on the request signing mechanism implemented by Amazon for s3.

Both requests and URLs can be signed. Request signing are used where the user agent itself is able to perform the signing (i.e. it knows the API key and secret). URL signing is used where a authentication needs to be delegated to a user agent not capable (or not trusted) to sign the request.

To use in a Django project

Add the git repository to your pip install file and then use:

pip install -r setup.pip

To your you need to add the middleware. It doesn’t matter where in the middleware list it is added:


Unless you’ve already added a custom authentication backend your probably doesn’t already have the authentication backends set on it. The following will enable the normal Django authentication (forms/session based) and the Fost authentication:


In order to be able to authenticate requests the authentication backend needs to know how to map API keys to secrets and to users.

Finding a secret from an API key is done by giving a function to the FOST_AUTHN_GET_SECRET setting. If this is not configured then the default implementation will use a SHA1 hash of the user’s password field. This ensures that the secret will change even if the user changes their password to the same as it was due to the change in the salt stored in the field.

The following is an example of what can be put in the

def FOST_AUTHN_GET_SECRET(request, key):
    from myapp.models import api_keys
    return api_keys.objects.get(key=key).secret

Changes to existing Django classes

After installing the authentication middleware the Django HttpRequest object is augmented with a new member, SIGNED, which is a dict holding the signed request header members. If the request has not been properly signed this dict will be empty.

Optional settings


The amount of time to sleep when a FOST Authorization header is incorrect. Defaults to 0.5 seconds.


The maximum allowed difference between the time when the request was signed and the time on the server. Defaults to 300 seconds.

Signing requests

In order to authenticate against the back end requests must be properly signed. `fost_auth.signature’ includes two functions for doing this that can be used.

fost_hmac_request_signature(secret, method, path, timestamp, headers = {}, body = '')

The headers are in the form of a dict giving the header name and values. The function returns both the signature and the document that was signed.

It is the responsibility of the caller to correctly place the header values into the request object that is to be used, including the Authorization header.

Running tests

For best results create a virtualenv, then initialise it with test.pip.:

mkvirtualenv django-fost-authn
pip install -r test.pip

Run the tests using:


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
django-fost-authn-0.3.8.tar.gz (6.1 kB) Copy SHA256 hash SHA256 Source None Nov 26, 2013

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