コード例 #1
0
ファイル: __init__.py プロジェクト: 02strich/pyazure
 def create_signature(self, container, path, resource="b", permissions="r", start="", expiry="", identifier=""):
     canonicalized_name = "/" + self._account + "/" + container + "/" + path
     data_to_sign = [permissions, start, expiry, canonicalized_name, identifier]
     string_to_sign = NEW_LINE.join(data_to_sign)
     return base64.encodestring(
         hmac.new(self._key, unicode(string_to_sign).encode("utf-8"), hashlib.sha256).digest()
     ).strip()
コード例 #2
0
ファイル: __init__.py プロジェクト: 02strich/pyazure
    def _sign_request_impl(self, request, for_tables=False, use_path_style_uris=None):
        (scheme, host, path, query, fragment) = urlsplit(request.get_full_url())
        if use_path_style_uris:
            path = path[path.index("/") :]

        if use_path_style_uris is None:
            use_path_style_uris = re.match("^[\d.:]+$", host) is not None

        # RFC 1123
        request.add_header("x-ms-date", get_azure_time())
        canonicalized_headers = NEW_LINE.join(
            (
                "%s:%s" % (k.lower(), request.get_header(k).strip())
                for k in sorted(request.headers.keys(), lambda x, y: cmp(x.lower(), y.lower()))
                if k.lower().startswith("x-ms-")
            )
        )

        # verb
        string_to_sign = request.get_method().upper() + NEW_LINE

        for field in ["Content-encoding", "Content-language", "Content-length", "Content-MD5", "Content-type"]:
            if request.has_header(field):
                string_to_sign += request.get_header(field)
            string_to_sign += NEW_LINE

        # Date
        if for_tables:
            string_to_sign += request.get_header("x-ms-date") + NEW_LINE
        else:
            string_to_sign += NEW_LINE

        for field in ["If-modified-since", "If-match", "If-none-match", "If-unmodified-since", "Range"]:
            if request.get_header(field) is not None:
                string_to_sign += request.get_header(field)
            string_to_sign += NEW_LINE

        # Canonicalized headers
        if not for_tables:
            string_to_sign += canonicalized_headers + NEW_LINE

        # Canonicalized resource
        string_to_sign += "/" + self._account + path
        for key, value in parse_qs(query).iteritems():
            string_to_sign += NEW_LINE
            string_to_sign += key + ":" + value[0]

        request.add_header(
            "Authorization",
            "SharedKey "
            + self._account
            + ":"
            + base64.encodestring(
                hmac.new(self._key, unicode(string_to_sign).encode("utf-8"), hashlib.sha256).digest()
            ).strip(),
        )
        return request