|
| 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 | + |
| 20 | +class _StorageSharedKeyAuthentication(object): |
| 21 | + def __init__(self, account_name, account_key): |
| 22 | + self.account_name = account_name |
| 23 | + self.account_key = account_key |
| 24 | + |
| 25 | + def _get_headers(self, request, headers_to_sign): |
| 26 | + headers = dict((name.lower(), value) for name, value in request.headers if value) |
| 27 | + if 'content-length' in headers and headers['content-length'] == '0': |
| 28 | + del headers['content-length'] |
| 29 | + return '\n'.join(headers.get(x, '') for x in headers_to_sign) + '\n' |
| 30 | + |
| 31 | + def _get_verb(self, request): |
| 32 | + return request.method + '\n' |
| 33 | + |
| 34 | + def _get_canonicalized_resource(self, request): |
| 35 | + uri_path = request.path.split('?')[0] |
| 36 | + return '/' + self.account_name + uri_path |
| 37 | + |
| 38 | + def _get_canonicalized_headers(self, request): |
| 39 | + string_to_sign = '' |
| 40 | + x_ms_headers = [] |
| 41 | + for name, value in request.headers: |
| 42 | + if name.startswith('x-ms-'): |
| 43 | + x_ms_headers.append((name.lower(), value)) |
| 44 | + x_ms_headers.sort() |
| 45 | + for name, value in x_ms_headers: |
| 46 | + if value is not None: |
| 47 | + string_to_sign += ''.join([name, ':', value, '\n']) |
| 48 | + return string_to_sign |
| 49 | + |
| 50 | + def _add_authorization_header(self, request, string_to_sign): |
| 51 | + signature = _sign_string(self.account_key, string_to_sign) |
| 52 | + auth_string = 'SharedKey ' + self.account_name + ':' + signature |
| 53 | + request.headers.append(('Authorization', auth_string)) |
| 54 | + |
| 55 | + |
| 56 | +class _StorageSharedKeyAuthentication(_StorageSharedKeyAuthentication): |
| 57 | + def sign_request(self, request): |
| 58 | + string_to_sign = \ |
| 59 | + self._get_verb(request) + \ |
| 60 | + self._get_headers( |
| 61 | + request, |
| 62 | + [ |
| 63 | + 'content-encoding', 'content-language', 'content-length', |
| 64 | + 'content-md5', 'content-type', 'date', 'if-modified-since', |
| 65 | + 'if-match', 'if-none-match', 'if-unmodified-since', 'byte_range' |
| 66 | + ] |
| 67 | + ) + \ |
| 68 | + self._get_canonicalized_headers(request) + \ |
| 69 | + self._get_canonicalized_resource(request) + \ |
| 70 | + self._get_canonicalized_resource_query(request) |
| 71 | + |
| 72 | + self._add_authorization_header(request, string_to_sign) |
| 73 | + |
| 74 | + def _get_canonicalized_resource_query(self, request): |
| 75 | + request.query.sort() |
| 76 | + |
| 77 | + string_to_sign = '' |
| 78 | + for name, value in request.query: |
| 79 | + if value: |
| 80 | + string_to_sign += '\n' + name.lower() + ':' + value |
| 81 | + |
| 82 | + return string_to_sign |
| 83 | + |
| 84 | + |
| 85 | +class _StorageTableSharedKeyAuthentication(_StorageSharedKeyAuthentication): |
| 86 | + def sign_request(self, request): |
| 87 | + string_to_sign = \ |
| 88 | + self._get_verb(request) + \ |
| 89 | + self._get_headers( |
| 90 | + request, |
| 91 | + ['content-md5', 'content-type', 'x-ms-date'], |
| 92 | + ) + \ |
| 93 | + self._get_canonicalized_resource(request) + \ |
| 94 | + self._get_canonicalized_resource_query(request) |
| 95 | + |
| 96 | + self._add_authorization_header(request, string_to_sign) |
| 97 | + |
| 98 | + def _get_canonicalized_resource_query(self, request): |
| 99 | + for name, value in request.query: |
| 100 | + if name == 'comp': |
| 101 | + return '?comp=' + value |
| 102 | + return '' |
| 103 | + |
| 104 | + |
| 105 | +class _StorageNoAuthentication(object): |
| 106 | + def sign_request(self, request): |
| 107 | + pass |
| 108 | + |
| 109 | + |
| 110 | +class _StorageSASAuthentication(object): |
| 111 | + def __init__(self, sas_token): |
| 112 | + self.sas_token = sas_token |
| 113 | + |
| 114 | + def sign_request(self, request): |
| 115 | + if '?' in request.path: |
| 116 | + request.path += '&' |
| 117 | + else: |
| 118 | + request.path += '?' |
| 119 | + |
| 120 | + request.path += self.sas_token |
0 commit comments