Skip to main content

dj-api-auth is a Django application, providing an AWS-alike API auth solution.

When I was seeking a simple solution rather than intricate OAuth, I was inspired by this article Designing a Secure REST (Web) API without OAuth. Thanks to the author and the comments.

Features

  1. API key, SEC key related forms

  2. Each API key can be associated with a set of API

  3. API can be associated with user, your legacy code with request.user underneath can work smoothly with dj-api-auth

  4. Add auth by simply put a decorator on your view.

  5. Discover API with auth enabled automatically, these auth-required APIs will display in assignable list when creating API keys

  6. A Django command to scan and update API information to database.

How it works

  1. Generate a pair of API key and SEC key, assign some APIs to it.

  2. Client put API key and current UNIX time as apikey and timestemp in requestURL

  3. Client also generate a signature by calculate a SHA256 value on the whole URL(without signature) by its known SEC key.

  4. Server side will verify
    • is timestamp from client in reasonable drift compare to server time.

    • is apikey from client exists

    • is the API client trying to access allowed for the apikey

    • compare the signature with the one calculated on server side by same algorithm

  5. if any verification failed, return 403 error with brief message

Add to your project

  1. Add djapiauth to INSTALLED_APPS in sttings.py

  2. There are two optional settings

  • API_AUTH_ALLOWED_TIME_DRIFT
    • optional, set the allowed time drift between server time and the timestamp parameter in coming URL.

    • format : integer, unit: second

    • default : 300, (5 minutes)

  • API_AUTH_ADMIN_USER_FILTER
    • optional, when creating API keys, you can assign the API key to an user, this filter is used to filter the users showing in the API key creating form.

    • format: dictionary, the filter parameter will be passed to get_user_model().objects.filter(). e.g. {'name_startswith': 'admin'}

    • default: {}, means all users will show in the API key creating form.

Generate/Manage API and SEC key

If you have admin enabled for your project, you can find these features in admin site. Otherwise, you can import forms from djapiauth.forms or write your own form based on models in djapiauth.models

Add auth for function-based views

  • For legacy views, we provide utility function url_with_auth in djapiauth

# add auth for a browser-oriented view
url_with_auth(r'^hello/$', 'djapp.views.index'),
#...
  • For API views, simply add @api_auth for the view after from djapiauth import api_auth

@api_auth
def api_whoami(request):
        return JsonResponse({"user": "feifan", "boss": "lidan zhou"})

Add auth for class-based views

For class-baesd views, simply add djapiauth.utility.AuthMixin as a base class to get auth protection.

from djapiauth import AuthMixin
class ProtectedView(AuthMixin,View):
    def get(self, request):
        return HttpResponse('hello, auth')


class UnprotectedView(AuthMixin,View):
    api_auth = False
    def get(self, request):
        return HttpResponse('hello, no auth needed')

and add URL mapping in urls.py

...
url(r'^classbased1/$', apis.ProtectedView.as_view())
...

Scan API

There’s a Django command reloadentrypoints to help you to collect and save all auth-required APIs to database.

API Key and associate API set

since version 0.8, I start using a tree-alike structure to speed up matching incoming URLs.

when you upgrade from previous versions, you should run following commands to expand database fields and build matching tree for each API key.

python manage.py migrate
python manage.py repairapikeys

Error messages

  • parameter missing, any of apikey, timestamp or signature missing in URL

  • time drift xxx, check your local time and server time. You can implement an API to return server time

  • entry point not allowed for the API key, check the assigned API for this API key in admin site or anywhere else you manage API keys

  • signature error, obviously, signature mismatch

DEMO

  • Source code under example/djapp folder.

  • Test code is under example/test/, we have python and javascript test code ready.

Server application provides 2 APIs

  • /hello/ : reused the code of index view, add an auth layer on it

  • /goodbye/ : a view you must access it by the signature stuff

DIY:

  • Start the djapp

  • there’s already one pair of API+SEC keys: 483a570a, d7228d70cd7f456d9bfdc35ed8fee375

  • modify variable URL in test.py, or URL in test.js

  • Generate API key and SEC key from localhost:8000/admin/xxx, modify variable API_KEY and SEC_KEY in test.py or test.js

  • Run python test.py or node test.js

  • login admin site with admin user: admin/123, remove all APIs associated with 483a570a, try to run the test code again, you should see 403 errors __main__.APIException: (403, '{"error": "entry point not allowed for the API key"}')

  • modify the API key to an invalid one

  • modify the SEC key to an invalid one

  • modify your local time to one hour ago

Thanks

Thanks for the Javascript test code from Neil Chen (neil.chen.nj@gmail.com)

Metadata

Release files for dj-api-auth 0.7

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

Source distribution (sdist)

Source distribution for dj-api-auth 0.7
File Size Uploaded Provenance Actions
dj-api-auth-0.7.tar.gz 39.1 kB Without provenance Details for dj-api-auth-0.7.tar.gz

Release files / dj-api-auth-0.7.tar.gz

Download URL dj-api-auth-0.7.tar.gz
Size 39.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6ae66fcb8ab8a27bf3121c06e9b482fbdf2e7e955d41265c73fb04460ef47e37
BLAKE2b-256 checksum
How to use checksums
cbde5970573e656767fbea7e0b5077e02c5aa646aa4d5a9cb36efa17993ac85b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for dj-api-auth 0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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