|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# -------------------------------------------------------------------------- |
| 15 | +from ._common_conversion import ( |
| 16 | + _sign_string, |
| 17 | +) |
| 18 | + |
| 19 | +import logging |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +class _StorageSharedKeyAuthentication(object): |
| 24 | + def __init__(self, account_name, account_key): |
| 25 | + self.account_name = account_name |
| 26 | + self.account_key = account_key |
| 27 | + |
| 28 | + def _get_headers(self, request, headers_to_sign): |
| 29 | + headers = dict((name.lower(), value) for name, value in request.headers.items() if value) |
| 30 | + if 'content-length' in headers and headers['content-length'] == '0': |
| 31 | + del headers['content-length'] |
| 32 | + return '\n'.join(headers.get(x, '') for x in headers_to_sign) + '\n' |
| 33 | + |
| 34 | + def _get_verb(self, request): |
| 35 | + return request.method + '\n' |
| 36 | + |
| 37 | + def _get_canonicalized_resource(self, request): |
| 38 | + uri_path = request.path.split('?')[0] |
| 39 | + return '/' + self.account_name + uri_path |
| 40 | + |
| 41 | + def _get_canonicalized_headers(self, request): |
| 42 | + string_to_sign = '' |
| 43 | + x_ms_headers = [] |
| 44 | + for name, value in request.headers.items(): |
| 45 | + if name.startswith('x-ms-'): |
| 46 | + x_ms_headers.append((name.lower(), value)) |
| 47 | + x_ms_headers.sort() |
| 48 | + for name, value in x_ms_headers: |
| 49 | + if value is not None: |
| 50 | + string_to_sign += ''.join([name, ':', value, '\n']) |
| 51 | + return string_to_sign |
| 52 | + |
| 53 | + def _add_authorization_header(self, request, string_to_sign): |
| 54 | + signature = _sign_string(self.account_key, string_to_sign) |
| 55 | + auth_string = 'SharedKey ' + self.account_name + ':' + signature |
| 56 | + request.headers['Authorization'] = auth_string |
| 57 | + |
| 58 | + |
| 59 | +class _StorageSharedKeyAuthentication(_StorageSharedKeyAuthentication): |
| 60 | + def sign_request(self, request): |
| 61 | + string_to_sign = \ |
| 62 | + self._get_verb(request) + \ |
| 63 | + self._get_headers( |
| 64 | + request, |
| 65 | + [ |
| 66 | + 'content-encoding', 'content-language', 'content-length', |
| 67 | + 'content-md5', 'content-type', 'date', 'if-modified-since', |
| 68 | + 'if-match', 'if-none-match', 'if-unmodified-since', 'byte_range' |
| 69 | + ] |
| 70 | + ) + \ |
| 71 | + self._get_canonicalized_headers(request) + \ |
| 72 | + self._get_canonicalized_resource(request) + \ |
| 73 | + self._get_canonicalized_resource_query(request) |
| 74 | + |
| 75 | + self._add_authorization_header(request, string_to_sign) |
| 76 | + logger.debug("String_to_sign=%s", string_to_sign) |
| 77 | + |
| 78 | + def _get_canonicalized_resource_query(self, request): |
| 79 | + sorted_queries = [(name, value) for name, value in request.query.items()] |
| 80 | + sorted_queries.sort() |
| 81 | + |
| 82 | + string_to_sign = '' |
| 83 | + for name, value in sorted_queries: |
| 84 | + if value: |
| 85 | + string_to_sign += '\n' + name.lower() + ':' + value |
| 86 | + |
| 87 | + return string_to_sign |
| 88 | + |
| 89 | + |
| 90 | +class _StorageTableSharedKeyAuthentication(_StorageSharedKeyAuthentication): |
| 91 | + def sign_request(self, request): |
| 92 | + string_to_sign = \ |
| 93 | + self._get_verb(request) + \ |
| 94 | + self._get_headers( |
| 95 | + request, |
| 96 | + ['content-md5', 'content-type', 'x-ms-date'], |
| 97 | + ) + \ |
| 98 | + self._get_canonicalized_resource(request) + \ |
| 99 | + self._get_canonicalized_resource_query(request) |
| 100 | + |
| 101 | + self._add_authorization_header(request, string_to_sign) |
| 102 | + logger.debug("String_to_sign=%s", string_to_sign) |
| 103 | + |
| 104 | + def _get_canonicalized_resource_query(self, request): |
| 105 | + for name, value in request.query.items(): |
| 106 | + if name == 'comp': |
| 107 | + return '?comp=' + value |
| 108 | + return '' |
| 109 | + |
| 110 | + |
| 111 | +class _StorageNoAuthentication(object): |
| 112 | + def sign_request(self, request): |
| 113 | + pass |
| 114 | + |
| 115 | + |
| 116 | +class _StorageSASAuthentication(object): |
| 117 | + def __init__(self, sas_token): |
| 118 | + self.sas_token = sas_token |
| 119 | + |
| 120 | + def sign_request(self, request): |
| 121 | + # if 'sig=' is present, then the request has already been signed |
| 122 | + # as is the case when performing retries |
| 123 | + if 'sig=' in request.path: |
| 124 | + return |
| 125 | + if '?' in request.path: |
| 126 | + request.path += '&' |
| 127 | + else: |
| 128 | + request.path += '?' |
| 129 | + |
| 130 | + request.path += self.sas_token |
0 commit comments