API Authentication
Generate HMAC Signature
var apiKey = environment.api_key,
apiSecret = environment.api_secret,
date = (new Date()).toUTCString(),
sigContent = 'date: ' + date,
sig = CryptoJS.HmacSHA512(sigContent, apiSecret).toString(CryptoJS.enc.Base64),
authHeader = 'Signature keyId="' + apiKey + '",algorithm="hmac-sha512",signature="' + encodeURIComponent(sig) + '"';
pm.environment.set("authHeader", authHeader); // Authorization header
pm.environment.set("date", date); // Date headerimport requests
# Module defined below
from auth import SignatureAuth
# Prodivded by the assigned Client Solutions Engineer (CSE)
XCOVER_API_KEY = ""
XCOVER_SECRET = ""
def _apply_auth(request):
request.headers.update(
SignatureAuth(XCOVER_API_KEY,
XCOVER_SECRET).sign()
)
return request
session = requests.Session()
request = requests.Request('POST', url, json=payload, headers=headers)
response = session.send(_apply_auth(request.prepare()))
print(response.status_code)
# auth.py
"""
Authentication related module.
"""
from __future__ import unicode_literals
from base64 import b64encode
from hashlib import sha512
import hmac
from urllib.parse import quote
from datetime import datetime
from time import mktime
from wsgiref.handlers import format_date_time
def http_date():
now = datetime.utcnow()
return format_date_time(mktime(now.timetuple()))
class SignatureAuth:
"""Class for basic authentication support."""
def __init__(self, key, secret):
self._key = key
self._secret = secret
self._headers = None
def create_signature(self, date):
raw = 'date: {date}'.format(date=date)
hashed = hmac.new(self._secret.encode('utf-8'), raw.encode('utf-8'), sha512).digest()
return quote(b64encode(hashed), safe='')
def build_signature(self, signature, key):
template = ('Signature keyId="%(key)s",algorithm="hmac-sha512",'
'signature="%(signature)s"')
return template % {
'key': key,
'signature': signature
}
def sign(self):
date = http_date()
auth = self.build_signature(signature=self.create_signature(date), key=self._key)
return {
'Date': date,
'Authorization': auth,
'X-Api-Key': self._key,
}
Testing API Keys
Last updated