Skip to content

Instantly share code, notes, and snippets.

@whalesalad
Created August 22, 2014 01:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whalesalad/2142f0075c6896f4547c to your computer and use it in GitHub Desktop.
Save whalesalad/2142f0075c6896f4547c to your computer and use it in GitHub Desktop.
Holy crap.
# Copyright (c) Microsoft Corporation. All rights reserved.
"""Authorization helper functions.
"""
from hashlib import sha256
import hmac
import pydocumentdb.http_constants as http_constants
def GetAuthorizationHeader(document_client,
verb,
path,
resource_id,
resource_type,
headers):
"""Gets the authorization header.
:Parameters:
- `document_client`: document_client.DocumentClient
- `verb`: str
- `path`: str
- `resource_id`: str
- `resource_type`: str
- `headers`: dict
:Returns:
dict, the authorization headers.
"""
if document_client.master_key:
return __GetAuthorizationTokenUsingMasterKey(verb,
resource_id,
resource_type,
headers,
document_client.master_key)
elif document_client.resource_tokens:
return __GetAuthorizationTokenUsingResourceTokens(
document_client.resource_tokens, path, resource_id)
def __GetAuthorizationTokenUsingMasterKey(verb,
resource_id,
resource_type,
headers,
master_key):
"""Gets the authorization token using `master_key.
:Parameters:
- `verb`: str
- `resource_id`: str
- `resource_type`: str
- `headers`: dict
- `master_key`: st
:Returns:
dict
"""
key = master_key.decode('base64')
text = ((verb or '') + '\n' +
(resource_type or '') + '\n' +
(resource_id or '') + '\n' +
(headers[http_constants.HttpHeaders.XDate]
if http_constants.HttpHeaders.XDate in headers else '') + '\n' +
(headers[http_constants.HttpHeaders.HttpDate]
if http_constants.HttpHeaders.HttpDate in headers else '') + '\n')
body = text.lower().decode('utf8')
hm = hmac.new(key, body, sha256)
signature = hm.digest().encode('base64')
master_token = 'master'
token_version = '1.0'
return 'type=' + master_token +'&ver=' + token_version + '&sig=' + signature[:-1]
def __GetAuthorizationTokenUsingResourceTokens(resource_tokens,
path,
resource_id):
"""Get the authorization token using `resource_tokens`.
:Parameters:
- `resource_tokens`: dict
- `path`: str
- `resource_id`: str
:Returns:
dict
"""
if resource_id in resource_tokens and resource_tokens[resource_id]:
return resource_tokens[resource_id]
else:
path_parts = path.split('/')
resource_types = ['dbs', 'colls', 'docs', 'sprocs', 'udfs', 'triggers',
'users', 'permissions', 'attachments', 'media',
'conflicts']
for i in range(len(path_parts) - 1, -1, -1):
if not path_parts[i] in resource_types and path_parts[i] in resource_tokens:
return resource_tokens[path_parts[i]]
return None
@shipunyc
Copy link

Hi whalesalad, I was the author of this SDK. It’s built in short period of time so not good enough yet. Any feedback on how I can improvement it?

@peterbe
Copy link

peterbe commented Aug 22, 2014

@shipunyc Do you come from Java or C#?

@peterbe
Copy link

peterbe commented Aug 22, 2014

@shipunyc It's probably that the code isn't "pythonic". Basically, it's written as if the language is some other language. It often revolves around using the good parts of Python. For example, instead of

path_parts = path.split('/')
for i in range(path_parts):
    do_something(path_parts[i])

a much smoother way of doing it would be:

for part in path.split('/'):
    do_something(part)

@smilliken
Copy link

@shipunyc, please consider following the official style guide: http://legacy.python.org/dev/peps/pep-0008/.

You can automate style checking with these tools:
https://pypi.python.org/pypi/pep8
https://pypi.python.org/pypi/pylint

@shipunyc
Copy link

@peterbe, @smilliken, thanks for the feedback. Good to know there are style checking tools.
The code was translated from C# SDK and I was more of a C++ developer.

@uschen
Copy link

uschen commented Aug 22, 2014

this gist is iconic... using some editors like sublime text or ... and let it do the linting and formatting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment