Mod xml curl Python example
From FreeSWITCH Wiki
Account authentication with Python
This code will output the xml needed to authenticate users. Uses the lxml and hashlib modules, so make sure you install them before running.
#!/usr/bin/env python
print "Content-Type: text/xml"
print
from lxml.builder import E
from lxml import etree
import hashlib
def create_base_directory_xml_doc():
doc = (
E.document(
E.section()
,type="freeswitch/xml")
)
return doc
def hash_password(domain, username, password):
hash = hashlib.md5()
hash.update(username + ":" + domain + ":" + password)
password_hash = hash.hexdigest()
password_param = "a1-hash"
return password_param, password_hash
def add_directory_domain_user(doc, domain, username, password):
section = doc.find("section")
password_param = "password"
#comment out the line below to test with plain text passwords
password_param, password = hash_password(domain, username, password)
domain = (
E.domain(
E.params(
E.param(
name="dial-string",
value='{presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}'
)
),
E.groups(
E.group(
E.user(
E.params(
E.param(name=password_param, password=password)
)
,id=username)
,name="default")
)
,name=domain)
)
section.append(domain)
document = create_base_directory_xml_doc()
add_directory_domain_user(document, "domain234.blah.testtld", "user123", "password345")
print(etree.tostring(document, pretty_print=True))
sample output
Content-Type: text/xml
<document type="freeswitch/xml">
<section>
<domain name="domain234.blah.testtld">
<params>
<param name="dial-string" value="{presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}"/>
</params>
<groups>
<group name="default">
<user id="user123">
<params>
<param password="3333600b55ffe6621ef6f9931886aae3" name="a1-hash"/>
</params>
</user>
</group>
</groups>
</domain>
</section>
</document>

