Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Port shared folder
  • Loading branch information
rakshith91 committed Jul 16, 2019
commit 6305b891064f4e1997761ee65d18a7016c7ae4de
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from azure.core.exceptions import ResourceExistsError, DecodeError

from ._shared.models import StorageErrorCode
from ._shared.encryption import _decrypt_queue_message, _encrypt_queue_message
from ._shared.encryption import decrypt_queue_message, encrypt_queue_message
from .models import QueueProperties


Expand Down Expand Up @@ -58,7 +58,7 @@ def __call__(self, content):
if content:
content = self.encode(content)
if self.key_encryption_key is not None:
content = _encrypt_queue_message(content, self.key_encryption_key)
content = encrypt_queue_message(content, self.key_encryption_key)
return content

def configure(self, require_encryption, key_encryption_key, resolver):
Expand All @@ -85,7 +85,7 @@ def __call__(self, response, obj, headers):
continue
content = message.message_text
if (self.key_encryption_key is not None) or (self.resolver is not None):
content = _decrypt_queue_message(
content = decrypt_queue_message(
content, response,
self.require_encryption,
self.key_encryption_key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,54 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import base64
import hashlib
import hmac

try:
from urllib.parse import quote, unquote
except ImportError:
from urllib2 import quote, unquote # type: ignore

import six


def url_quote(url):
return quote(url)


def url_unquote(url):
return unquote(url)


def encode_base64(data):
if isinstance(data, six.text_type):
data = data.encode('utf-8')
encoded = base64.b64encode(data)
return encoded.decode('utf-8')


def decode_base64_to_bytes(data):
if isinstance(data, six.text_type):
data = data.encode('utf-8')
return base64.b64decode(data)


def decode_base64_to_text(data):
decoded_bytes = decode_base64_to_bytes(data)
return decoded_bytes.decode('utf-8')


def sign_string(key, string_to_sign, key_is_base64=True):
if key_is_base64:
key = decode_base64_to_bytes(key)
else:
if isinstance(key, six.text_type):
key = key.encode('utf-8')
if isinstance(string_to_sign, six.text_type):
string_to_sign = string_to_sign.encode('utf-8')
signed_hmac_sha256 = hmac.HMAC(key, string_to_sign, hashlib.sha256)
digest = signed_hmac_sha256.digest()
encoded_digest = encode_base64(digest)
return encoded_digest
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
# license information.
# --------------------------------------------------------------------------

import base64
import hashlib
import hmac
import logging
import sys
try:
Expand All @@ -18,43 +15,12 @@
from azure.core.exceptions import ClientAuthenticationError
from azure.core.pipeline.policies import SansIOHTTPPolicy

if sys.version_info < (3,):
_unicode_type = unicode # pylint: disable=undefined-variable
else:
_unicode_type = str
logger = logging.getLogger(__name__)


def _encode_base64(data):
if isinstance(data, _unicode_type):
data = data.encode('utf-8')
encoded = base64.b64encode(data)
return encoded.decode('utf-8')
from . import sign_string


def _decode_base64_to_bytes(data):
if isinstance(data, _unicode_type):
data = data.encode('utf-8')
return base64.b64decode(data)


def _decode_base64_to_text(data):
decoded_bytes = _decode_base64_to_bytes(data)
return decoded_bytes.decode('utf-8')
logger = logging.getLogger(__name__)


def _sign_string(key, string_to_sign, key_is_base64=True):
if key_is_base64:
key = _decode_base64_to_bytes(key)
else:
if isinstance(key, _unicode_type):
key = key.encode('utf-8')
if isinstance(string_to_sign, _unicode_type):
string_to_sign = string_to_sign.encode('utf-8')
signed_hmac_sha256 = hmac.HMAC(key, string_to_sign, hashlib.sha256)
digest = signed_hmac_sha256.digest()
encoded_digest = _encode_base64(digest)
return encoded_digest

# wraps a given exception with the desired exception type
def _wrap_exception(ex, desired_type):
Expand Down Expand Up @@ -125,7 +91,7 @@ def _get_canonicalized_resource_query(self, request):

def _add_authorization_header(self, request, string_to_sign):
try:
signature = _sign_string(self.account_key, string_to_sign)
signature = sign_string(self.account_key, string_to_sign)
auth_string = 'SharedKey ' + self.account_name + ':' + signature
request.http_request.headers['Authorization'] = auth_string
except Exception as ex:
Expand Down
Loading