Advertisement
Guest User

get-info-request.py

a guest
Jul 2nd, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import xml.etree.ElementTree as ET
  4. import requests
  5. from yattag import indent
  6.  
  7. url = 'https://mail.hovedkvarteret.no:7071/service/admin/soap'
  8. headers = { 'Content-Type': 'application/soap+xml' }
  9.  
  10. # Get the credentials through zmlocalconfig
  11. # zmlocalconfig zimbra_user
  12. # zmlocalconfig -s zimbra_ldap_password
  13.  
  14. zimbra_user = 'zimbra'
  15. zimbra_password = 'zimbra_ldap_password'
  16.  
  17. token_xml = '<?xml version="1.0" ?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">\
  18. <soap:Header><context xmlns="urn:zimbra"><format type="xml"/></context></soap:Header><soap:Body><AuthRequest xmlns="urn:zimbraAdmin">\
  19. <name>%s</name><password>%s</password></AuthRequest></soap:Body></soap:Envelope>' % (zimbra_user, zimbra_password)
  20.  
  21.  
  22. r = requests.post(url, data=token_xml, headers=headers)
  23.  
  24. # Got the admin token, now you can get the delegated token to act on behalf a specific account
  25. admin_token = ET.fromstring(r.content).find('.//{urn:zimbraAdmin}authToken').text
  26.  
  27. username = 'user3@hovedkvarteret.no'
  28.  
  29. delegated_token_xml = '<?xml version="1.0" ?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">\
  30.    <soap:Header>\
  31.        <context xmlns="urn:zimbra">\
  32.            <authToken>%s</authToken>\
  33.        </context>\
  34.    </soap:Header>\
  35.    <soap:Body>\
  36.        <DelegateAuthRequest duration="86400" xmlns="urn:zimbraAdmin">\
  37.            <account by="name">%s</account>\
  38.        </DelegateAuthRequest>\
  39. </soap:Body>\
  40. </soap:Envelope>' % (admin_token, username)
  41.  
  42. r = requests.post(url, data=delegated_token_xml, headers=headers)
  43.  
  44. #print(r)
  45. delegated_token = ET.fromstring(r.content).find('.//{urn:zimbraAdmin}authToken').text
  46.  
  47. info_request_xml = '<?xml version="1.0" ?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">\
  48.    <soap:Header>\
  49.        <context xmlns="urn:zimbra">\
  50.            <authToken>%s</authToken>\
  51.            <session/><account by="name">%s</account>\
  52.            <userAgent name="zclient" version="8.0.7_GA_6020"/>\
  53.        </context>\
  54.    </soap:Header>\
  55.    <soap:Body>\
  56.        <GetAccountInfoRequest>\
  57.            <account by="id">%s</account>\
  58.        </GetAccountInfoRequest>\
  59.    </soap:Body>\
  60. </soap:Envelope>' % (delegated_token, username, "uuid")
  61.  
  62.  
  63. # print (info_request_xml)
  64. # Now you can start using the main url
  65. main_url = 'https://mail.hovedkvarteret.no/service/soap'
  66.  
  67. r = requests.post(main_url, data=info_request_xml, headers=headers)
  68. print(r.content)
  69.  
  70. # print(getcontacts)
  71. pretty_string = indent(r.content)
  72. print(pretty_string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement