Skip to content

Instantly share code, notes, and snippets.

@aribornstein
Last active November 8, 2017 19:07
Show Gist options
  • Save aribornstein/0d49160b0b88324dfb482262da64f296 to your computer and use it in GitHub Desktop.
Save aribornstein/0d49160b0b88324dfb482262da64f296 to your computer and use it in GitHub Desktop.
Python Eventhub Rest Authentication Example
import time
import urllib
import hmac
import hashlib
import base64
def get_auth_token(sb_name, eh_name, sas_name, sas_value):
"""
Returns an auth token dictionary for making calls to eventhub
REST API.
"""
uri = urllib.parse.quote_plus("https://{}.servicebus.windows.net/{}" \
.format(sb_name, eh_name))
sas = sas_value.encode('utf-8')
expiry = str(int(time.time() + 10000))
string_to_sign = (uri + '\n' + expiry).encode('utf-8')
signed_hmac_sha256 = hmac.HMAC(sas, string_to_sign, hashlib.sha256)
signature = urllib.parse.quote(base64.b64encode(signed_hmac_sha256.digest()))
return {"sb_name": sb_name,
"eh_name": eh_name,
"token":'SharedAccessSignature sr={}&sig={}&se={}&skn={}' \
.format(uri, signature, expiry, sas_name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment