Metadata-Version: 2.4
Name: ninja-api-key
Version: 1.0.3
Summary: Django Ninja API Key Authentication
Keywords: django,rest,ninja,auth,apikey
Author-email: Lucas Rangel Cezimbra <lucas@cezimbra.tec.br>, Maximilian Wassink <wassink.maximilian@protonmail.com>
Requires-Python: >=3.6.2
Description-Content-Type: text/markdown
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 3.0
Classifier: Framework :: Django :: 3.1
Classifier: Framework :: Django :: 3.2
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Topic :: Internet
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Topic :: Software Development :: Libraries :: Python Modules
License-File: LICENSE
Requires-Dist: django
Requires-Dist: django-ninja
Requires-Dist: pre-commit ; extra == "dev"
Requires-Dist: django[argon2, bcrypt] ; extra == "test"
Requires-Dist: pytest ; extra == "test"
Requires-Dist: pytest-cov ; extra == "test"
Requires-Dist: pytest-django ; extra == "test"
Project-URL: Source, https://github.com/lucasrcezimbra/ninja-api-key
Provides-Extra: dev
Provides-Extra: test

# Ninja API Key


[![PyPI](https://img.shields.io/pypi/v/ninja-api-key.svg)](https://pypi.python.org/pypi/ninja-api-key)

[![codecov](https://codecov.io/gh/lucasrcezimbra/ninja-api-key/graph/badge.svg)](https://codecov.io/gh/lucasrcezimbra/ninja-api-key)


API Key authentication for [Django Ninja](https://django-ninja.dev/).

This is a fork from [django-ninja-apikey](https://github.com/mawassk/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

```bash
pip install ninja-api-key
```


## How to use
1. Add `ninja_apikey` to your installed apps in your Django project:
```Python
# settings.py

INSTALLED_APPS = [
    # ...
    "ninja_apikey",
]
```

2. Apply migrations
```shell
python manage.py migrate
```

3. Secure

    a. the whole API
    ```Python
    # 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
    ```Python
    # 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}!"
    ```


4. 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`.

   ```python
    # 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](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
```

