Commit 447399e8 authored by Michael Kropat's avatar Michael Kropat

Update mail tool to pass api key auth

parent 067052d4
...@@ -3,7 +3,11 @@ ...@@ -3,7 +3,11 @@
import sys, getpass, urllib.request, urllib.error import sys, getpass, urllib.request, urllib.error
def mgmt(cmd, data=None): def mgmt(cmd, data=None):
req = urllib.request.Request('http://localhost:10222' + cmd, urllib.parse.urlencode(data).encode("utf8") if data else None) mgmt_uri = 'http://localhost:10222'
setup_key_auth(mgmt_uri)
req = urllib.request.Request(mgmt_uri + cmd, urllib.parse.urlencode(data).encode("utf8") if data else None)
try: try:
response = urllib.request.urlopen(req) response = urllib.request.urlopen(req)
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e:
...@@ -20,6 +24,18 @@ def read_password(): ...@@ -20,6 +24,18 @@ def read_password():
second = getpass.getpass(' (again): ') second = getpass.getpass(' (again): ')
return first return first
def setup_key_auth(mgmt_uri):
key = open('/var/lib/mailinabox/api.key').read().strip()
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(
realm='Mail-in-a-Box Management Server',
uri=mgmt_uri,
user=key,
passwd='')
opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: ") print("Usage: ")
print(" tools/mail.py user (lists users)") print(" tools/mail.py user (lists users)")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment