I am attempting to utilize Duo MFA's - Admin API. It requires Date and Authorization headers to have a signed canonical string which I am having trouble with.
My current code:
import base64, email, hmac, hashlib, urllib, requests
url = "admin api link"
ikey = "secret"
skey = "secret"
def sign(method, host, path, params, skey, ikey):
"""
Return HTTP Basic Authentication ("Authorization" and "Date") headers.
method, host, path: strings from request
params: dict of request parameters
skey: secret key
ikey: integration key
"""
# create canonical string
now = email.Utils.formatdate()
canon = [now, method.upper(), host.lower(), path]
args = []
for key in sorted(params.keys()):
val = params[key]
if isinstance(val, unicode):
val = val.encode("utf-8")
args.append(
'%s=%s' % (urllib.quote(key, '~'), urllib.quote(val, '~')))
canon.append('&'.join(args))
canon = '\n'.join(canon)
# sign canonical string
sig = hmac.new(skey, canon, hashlib.sha1)
auth = '%s:%s' % (ikey, sig.hexdigest())
# return headers
return {'Date': now, 'Authorization': 'Basic %s' % base64.b64encode(auth)}
headers = {
'Date': ?,
'Authorization': ?,
}
response = requests.request("GET", url, headers=headers)
print(response.text)
How can I set the headers to use the sign
functions return Date
and Authorization
?
I have tried sign(Date)
and sign(Authorization)
with no luck. I believe I am missing some fundamental/rudimentary python logic on using the returns from functions.