HMAC 512 auth #1434
Replies: 3 comments
-
Hi @anryangelov I'm afraid swagger does not support that out of the box as of python implementation - if you can provide example python code of your inputs, validation and finding user/principal - I can give you example on how to integrate that with django ninja |
Beta Was this translation helpful? Give feedback.
-
Hi @vitalik thanks for the answer. Below is the exact description (click to the arrow) which I provided to description parameter when initialize the api instance. I hope this help for clarifying. So I do not how to do this interactive in swagger so that i can input api key and api secret as vars. Surely with some javascript but I am just wondering which is most straightforward and elegant way for doing this. Also is there a better place where I can explain how auth happened in openapi. For now I use redoc because just looks better. By the way can I use both swagger and redoc at same time with one api instance as well at least for development? . Also I forgot to mention that initially inherit from ninja security api class but currently i move auth logic in django middleware if that meters. AuthenticationThis API uses HMAC authentication. Authentication instructionsRequired Headers:
Message Format:
For example, a
Bash Example#!/bin/bash
API_KEY="your_api_key"
API_SECRET="your_api_secret"
TIMESTAMP=$(date +%s)000 # or `date +%s%3N`
PATH="{{ external_url_prefix }}ping"
BODY=""
MESSAGE="${METHOD}${PATH}${BODY}${TIMESTAMP}"
SIGNATURE=$(echo -n "$MESSAGE" | openssl dgst -sha512 -hmac "$API_SECRET" | sed 's/^.* //')
curl -X GET "{{ host90k }}{{ external_url_prefix }}ping" \
-H "X-API-key: $API_KEY" \
-H "X-API-Timestamp: $TIMESTAMP" \
-H "X-API-Signature: $SIGNATURE" \
-d "$BODY" Python Exampleimport time, hmac, hashlib, requests
API_KEY = 'your_api_key'
API_SECRET = 'your_api_secret'
METHOD = 'GET'
PATH = '{{ external_url_prefix }}ping'
BODY = ''
timestamp = str(int(time.time_ns() / 1000_000))
message = METHOD + PATH + BODY + timestamp
signature = hmac.new(API_SECRET.encode('utf-8'), message.encode('utf-8'), hashlib.sha512).hexdigest()
response = requests.get(
'{{ host90k }}' + PATH,
headers={
'X-API-key': API_KEY,
'X-API-Timestamp': timestamp,
'X-API-Signature': signature
},
data=BODY
)
print(response.status_code, response.json()) Always use HTTPS to ensure secure transmission. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
How can I integrate with swagger hence openapi if my api has HMAC 512 auth. So clients have api key and secret key. So when clients want to make request they must provide api key, timestamp and signature as http headers. Also i would like provide ability to the clients to enter into swagger api key and secret key as vars so that swagger automatically calculate signature on request and add needed headers to authorize request.
Beta Was this translation helpful? Give feedback.
All reactions