Django HMAC authentication using shared secret
Project description
django_hmac_authentication
Django hmac authentication with shared secret
- Django model with HMAC shared encrypted secret
- Authentication class
HMACAuthentication
- Reject requests earlier than configured timeout
- Supports
HMAC-SHA512
,HMAC-SHA384
,HMAC-SHA256
- HMAC secret can be created with management command or obtained with a configured url
1. Install
pip install django_hmac_authentication
2. Configuration
2.1 settings.py
-
Add
MAX_HMACS_PER_USER
Default: 10 -
Add
HMAC_AUTH_REQUEST_TIMEOUT
in seconds. Requests earlier than this are rejected Default:5
-
Add
django_hmac_authentication
to installed apps along withrest_framework
. -
Add hmac authentication class to
REST_FRAMEWORK
insettings.py
. -
Example
MAX_HMACS_PER_USER = 10
HMAC_AUTH_REQUEST_TIMEOUT = 4
INSTALLED_APPS = [
...,
'rest_framework',
'django_hmac_authentication',
...
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'django_hmac_authentication.authentication.HMACAuthentication',
],
}
2.2 urls.py
Add url to obtain HMAC key and secret
...
from django_hmac_authentication.views import CreateApiHMACKey
urlpatterns = [
...,
# django_hmac_authentication
path('obtain-hmac-api-key/',
CreateApiHMACKey.as_view(),
name='api_hmac_key'),
...
]
2.3 Run migrations
python manage.py migrate django_hmac_authentication
3. Usage
3.1 Obtain HMAC key and secret
3.1.1 Using management command
Use management command to create a HMAC API key with secret for a user
python manage.py create_hmac_for_user bob
{"api_key": "f4c3801c-a277-4fcb-92bb-44cb814026f6", "api_secret": "vEOQRdvaK4jyeLKGNP9oqpYTUvt/GZWbGG6iNmnDh8c=", "message": "These credentials will be lost forever if not stored now"}
3.1.2 Using curl
# Use the url from configuration
curl -X POST -H "Content-Type: application/json" -d '{"username":"bob", "password":"bobspassword"}' http://127.0.0.1:8000/obtain-hmac-api-key/
{"api_key":"7ebc25d7-d237-4f90-b4ad-98f0c228fc1e","api_secret":"EDQppq0B3rIxvaA7PyPUHPF6kiXTnnbvnMiZDzYFSRA=","message":"These credentials will be lost forever if not stored now"}
4. Sign requests client-side
4.1 Javascript client
See example_django_project/javascript_topman_collection
folder
A postman collection with environment is provided which can be imported to Postman. A prerequest script for generating the signature is provided (same as included in postman collection).
4.2 Python client
See example_django_project/example_python_client.py
5. Signature
Signature is calculated on hash( request body json ) + utc 8601
Fields
- Hash of request body. Hash function is one of supported methods in Authorization header
- UTC time now in ISO 8601 format. Example
2023-05-07T14:15:37.862560+00:00
6. Authorization header
- method: One of
HMAC-SHA512
,HMAC-SHA384
,HMAC-SHA256
- api_key: Key used to identify the hmac secret used to generate signature
- signature: base64 signature
- request_utc: time in ISO 8601 set in signed string
Syntax
: method api_key;signature;request_utc
Example
'HMAC-SHA512 aa733037-e4c0-4f75-a864-df6c1966481b;6k3XaUREI6dDw6thyQWASJjzjsx1M7GOZAglguv0OElpRue1+gb7CK2n3JpzJGz9VcREw2y3rIW5zoZYEUY+0w==;2023-05-07T14:15:37.862560+00:00'
7. License
Apache2 License
8. Github
https://github.com/harisankar-krishna-swamy/django_hmac_authentication
9. See also
https://www.okta.com/au/identity-101/hmac/
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
Hashes for django_hmac_authentication-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0450ffbfe19280a21c99a5e566946c785d28a50443e102e1f5c85af0efdde392 |
|
MD5 | 12e2ad8d9ae07f0c0e7edf8432d598c9 |
|
BLAKE2b-256 | 5d333a84f8625aab97d47ef4562cd43a3727f1534fb537695b493cf9ce397c90 |
Hashes for django_hmac_authentication-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | baa472515ef2f6ef676d5c4a410adf5666df6b251eb94e15780b7a29cf9c0720 |
|
MD5 | e00d8acf2df725ed7e818c19c4ab537b |
|
BLAKE2b-256 | 36b7520762c54b70b7cc210310067408da8d2b263d3dc89e84cbe564ea9f3746 |