A library that provides a simple token authorization for Django REST framework.
Project description
DRF Simple Access Key
A library that provides a simple token authorization for Django REST framework.
Installation
With a correctly configured pipenv toolchain:
pipenv install drf-simple-access-key
You may also use classic pip to install the package:
pip install drf-simple-access-key
Auto-formatter setup
We use isort (https://github.com/pycqa/isort) and black (https://github.com/psf/black) for local auto-formatting and for linting in the CI pipeline. The pre-commit framework (https://pre-commit.com) provides GIT hooks for these tools, so they are automatically applied before every commit.
Steps to activate:
- Install the pre-commit framework:
pip install pre-commit(for alternative installation options see https://pre-commit.com/#install) - Activate the framework (from the root directory of the repository):
pre-commit install
Hint: You can also run the formatters manually at any time with the following command: pre-commit run --all-files
Getting started
Configuration options
HTTP_AUTHORIZATION_HEADER: str
Default: 'x-authorization'
Name of the HTTP request header used for authorization.
HTTP_AUTHORIZATION_SCHEME: str
Default: 'bearer'
Name of the HTTP authorization scheme.
AUTHORIZATION_KEYS: list[str]
Default: []
List of valid authorization keys. Note that any request is allowed if this configuration option is empty!
Example configuration for Django settings
SIMPLE_ACCESS_KEY_SETTINGS = {
'HTTP_AUTHORIZATION_HEADER': 'x-authorization',
'HTTP_AUTHORIZATION_SCHEME': 'bearer',
'AUTHORIZATION_KEYS': [
'example-token-1234',
],
}
REST_FRAMEWORK = {
# ...
'DEFAULT_PERMISSION_CLASSES': [
'drf_simple_access_key.SimpleAccessKey',
# ...
],
# ...
}
How to use
All API endpoints that use the permission class are protected by the simple access key authorization.
GET http://my.tld/api/v1/resource/
x-authorization: bearer example-token-1234
When to use
This library provides the simplest possible solution to protect a REST API from unauthorized access. It allows anyone in possession of a valid key to access the endpoints without the possibility of user authentication. This type of authorization is well suited for microservices that users cannot access directly.
In summary this means:
✔️ Use this authorization only if access to the REST API is possible from known and trusted sources only (e.g. an API gateway).
✔️ Use this authorization only if no user authentication is required within the REST API.
❌ Never use this authorization if the REST API is publicly accessible over the Internet.
❌ Never use this authorization if the consumers of the REST API are real users, and not exclusively systems such as an API gateway.
Supported versions
| Django REST framework 3.14 | Django REST framework 3.15 | |
|---|---|---|
| Python 3.9 | ✓ | ✓ |
| Python 3.10 | ✓ | ✓ |
| Python 3.11 | ✓ | ✓ |
| Python 3.12 | ✓ | ✓ |
| Python 3.13 | ✓ | ✓ |
Tests
See folder tests/. Basically, all endpoints are covered with multiple unit tests.
Follow below instructions to run the tests.
You may exchange the installed Django and DRF versions according to your requirements.
:warning: Depending on your local environment settings you might need to explicitly call python3 instead of python.
# install dependencies
python -m pip install --upgrade pip
pip install -r requirements.txt
# setup environment
pip install -e .
# run tests
cd tests && python manage.py test
List of developers
- Andreas Stocker AStocker@anexia-it.com
- Harald Nezbeda HNezbeda@anexia-it.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drf_simple_access_key-1.1.0.tar.gz.
File metadata
- Download URL: drf_simple_access_key-1.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
161c8c61a231c47a16439dc661908fc56aa77a43b8b102b49bd3b5717c1ec085
|
|
| MD5 |
07de1a9840850cc0a23b2932e07e9f5b
|
|
| BLAKE2b-256 |
80e94a7277644f1ab794cdc47bb3ffbabbda9b2c3109083acecabce89d6761ef
|
File details
Details for the file drf_simple_access_key-1.1.0-py3-none-any.whl.
File metadata
- Download URL: drf_simple_access_key-1.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a44e7dad0f18849c7985e15e06c7ede3178841f561356262beb634f7df678c9
|
|
| MD5 |
1d624d550cd0dbc09703608c58df90d9
|
|
| BLAKE2b-256 |
9d0778e991ba6da1814c19e9f157821cabb92affd6bfa14c05cf7f150502cf51
|