Sign a string with the secret key, returning base64 encoded results. By default the configured secret key is used, but may be overridden as an argument. Useful for REST authentication. See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html string_to_sign should b
(method='GET', canonical_uri='/', params=None, cur_headers=None)
| 79 | |
| 80 | |
| 81 | def sign_request_v2(method='GET', canonical_uri='/', params=None, cur_headers=None): |
| 82 | """Sign a string with the secret key, returning base64 encoded results. |
| 83 | By default the configured secret key is used, but may be overridden as |
| 84 | an argument. |
| 85 | |
| 86 | Useful for REST authentication. See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html |
| 87 | string_to_sign should be utf-8 "bytes". |
| 88 | """ |
| 89 | # valid sub-resources to be included in sign v2: |
| 90 | SUBRESOURCES_TO_INCLUDE = ['acl', 'lifecycle', 'location', 'logging', |
| 91 | 'notification', 'partNumber', 'policy', |
| 92 | 'requestPayment', 'tagging', 'torrent', |
| 93 | 'uploadId', 'uploads', 'versionId', |
| 94 | 'versioning', 'versions', 'website', |
| 95 | # Missing of aws s3 doc but needed |
| 96 | 'delete', 'cors', 'restore'] |
| 97 | |
| 98 | if cur_headers is None: |
| 99 | cur_headers = SortedDict(ignore_case = True) |
| 100 | |
| 101 | access_key = Config.Config().access_key |
| 102 | |
| 103 | string_to_sign = method + "\n" |
| 104 | string_to_sign += cur_headers.get("content-md5", "") + "\n" |
| 105 | string_to_sign += cur_headers.get("content-type", "") + "\n" |
| 106 | string_to_sign += cur_headers.get("date", "") + "\n" |
| 107 | |
| 108 | for header in sorted(cur_headers.keys()): |
| 109 | if header.startswith("x-amz-"): |
| 110 | string_to_sign += header + ":" + cur_headers[header] + "\n" |
| 111 | if header.startswith("x-emc-"): |
| 112 | string_to_sign += header + ":"+ cur_headers[header] + "\n" |
| 113 | |
| 114 | |
| 115 | canonical_uri = s3_quote(canonical_uri, quote_backslashes=False, unicode_output=True) |
| 116 | canonical_querystring = format_param_str(params, limited_keys=SUBRESOURCES_TO_INCLUDE) |
| 117 | # canonical_querystring would be empty if no param given, otherwise it will |
| 118 | # starts with a "?" |
| 119 | canonical_uri += canonical_querystring |
| 120 | |
| 121 | string_to_sign += canonical_uri |
| 122 | |
| 123 | debug("SignHeaders: " + repr(string_to_sign)) |
| 124 | signature = decode_from_s3(sign_string_v2(encode_to_s3(string_to_sign))) |
| 125 | |
| 126 | new_headers = SortedDict(list(cur_headers.items()), ignore_case=True) |
| 127 | new_headers["Authorization"] = "AWS " + access_key + ":" + signature |
| 128 | |
| 129 | return new_headers |
| 130 | __all__.append("sign_request_v2") |
| 131 | |
| 132 |
no test coverage detected
searching dependent graphs…