Skip to content

Commit

Permalink
Add auth token and if to outbound socket to sign every fetch xml request
Browse files Browse the repository at this point in the history
  • Loading branch information
bevenky committed May 16, 2011
1 parent ecb8e7a commit f49c06d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/plivo/rest/freeswitch/helpers.py
Expand Up @@ -100,16 +100,16 @@ def get_method(self):
class HTTPRequest:
"""Helper class for preparing HTTP requests.
"""
def __init__(self, id ='', token =''):
def __init__(self, auth_id ='', auth_token =''):
"""initialize a object
id: Plivo SID/ID
token: Plivo token
returns a HTTPRequest object
"""
self.id = id
self.token = token
self.auth_id = auth_id
self.auth_token = auth_token
self.opener = None

def _build_get_uri(self, uri, params):
Expand All @@ -136,7 +136,8 @@ def _prepare_http_request(self, uri, params, method='POST'):
if method and (method == 'DELETE' or method == 'PUT'):
request.http_method = method

authstring = base64.encodestring('%s:%s' % (self.id, self.token))
authstring = base64.encodestring('%s:%s' % (self.auth_id,
self.auth_token))
authstring = authstring.replace('\n', '')
request.add_header("Authorization", "Basic %s" % authstring)

Expand Down
13 changes: 9 additions & 4 deletions src/plivo/rest/freeswitch/outbound_server.py
Expand Up @@ -20,8 +20,8 @@


"""
PlivoOutboundServer is our event_socket server listening for connection
with Freeswitch.
PlivoOutboundServer is our event_socket server listening for connection
with Freeswitch.
This server is listening by default on 127.0.0.1:8084
Expand All @@ -46,6 +46,10 @@ def __init__(self, configfile, daemon=False,
fs_port = int(fs_port)
self.default_answer_url = helpers.get_conf_value(self._config,
'freeswitch', 'DEFAULT_ANSWER_URL')
self.auth_id = helpers.get_conf_value(self._config,
'rest_server', 'AUTH_ID')
self.auth_token = helpers.get_conf_value(self._config,
'rest_server', 'AUTH_TOKEN')
#This is where we define the connection with the
#Plivo XML grammar Processor
OutboundServer.__init__(self, (fs_host, fs_port),
Expand All @@ -62,7 +66,8 @@ def do_handle(self, socket, address):
request_id = self._get_request_id()
self.log.info("(%d) New request from %s" % (request_id, str(address)))
self._handle_class(socket, address, self.log, self.default_answer_url,
filter=self._filter, request_id=request_id)
filter=self._filter, request_id=request_id,
auth_id=self.auth_id, auth_token= self.auth_token)
self.log.info("(%d) End request from %s" % (request_id, str(address)))

def create_logger(self):
Expand Down Expand Up @@ -138,4 +143,4 @@ def start(self):
outboundserver = PlivoOutboundServer(
configfile='../../../config/default.conf',
daemon=False)
outboundserver.start()
outboundserver.start()
6 changes: 4 additions & 2 deletions src/plivo/rest/freeswitch/outbound_socket.py
Expand Up @@ -63,7 +63,7 @@ class PlivoOutboundEventSocket(OutboundEventSocket):
"""

def __init__(self, socket, address, log, default_answer_url, filter=None,
request_id=0):
request_id=0, auth_id="", auth_token=""):
self._request_id = request_id
self._log = log
self.log = RequestLogger(logger=self._log, request_id=self._request_id)
Expand All @@ -78,6 +78,8 @@ def __init__(self, socket, address, log, default_answer_url, filter=None,
self.answered = False
self._hangup_cause = ''
self.no_answer_grammar = ['Wait', 'Reject', 'Preanswer', 'Dial']
self.auth_id = auth_id
self.auth_token = auth_token
OutboundEventSocket.__init__(self, socket, address, filter)

def _protocol_send(self, command, args=""):
Expand Down Expand Up @@ -239,7 +241,7 @@ def fetch_xml(self, method='POST'):
The url result expected is an XML content which will be stored in
xml_response
"""
http_obj = HTTPRequest('111', '111')
http_obj = HTTPRequest(self.auth_id, self.auth_token)
self.xml_response = http_obj.fetch_response(self.answer_url,
self.params, method)
self.log.info("Requested to %s with %s" % (self.answer_url,
Expand Down

0 comments on commit f49c06d

Please sign in to comment.