def _get_token(self, host, path):
        '''
        Returns token for the request.

        host: the service bus service request.
        path: the service bus service request.
        '''
        wrap_scope = 'http://' + host + path + self.issuer + self.account_key

        # Check whether has unexpired cache, return cached token if it is still
        # usable.
        if wrap_scope in _tokens:
            token = _tokens[wrap_scope]
            if not self._token_is_expired(token):
                return token

        # get token from accessconstrol server
        request = HTTPRequest()
        request.protocol_override = 'https'
        request.host = host.replace('.servicebus.', '-sb.accesscontrol.')
        request.method = 'POST'
        request.path = '/WRAPv0.9'
        request.body = ('wrap_name=' + url_quote(self.issuer) +
                        '&wrap_password='******'&wrap_scope=' +
                        url_quote('http://' + host + path)).encode('utf-8')
        request.headers.append(('Content-Length', str(len(request.body))))
        resp = self._httpclient.perform_request(request)

        token = resp.body.decode('utf-8')
        token = url_unquote(token[token.find('=') + 1:token.rfind('&')])
        _tokens[wrap_scope] = token

        return token
    def _get_token(self, host, path):
        '''
        Returns token for the request.

        host: the service bus service request.
        path: the service bus service request.
        '''
        wrap_scope = 'http://' + host + path + self.issuer + self.account_key

        # Check whether has unexpired cache, return cached token if it is still
        # usable.
        if wrap_scope in _tokens:
            token = _tokens[wrap_scope]
            if not self._token_is_expired(token):
                return token

        # get token from accessconstrol server
        request = HTTPRequest()
        request.protocol_override = 'https'
        request.host = host.replace('.servicebus.', '-sb.accesscontrol.')
        request.method = 'POST'
        request.path = '/WRAPv0.9'
        request.body = ('wrap_name=' + url_quote(self.issuer) +
                        '&wrap_password='******'&wrap_scope=' +
                        url_quote('http://' + host + path)).encode('utf-8')
        request.headers.append(('Content-Length', str(len(request.body))))
        resp = self._httpclient.perform_request(request)

        token = resp.body.decode('utf-8')
        token = url_unquote(token[token.find('=') + 1:token.rfind('&')])
        _tokens[wrap_scope] = token

        return token
Пример #3
0
    def _get_authorization(self, request, httpclient):
        uri = httpclient.get_uri(request)
        uri = url_quote(uri, '').lower()
        expiry = str(self._get_expiry())

        to_sign = uri + '\n' + expiry
        signature = url_quote(_sign_string(self.key_value, to_sign, False), '')

        auth_format = 'SharedAccessSignature sig={0}&se={1}&skn={2}&sr={3}'
        auth = auth_format.format(signature, expiry, self.key_name, uri)

        return auth
Пример #4
0
    def _get_authorization(self, request, httpclient):
        uri = httpclient.get_uri(request)
        uri = url_quote(uri, '').lower()
        expiry = str(self._get_expiry())

        to_sign = uri + '\n' + expiry
        signature = url_quote(_sign_string(self.key_value, to_sign, False), '')

        auth_format = 'SharedAccessSignature sig={0}&se={1}&skn={2}&sr={3}'
        auth = auth_format.format(signature, expiry, self.key_name, uri)

        return auth
    def generate_signed_query_string(self,
                                     path,
                                     resource_type,
                                     shared_access_policy,
                                     version=X_MS_VERSION,
                                     cache_control=None,
                                     content_disposition=None,
                                     content_encoding=None,
                                     content_language=None,
                                     content_type=None,
                                     table_name=None):
        '''
        Generates the query string for path, resource type and shared access
        policy.

        path:
            the resource
        resource_type:
            'b' for blob, 'c' for container, None for queue/table
        shared_access_policy:
            shared access policy
        version:
            x-ms-version for storage service, or None to get a signed query
            string compatible with pre 2012-02-12 clients, where the version
            is not included in the query string.
        cache_control:
            Response header value for Cache-Control when resource is accessed
            using this shared access signature.
        content_disposition:
            Response header value for Content-Disposition when resource is accessed
            using this shared access signature.
        content_encoding:
            Response header value for Content-Encoding when resource is accessed
            using this shared access signature.
        content_language:
            Response header value for Content-Language when resource is accessed
            using this shared access signature.
        content_type:
            Response header value for Content-Type when resource is accessed
            using this shared access signature.
        table_name:
            Name of table.
        '''
        query_dict = self._generate_signed_query_dict(
            path,
            resource_type,
            shared_access_policy,
            version,
            cache_control,
            content_disposition,
            content_encoding,
            content_language,
            content_type,
            table_name,
        )
        return '&'.join([
            '{0}={1}'.format(n, url_quote(v, '/()$=\','))
            for n, v in query_dict.items() if v is not None
        ])
 def _upload_chunk(self, chunk_offset, chunk_data):
     range_id = url_quote(_encode_base64('{0:032d}'.format(chunk_offset)))
     self.blob_service.put_block(self.container_name,
                                 self.blob_name,
                                 chunk_data,
                                 range_id,
                                 x_ms_lease_id=self.x_ms_lease_id)
     return range_id
Пример #7
0
 def _upload_chunk(self, chunk_offset, chunk_data):
     range_id = url_quote(_encode_base64('{0:032d}'.format(chunk_offset)))
     self.blob_service.put_block(
         self.container_name,
         self.blob_name,
         chunk_data,
         range_id,
         x_ms_lease_id=self.x_ms_lease_id
     )
     return range_id
    def _convert_query_string(self, query_string):
        ''' Converts query string to str. The order of name, values is very
        important and can't be wrong.'''

        convert_str = ''
        if SIGNED_START in query_string:
            convert_str += SIGNED_START + '=' + \
                url_quote(query_string[SIGNED_START]) + '&'
        convert_str += SIGNED_EXPIRY + '=' + \
            url_quote(query_string[SIGNED_EXPIRY]) + '&'
        convert_str += SIGNED_PERMISSION + '=' + \
            query_string[SIGNED_PERMISSION] + '&'
        convert_str += SIGNED_RESOURCE + '=' + \
            query_string[SIGNED_RESOURCE] + '&'

        if SIGNED_IDENTIFIER in query_string:
            convert_str += SIGNED_IDENTIFIER + '=' + \
                query_string[SIGNED_IDENTIFIER] + '&'
        if SIGNED_VERSION in query_string:
            convert_str += SIGNED_VERSION + '=' + \
                query_string[SIGNED_VERSION] + '&'
        if SIGNED_CACHE_CONTROL in query_string:
            convert_str += SIGNED_CACHE_CONTROL + '=' + \
                query_string[SIGNED_CACHE_CONTROL] + '&'
        if SIGNED_CONTENT_DISPOSITION in query_string:
            convert_str += SIGNED_CONTENT_DISPOSITION + '=' + \
                query_string[SIGNED_CONTENT_DISPOSITION] + '&'
        if SIGNED_CONTENT_ENCODING in query_string:
            convert_str += SIGNED_CONTENT_ENCODING + '=' + \
                query_string[SIGNED_CONTENT_ENCODING] + '&'
        if SIGNED_CONTENT_LANGUAGE in query_string:
            convert_str += SIGNED_CONTENT_LANGUAGE + '=' + \
                query_string[SIGNED_CONTENT_LANGUAGE] + '&'
        if SIGNED_CONTENT_TYPE in query_string:
            convert_str += SIGNED_CONTENT_TYPE + '=' + \
                query_string[SIGNED_CONTENT_TYPE] + '&'
        convert_str += SIGNED_SIGNATURE + '=' + \
            url_quote(query_string[SIGNED_SIGNATURE]) + '&'
        return convert_str
    def _convert_query_string(self, query_string):
        ''' Converts query string to str. The order of name, values is very
        important and can't be wrong.'''

        convert_str = ''
        if SIGNED_START in query_string:
            convert_str += SIGNED_START + '=' + \
                url_quote(query_string[SIGNED_START]) + '&'
        convert_str += SIGNED_EXPIRY + '=' + \
            url_quote(query_string[SIGNED_EXPIRY]) + '&'
        convert_str += SIGNED_PERMISSION + '=' + \
            query_string[SIGNED_PERMISSION] + '&'
        convert_str += SIGNED_RESOURCE + '=' + \
            query_string[SIGNED_RESOURCE] + '&'

        if SIGNED_IDENTIFIER in query_string:
            convert_str += SIGNED_IDENTIFIER + '=' + \
                query_string[SIGNED_IDENTIFIER] + '&'
        if SIGNED_VERSION in query_string:
            convert_str += SIGNED_VERSION + '=' + \
                query_string[SIGNED_VERSION] + '&'
        if SIGNED_CACHE_CONTROL in query_string:
            convert_str += SIGNED_CACHE_CONTROL + '=' + \
                query_string[SIGNED_CACHE_CONTROL] + '&'
        if SIGNED_CONTENT_DISPOSITION in query_string:
            convert_str += SIGNED_CONTENT_DISPOSITION + '=' + \
                query_string[SIGNED_CONTENT_DISPOSITION] + '&'
        if SIGNED_CONTENT_ENCODING in query_string:
            convert_str += SIGNED_CONTENT_ENCODING + '=' + \
                query_string[SIGNED_CONTENT_ENCODING] + '&'
        if SIGNED_CONTENT_LANGUAGE in query_string:
            convert_str += SIGNED_CONTENT_LANGUAGE + '=' + \
                query_string[SIGNED_CONTENT_LANGUAGE] + '&'
        if SIGNED_CONTENT_TYPE in query_string:
            convert_str += SIGNED_CONTENT_TYPE + '=' + \
                query_string[SIGNED_CONTENT_TYPE] + '&'
        convert_str += SIGNED_SIGNATURE + '=' + \
            url_quote(query_string[SIGNED_SIGNATURE]) + '&'
        return convert_str
    def generate_signed_query_string(self, path, resource_type,
                                     shared_access_policy,
                                     version=X_MS_VERSION,
                                     cache_control=None, content_disposition=None,
                                     content_encoding=None, content_language=None,
                                     content_type=None, table_name=None):
        '''
        Generates the query string for path, resource type and shared access
        policy.

        path:
            the resource
        resource_type:
            'b' for blob, 'c' for container, None for queue/table
        shared_access_policy:
            shared access policy
        version:
            x-ms-version for storage service, or None to get a signed query
            string compatible with pre 2012-02-12 clients, where the version
            is not included in the query string.
        cache_control:
            Response header value for Cache-Control when resource is accessed
            using this shared access signature.
        content_disposition:
            Response header value for Content-Disposition when resource is accessed
            using this shared access signature.
        content_encoding:
            Response header value for Content-Encoding when resource is accessed
            using this shared access signature.
        content_language:
            Response header value for Content-Language when resource is accessed
            using this shared access signature.
        content_type:
            Response header value for Content-Type when resource is accessed
            using this shared access signature.
        table_name:
            Name of table.
        '''
        query_dict = self._generate_signed_query_dict(
            path,
            resource_type,
            shared_access_policy,
            version,
            cache_control,
            content_disposition,
            content_encoding,
            content_language,
            content_type,
            table_name,
        )
        return '&'.join(['{0}={1}'.format(n, url_quote(v, '/()$=\',')) for n, v in query_dict.items() if v is not None])
    def _convert_query_string(self, query_string):
        ''' Converts query string to str. The order of name, values is very import and can't be wrong.'''

        convert_str = ''
        if SIGNED_START in query_string:
            convert_str += SIGNED_START + '=' + query_string[SIGNED_START] + '&'
        convert_str += SIGNED_EXPIRY + '=' + query_string[SIGNED_EXPIRY] + '&'
        convert_str += SIGNED_PERMISSION + '=' + query_string[SIGNED_PERMISSION] + '&'
        convert_str += SIGNED_RESOURCE + '=' + query_string[SIGNED_RESOURCE] + '&'

        if SIGNED_IDENTIFIER in query_string:
            convert_str += SIGNED_IDENTIFIER + '=' + query_string[SIGNED_IDENTIFIER] + '&'
        convert_str += SIGNED_SIGNATURE + '=' + url_quote(query_string[SIGNED_SIGNATURE]) + '&'
        return convert_str
    def _convert_query_string(self, query_string):
        ''' Converts query string to str. The order of name, values is very
        important and can't be wrong.'''

        convert_str = ''
        if SIGNED_START in query_string:
            convert_str += SIGNED_START + '=' + \
                url_quote(query_string[SIGNED_START]) + '&'
        convert_str += SIGNED_EXPIRY + '=' + \
            url_quote(query_string[SIGNED_EXPIRY]) + '&'
        convert_str += SIGNED_PERMISSION + '=' + \
            query_string[SIGNED_PERMISSION] + '&'
        convert_str += SIGNED_RESOURCE + '=' + \
            query_string[SIGNED_RESOURCE] + '&'

        if SIGNED_IDENTIFIER in query_string:
            convert_str += SIGNED_IDENTIFIER + '=' + \
                query_string[SIGNED_IDENTIFIER] + '&'
        if SIGNED_VERSION in query_string:
            convert_str += SIGNED_VERSION + '=' + \
                query_string[SIGNED_VERSION] + '&'
        convert_str += SIGNED_SIGNATURE + '=' + \
            url_quote(query_string[SIGNED_SIGNATURE]) + '&'
        return convert_str