def calc_signature(self, request, params): logger.debug("Calculating signature using v2 auth.") split = urlsplit(request.url) path = split.path if len(path) == 0: path = '/' string_to_sign = '%s\n%s\n%s\n' % (request.method, split.netloc, path) lhmac = hmac.new(self.credentials.secret_key.encode('utf-8'), digestmod=sha256) pairs = [] for key in sorted(params): # Any previous signature should not be a part of this # one, so we skip that particular key. This prevents # issues during retries. if key == 'Signature': continue value = six.text_type(params[key]) pairs.append(quote(key.encode('utf-8'), safe='') + '=' + quote(value.encode('utf-8'), safe='-_~')) qs = '&'.join(pairs) string_to_sign += qs logger.debug('String to sign: %s', string_to_sign) lhmac.update(string_to_sign.encode('utf-8')) b64 = base64.b64encode(lhmac.digest()).strip().decode('utf-8') return (qs, b64)
def _type_check(param, errors, name): if not isinstance(param, valid_types): valid_type_names = [six.text_type(t) for t in valid_types] errors.report(name, 'invalid type', param=param, valid_types=valid_type_names) return False return True
def calc_signature(self, request, params): logger.debug("Calculating signature using v2 auth.") split = urlsplit(request.url) path = split.path if len(path) == 0: path = '/' string_to_sign = '%s\n%s\n%s\n' % (request.method, split.netloc, path) lhmac = hmac.new(self.credentials.secret_key.encode('utf-8'), digestmod=sha256) pairs = [] for key in sorted(params): # Any previous signature should not be a part of this # one, so we skip that particular key. This prevents # issues during retries. if key == 'Signature': continue value = six.text_type(params[key]) pairs.append( quote(key.encode('utf-8'), safe='') + '=' + quote(value.encode('utf-8'), safe='-_~')) qs = '&'.join(pairs) string_to_sign += qs logger.debug('String to sign: %s', string_to_sign) lhmac.update(string_to_sign.encode('utf-8')) b64 = base64.b64encode(lhmac.digest()).strip().decode('utf-8') return (qs, b64)
def _validate_timestamp(self, param, shape, errors, name): # We don't use @type_check because datetimes are a bit # more flexible. You can either provide a datetime # object, or a string that parses to a datetime. is_valid_type = self._type_check_datetime(param) if not is_valid_type: valid_type_names = [six.text_type(datetime), "timestamp-string"] errors.report(name, "invalid type", param=param, valid_types=valid_type_names)
def _validate_timestamp(self, param, shape, errors, name): # We don't use @type_check because datetimes are a bit # more flexible. You can either provide a datetime # object, or a string that parses to a datetime. is_valid_type = self._type_check_datetime(param) if not is_valid_type: valid_type_names = [six.text_type(datetime), 'timestamp-string'] errors.report(name, 'invalid type', param=param, valid_types=valid_type_names)
def __exit__(self, exc_type, exc_value, *args): cancel = False cancel_msg = '' cancel_exc_type = FatalError # If a exception was raised in the context handler, signal to cancel # all of the inprogress futures in the shutdown. if exc_type: cancel = True cancel_msg = six.text_type(exc_value) if not cancel_msg: cancel_msg = repr(exc_value) # If it was a KeyboardInterrupt, the cancellation was initiated # by the user. if isinstance(exc_value, KeyboardInterrupt): cancel_exc_type = CancelledError self._shutdown(cancel, cancel_msg, cancel_exc_type)
def _validate_document(self, params, shape, errors, name): if params is None: return if isinstance(params, dict): for key in params: self._validate_document(params[key], shape, errors, key) elif isinstance(params, list): for index, entity in enumerate(params): self._validate_document(entity, shape, errors, '%s[%d]' % (name, index)) elif not isinstance(params, (six.string_types, int, bool, float)): valid_types = (str, int, bool, float, list, dict) valid_type_names = [six.text_type(t) for t in valid_types] errors.report(name, 'invalid type for document', param=params, param_type=type(params), valid_types=valid_type_names)
def percent_encode(input_str, safe=SAFE_CHARS): """Urlencodes a string. Whereas percent_encode_sequence handles taking a dict/sequence and producing a percent encoded string, this function deals only with taking a string (not a dict/sequence) and percent encoding it. If given the binary type, will simply URL encode it. If given the text type, will produce the binary type by UTF-8 encoding the text. If given something else, will convert it to the text type first. """ # If its not a binary or text string, make it a text string. if not isinstance(input_str, (six.binary_type, six.text_type)): input_str = six.text_type(input_str) # If it's not bytes, make it bytes by UTF-8 encoding it. if not isinstance(input_str, six.binary_type): input_str = input_str.encode('utf-8') return quote(input_str, safe=safe)
def calc_signature(self, request, params): logger.debug("Calculating signature using v2 auth.") split = urlsplit(request.url) path = split.path if len(path) == 0: path = '/' string_to_sign = '%s\n%s\n%s\n' % (request.method, split.netloc, path) lhmac = hmac.new(self.credentials.secret_key.encode('utf-8'), digestmod=sha256) pairs = [] for key in sorted(params): value = six.text_type(params[key]) pairs.append( quote(key.encode('utf-8'), safe='') + '=' + quote(value.encode('utf-8'), safe='-_~')) qs = '&'.join(pairs) string_to_sign += qs logger.debug('String to sign: %s', string_to_sign) lhmac.update(string_to_sign.encode('utf-8')) b64 = base64.b64encode(lhmac.digest()).strip().decode('utf-8') return (qs, b64)
def calc_signature(self, request, params): logger.debug("Calculating signature using v2 auth.") split = urlsplit(request.url) path = split.path if len(path) == 0: path = '/' string_to_sign = '%s\n%s\n%s\n' % (request.method, split.netloc, path) lhmac = hmac.new(self.credentials.secret_key.encode('utf-8'), digestmod=sha256) pairs = [] for key in sorted(params): value = six.text_type(params[key]) pairs.append(quote(key.encode('utf-8'), safe='') + '=' + quote(value.encode('utf-8'), safe='-_~')) qs = '&'.join(pairs) string_to_sign += qs logger.debug('String to sign: %s', string_to_sign) lhmac.update(string_to_sign.encode('utf-8')) b64 = base64.b64encode(lhmac.digest()).strip().decode('utf-8') return (qs, b64)
def _document_str(self, section, value, path): # We do the string conversion because this might accept a type that # we don't specifically address. section.write(u"'%s'," % six.text_type(value))
def _default_serialize(self, xmlnode, params, shape, name): node = ElementTree.SubElement(xmlnode, name) node.text = six.text_type(params)