https://github.com/lucasrcezimbra/ninja-api-key
API Key authentication for Django Ninja
https://github.com/lucasrcezimbra/ninja-api-key
api api-key api-key-authentication apikey apikey-authentication django django-ninja ninja python
Last synced: 3 months ago
JSON representation
API Key authentication for Django Ninja
- Host: GitHub
- URL: https://github.com/lucasrcezimbra/ninja-api-key
- Owner: lucasrcezimbra
- License: mit
- Created: 2024-05-08T13:16:59.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-07T20:55:39.000Z (3 months ago)
- Last Synced: 2025-04-12T07:12:33.638Z (3 months ago)
- Topics: api, api-key, api-key-authentication, apikey, apikey-authentication, django, django-ninja, ninja, python
- Language: Python
- Homepage: https://pypi.org/project/ninja-api-key/
- Size: 75.2 KB
- Stars: 11
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# Ninja API Key
[](https://pypi.python.org/pypi/ninja-api-key)
[](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.pyINSTALLED_APPS = [
# ...
"ninja_apikey",
]
```2. Apply migrations
```shell
python manage.py migrate
```3. Secure
a. the whole API
```Python
# api.pyfrom 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.pyfrom 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.pyPASSWORD_HASHERS = [
"ninja_apikey.hashers.SHA256PasswordHasher",
# others
]
```⚠️ Keep in mind that this will affect Django authentication as a whole,
not just ninja-api-key.## 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
```