Skip to main content

Django Ninja API Key Authentication

Project description

Ninja API Key

PyPI

codecov

API Key authentication for Django Ninja.

This is a fork from django-ninja-apikey.

Key Features:

  • Easy integration into your projects
  • Well integrated with the Admin interface
  • Secure API keys due to hashing
  • Works with the standard user model

Installation

pip install ninja-api-key

How to use

  1. Add ninja_apikey to your installed apps in your Django project:
# settings.py

INSTALLED_APPS = [
    # ...
    "ninja_apikey",
]
  1. Apply migrations
python manage.py migrate
  1. Secure

    a. the whole API

    # api.py
    
    from ninja import NinjaAPI
    from ninja_apikey.security import APIKeyAuth
    
    #  ...
    
    api = NinjaAPI(auth=APIKeyAuth())
    
    # ...
    
    @api.get("/secure_endpoint")
    def secure_endpoint(request):
        return f"Hello, {request.user}!"
    

    b. an specific endpoint

    # api.py
    
    from ninja import NinjaAPI
    from ninja_apikey.security import APIKeyAuth
    
    #  ...
    
    auth = APIKeyAuth()
    api = NinjaAPI()
    
    # ...
    
    @api.get("/secure_endpoint", auth=auth)
    def secure_endpoint(request):
        return f"Hello, {request.user}!"
    
  2. ninja-api-key uses settings.PASSWORD_HASHERS to hash the API keys. Django's default PBKDF2PasswordHasher may be slow depending on the number of iterations. You can change the settings.PASSWORD_HASHERS to use a faster (but less secure) one. ninja-api-key provides a SHA256PasswordHasher.

     # settings.py
    
     PASSWORD_HASHERS = [
         "ninja_apikey.hashers.SHA256PasswordHasher",
         # others
     ]
    

    ⚠️ Keep in mind that this will affect Django authentication as a whole, not just ninja-api-key.

Changelog

See CHANGELOG.md for a detailed history of changes and releases.

Contributing

Contributions are welcome; feel free to open an Issue or Pull Request.

git clone https://github.com/lucasrcezimbra/ninja-api-key
cd ninja-api-key
python -m venv .venv
source .venv/bin/activate
pip install .[test]
pre-commit install
make test

Download files

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

Source Distribution

ninja_api_key-2.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

ninja_api_key-2.0.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file ninja_api_key-2.0.0.tar.gz.

File metadata

  • Download URL: ninja_api_key-2.0.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ninja_api_key-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3dbc2390d13671ff336377068e83504fa1819a2fe6acbc58ce8afe00a290527c
MD5 a46870e48d6e2d7439efd8bf759e1704
BLAKE2b-256 b5752b453e2055beef8bd1e3b9a66513b4487325736db4f5d85b03e474cdc9a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for ninja_api_key-2.0.0.tar.gz:

Publisher: publish.yml on lucasrcezimbra/ninja-api-key

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ninja_api_key-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: ninja_api_key-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ninja_api_key-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6da7d4900953747d2b8f7cca7de1d005eb3996cd2d9a2032d5eaaddd5eed267b
MD5 cf903cc3b0321bc68a6fa60956f7f12e
BLAKE2b-256 65268a9982dc656e2c905bda8438299ccfd75c7f1957c78758f7f602f0a7e23a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ninja_api_key-2.0.0-py3-none-any.whl:

Publisher: publish.yml on lucasrcezimbra/ninja-api-key

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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