def _remove_qs(self, url): ''' Removes a query string from a URL before signing. :param url: The URL to strip. :type url: str ''' scheme, netloc, path, query, fragment = urlsplit(url) return urlunsplit((scheme, netloc, path, '', fragment))
def fake_request(self, method, url, mock_request, ofly_params, user_id=None, hash_meth='sha1', **kwargs): mock_request.return_value = self.response user_id = user_id or self.service.user_id service = OflyService(self.app_id, self.app_secret, name='service', authorize_url=self.authorize_url, base_url=self.base_url) session = service.get_session(self.user_id) r = session.request(method, url, user_id=user_id, hash_meth=hash_meth, **deepcopy(kwargs)) url = self.session._set_url(url) kwargs.setdefault('params', {}) if is_basestring(kwargs['params']): kwargs['params'] = dict(parse_qsl(kwargs['params'])) url_path = urlsplit(url).path signature_base_string = self.service.app_secret + url_path + '?' if len(kwargs['params']): signature_base_string += \ self.fake_get_sorted_params(kwargs['params']) + '&' signature_base_string += self.fake_get_sorted_params(ofly_params) all_params = dict(tuple(ofly_params.items()) + tuple(kwargs['params'].items())) kwargs['params'] = self.fake_get_sorted_params(all_params) if not isinstance(kwargs['params'], bytes): kwargs['params'] = kwargs['params'].encode('utf-8') mock_request.assert_called_with(method, url, timeout=OFLY_DEFAULT_TIMEOUT, **kwargs) return r
def fake_request(self, method, url, mock_request, ofly_params, user_id=None, hash_meth='sha1', **kwargs): mock_request.return_value = self.response user_id = user_id or self.service.user_id service = OflyService(self.app_id, self.app_secret, name='service', authorize_url=self.authorize_url, base_url=self.base_url) session = service.get_session(self.user_id) r = session.request(method, url, user_id=user_id, hash_meth=hash_meth, **deepcopy(kwargs)) url = self.session._set_url(url) kwargs.setdefault('params', {}) if is_basestring(kwargs['params']): kwargs['params'] = dict(parse_qsl(kwargs['params'])) url_path = urlsplit(url).path signature_base_string = self.service.app_secret + url_path + '?' if len(kwargs['params']): signature_base_string += \ self.fake_get_sorted_params(kwargs['params']) + '&' signature_base_string += self.fake_get_sorted_params(ofly_params) all_params = dict( tuple(ofly_params.items()) + tuple(kwargs['params'].items())) kwargs['params'] = self.fake_get_sorted_params(all_params) if not isinstance(kwargs['params'], bytes): kwargs['params'] = kwargs['params'].encode('utf-8') mock_request.assert_called_with(method, url, timeout=OFLY_DEFAULT_TIMEOUT, **kwargs) return r
def sign(url, app_id, app_secret, hash_meth='sha1', **params): ''' A signature method which generates the necessary Ofly parameters. :param app_id: The oFlyAppId, i.e. "application ID". :type app_id: str :param app_secret: The oFlyAppSecret, i.e. "shared secret". :type app_secret: str :param hash_meth: The hash method to use for signing, defaults to "sha1". :type hash_meth: str :param \*\*params: Additional parameters. :type \*\*\params: dict ''' hash_meth_str = hash_meth if hash_meth == 'sha1': hash_meth = sha1 elif hash_meth == 'md5': hash_meth = md5 else: raise TypeError('hash_meth must be one of "sha1", "md5"') now = datetime.utcnow() milliseconds = now.microsecond // 1000 time_format = '%Y-%m-%dT%H:%M:%S.{0}Z'.format(milliseconds) ofly_params = { 'oflyAppId': app_id, 'oflyHashMeth': hash_meth_str.upper(), 'oflyTimestamp': now.strftime(time_format) } url_path = urlsplit(url).path signature_base_string = app_secret + url_path + '?' if len(params): signature_base_string += get_sorted_params(params) + '&' signature_base_string += get_sorted_params(ofly_params) if not isinstance(signature_base_string, bytes): signature_base_string = signature_base_string.encode('utf-8') ofly_params['oflyApiSig'] = \ hash_meth(signature_base_string).hexdigest() all_params = dict(tuple(ofly_params.items()) + tuple(params.items())) return get_sorted_params(all_params)
def sign(url, app_id, app_secret, hash_meth='sha1', **params): ''' A signature method which generates the necessary Ofly parameters. :param app_id: The oFlyAppId, i.e. "application ID". :type app_id: str :param app_secret: The oFlyAppSecret, i.e. "shared secret". :type app_secret: str :param hash_meth: The hash method to use for signing, defaults to "sha1". :type hash_meth: str :param \*\*params: Additional parameters. :type \*\*\params: dict ''' hash_meth_str = hash_meth if hash_meth == 'sha1': hash_meth = sha1 elif hash_meth == 'md5': hash_meth = md5 else: raise TypeError('hash_meth must be one of "sha1", "md5"') now = datetime.utcnow() milliseconds = now.microsecond // 1000 time_format = '%Y-%m-%dT%H:%M:%S.{0}Z'.format(milliseconds) ofly_params = {'oflyAppId': app_id, 'oflyHashMeth': hash_meth_str.upper(), 'oflyTimestamp': now.strftime(time_format)} url_path = urlsplit(url).path signature_base_string = app_secret + url_path + '?' if len(params): signature_base_string += get_sorted_params(params) + '&' signature_base_string += get_sorted_params(ofly_params) if not isinstance(signature_base_string, bytes): signature_base_string = signature_base_string.encode('utf-8') ofly_params['oflyApiSig'] = \ hash_meth(signature_base_string).hexdigest() all_params = dict(tuple(ofly_params.items()) + tuple(params.items())) return get_sorted_params(all_params)