Skip to content

Instantly share code, notes, and snippets.

@bebehei
Created February 8, 2018 11:53
Show Gist options
  • Save bebehei/5e3357e5a1bf46ec381379ef8f525c7f to your computer and use it in GitHub Desktop.
Save bebehei/5e3357e5a1bf46ec381379ef8f525c7f to your computer and use it in GitHub Desktop.
Python function to generate wsse headers
# See for WSSE key generation this link:
# https://oroinc.com/orocrm/doc/2.0/cookbook/how-to-use-wsse-authentication
# Returns a dictionary of headers
def genheaders(user_name, user_key):
random = str(uuid.uuid4()).encode('ascii')
nonce = hashlib.md5(random).digest()[0:16]
curdate = datetime.datetime.now().replace(microsecond=0)
hash_digest = hashlib.sha1()
hash_digest.update(nonce)
hash_digest.update(curdate.isoformat().encode('ascii'))
hash_digest.update(user_key)
x_wsse = ', '.join([ 'UsernameToken Username="{user}"',
'PasswordDigest="{digest}"',
'Nonce="{nonce}"',
'Created="{created}"'])
x_wsse = x_wsse.format(
user=user_name,
digest=base64.b64encode(hash_digest.digest()).decode('utf-8'),
nonce=base64.b64encode(nonce).decode('utf-8'),
created=curdate.isoformat(),
)
return {
'Authorization': 'WSSE profile="UsernameToken"',
'X-WSSE': x_wsse,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment