def handle(self): """NOTE: The result of this function is *backwards*. It returns None for success. Just like C. """ sub = self.subscriber params = dict(sub.params) params.update(self.data) h = httplib2.Http() h.follow_all_redirects = sub.follow_all_redirects headers = MultiDict(sub.headers) body = sub.body if sub.username: h.add_credentials(sub.username, sub.password) params = dict((key, simplejson.dumps(value)) for key, value in params.items()) if sub.method == "GET": #merge params with query string qs = sub.queryString if qs: qs += "&" else: qs = "?" qs += urllib.urlencode(params) headers['Content-Length'] = '0' else: body = urllib.urlencode(params) headers['Content-Length'] = str(len(body)) if not headers.has_key('Content-Type'): headers['Content-Type'] = 'application/x-www-form-urlencoded' username = config.get('username', None) password = config.get('password', None) if username: headers['AUTHORIZATION'] = 'WSSE profile="UsernameToken"' headers['X_WSSE'] = wsse_header(username, password) __traceback_info__ = '%s %s (%i bytes in body)' % (sub.method, self.url, len(body)) log.info('Sending event %s %s (%i bytes in body, id=%s' % (self.url, sub.method, len(body), self.id)) self.last_sent = datetime.now() self.failures += 1 #if we succeed, we won't care that someone called #us a failure if self.failures > MAX_SEND_FAILURES: self.fail() return None try: response = h.request(self.url, method=sub.method, body=body, headers=headers, redirections=sub.redirections) except socket.error, e: print >> sys.stderr, 'Error doing %s %s (body length: %i bytes)' % (self.url, sub.method, len(body)) return "%s" % e, ''
def rest_invoke(*args, **kwargs): #merge in wsse headers headers = kwargs.get('headers', {}) headers['Authorization'] = 'WSSE profile="UsernameToken"' headers['X-WSSE'] = wsse_header(username, password) kwargs['headers'] = headers if 'params' in kwargs: kwargs['params'] = dict((key, dumps(value)) for key, value in kwargs['params'].items()) return base_rest_invoke(*args, **kwargs)
def cabochon_message(self, path, params): #we need a wsse header params = dict((key, dumps(value)) for key, value in params.items()) extra_environ = {'HTTP_AUTHORIZATION' : 'WSSE profile="UsernameToken"', 'HTTP_X_WSSE' : wsse_header('topp', 'secret') } app = paste.fixture.TestApp(self.app, extra_environ=extra_environ) response = app.post(path, params = params) assert response.body == '{"status": "accepted"}' return response
def test_working(): users = {'jefferson' : 'airplane'} wsse_app = WSSEAuthMiddleware(wsgi_app, users) test_app = paste.fixture.TestApp(wsse_app, extra_environ={ 'HTTP_AUTHORIZATION': 'WSSE profile="UsernameToken"', 'HTTP_X_WSSE' : wsse_header('jefferson', 'airplane') }) test_app.get('/').mustcontain('Hello, jefferson')
def rest_invoke(self, *args, **kwargs): if self.username: headers = kwargs.get('headers', {}) headers['Authorization'] = 'WSSE profile="UsernameToken"' headers['X-WSSE'] = wsse_header(self.username, self.password) kwargs['headers'] = headers kwargs['params'] = dict((key, dumps(value)) for key, value in kwargs['params'].items()) try: return rest_invoke(*args, **kwargs) except socket.error, e: raise ServerError("Couldn't connect to server", e)
def test_login(self): """Tests whether the given username and password work with the given server. If the server is down, returns False even though queued messages with this username/password might eventually be accepted.""" headers = {} if self.username: headers['Authorization'] = 'WSSE profile="UsernameToken"' headers['X-WSSE'] = wsse_header(self.username, self.password) result, body = rest_invoke(self.server_url + "/event", method="GET", headers = headers, resp=True) return result['status'] == '200'
def test_required(): users = {'jefferson' : 'airplane'} wsse_app = WSSEAuthMiddleware(wsgi_app, users, required=True) test_app = paste.fixture.TestApp(wsse_app) result = test_app.get('/', status=401) test_app = paste.fixture.TestApp(wsse_app, extra_environ={ 'HTTP_AUTHORIZATION': 'WSSE profile="UsernameToken"', 'HTTP_X_WSSE' : wsse_header('jefferson', 'airplane') }) test_app.get('/').mustcontain('Hello, jefferson')
def send_one(self): return if not len(self.messages): return try: self.lock.acquire() message = self.messages[0] finally: self.lock.release() url, message, id = message.url, message.message, message.id if not url: return url #failure log.debug("sending a message") params = loads(message) headers = {} if params.has_key("__extra"): extra = params['__extra'] del params['__extra'] username = extra['username'] password = extra['password'] headers['Authorization'] = 'WSSE profile="UsernameToken"' headers['X-WSSE'] = wsse_header(username, password) params = dict((key, dumps(value)) for key, value in params.items()) #try to send it to the server result = rest_invoke(url, method="POST", params=params, headers = headers) try: result = loads(result) except ValueError: log.warning("Unparsable result from Cabochon: %s" % result) return False if result.get('status', None) != 'accepted': return #failure self.messages[0].destroySelf() try: self.lock.acquire() self.messages = self.messages[1:] finally: self.lock.release() return True