Skip to content

Commit

Permalink
ticket:21 administrative auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgarm committed Jul 9, 2010
1 parent 2a3738d commit 75750ea
Show file tree
Hide file tree
Showing 22 changed files with 421 additions and 136 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ ticket:31 - add proxy support
ticket:27 - error and exception handling
ticket:33 - switch to SOAPpy
ticket:20 - zimbra client preauthentication
ticket:21 - zimbra client administrative authentication
56 changes: 56 additions & 0 deletions client/sample/admin_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
"""
################################################################################
# Copyright (c) 2010, Ilgar Mashayev
#
# E-mail: [email protected]
# Website: http://github.com/ilgarm/pyzimbra
################################################################################
# This file is part of pyzimbra.
#
# Pyzimbra is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyzimbra is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyzimbra. If not, see <http://www.gnu.org/licenses/>.
################################################################################
Authentication samples.
@author: ilgar
"""
from pyzimbra import soap
from pyzimbra.base import ZimbraClientException
from pyzimbra.soap_auth import SoapAuthenticator
from pyzimbra.soap_transport import SoapTransport
from sample.util import load_properties
import pconstant


def authenticate():
p = load_properties()

transport = SoapTransport()
transport.debug = 1
transport.soap_url = soap.admin_soap_url(p[pconstant.ADMIN_HOSTNAME])

auth = SoapAuthenticator()
auth_token = auth.authenticate_admin(transport,
p[pconstant.ADMIN_ACCOUNT_NAME],
p[pconstant.ADMIN_PASSWORD])

print auth_token.token


if __name__ == '__main__':
try:
authenticate()
except ZimbraClientException, e:
e.print_trace()
10 changes: 6 additions & 4 deletions client/sample/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@
@author: ilgar
"""
from pyzimbra import pconstant
from pyzimbra import soap
from pyzimbra.base import ZimbraClientException
from pyzimbra.soap_auth import SoapAuthenticator
from pyzimbra.soap_transport import SoapTransport
from sample.util import load_properties
import pconstant


def authenticate():
p = load_properties()

transport = SoapTransport()
transport.debug = 1
transport.domains = p[pconstant.DOMAINS]
transport.soap_url = soap.soap_url(p[pconstant.HOSTNAME])

auth = SoapAuthenticator()
auth_token = auth.authenticate(transport,
Expand All @@ -53,10 +54,11 @@ def pre_authenticate():

transport = SoapTransport()
transport.debug = 1
transport.domains = p[pconstant.DOMAINS]
transport.soap_url = soap.soap_url(p[pconstant.HOSTNAME])

auth = SoapAuthenticator()
auth_token = auth.authenticate(transport, pconstant.ACCOUNT_NAME)
auth.domains = p[pconstant.DOMAINS]
auth_token = auth.authenticate(transport, p[pconstant.ACCOUNT_NAME])

print auth_token.token

Expand Down
66 changes: 66 additions & 0 deletions client/sample/get_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
"""
################################################################################
# Copyright (c) 2010, Ilgar Mashayev
#
# E-mail: [email protected]
# Website: http://github.com/ilgarm/pyzimbra
################################################################################
# This file is part of pyzimbra.
#
# Pyzimbra is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyzimbra is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyzimbra. If not, see <http://www.gnu.org/licenses/>.
################################################################################
Account info samples.
@author: ilgar
"""
from pyzimbra import sconstant, zconstant, soap
from pyzimbra.soap_auth import SoapAuthenticator
from pyzimbra.soap_transport import SoapTransport
from pyzimbra.zclient import ZimbraClient
from sample.util import load_properties
import SOAPpy
import pconstant


def get_account():
p = load_properties()

transport = SoapTransport()
transport.debug = 1
transport.soap_url = soap.admin_soap_url(p[pconstant.ADMIN_HOSTNAME])

auth = SoapAuthenticator()

zclient = ZimbraClient()
zclient.transport = transport

zclient.authenticate_admin(auth,
p[pconstant.ADMIN_ACCOUNT_NAME],
p[pconstant.ADMIN_PASSWORD])

attrs = {sconstant.A_BY: sconstant.V_NAME}
account = SOAPpy.Types.stringType(data=p[pconstant.ACCOUNT_NAME], attrs=attrs)

params = {sconstant.E_ACCOUNT: account}
res = zclient.invoke(zconstant.NS_ZIMBRA_ADMIN_URL,
sconstant.GetAccountRequest,
params)

print res


if __name__ == '__main__':
get_account()
5 changes: 3 additions & 2 deletions client/sample/get_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@
@author: ilgar
"""
from pyzimbra import sconstant, zconstant, pconstant
from pyzimbra import sconstant, zconstant, soap
from pyzimbra.soap_auth import SoapAuthenticator
from pyzimbra.soap_transport import SoapTransport
from pyzimbra.zclient import ZimbraClient
from sample.util import load_properties
import pconstant


def get_info():
p = load_properties()

transport = SoapTransport()
transport.debug = 1
transport.domains = p[pconstant.DOMAINS]
transport.soap_url = soap.soap_url(p[pconstant.HOSTNAME])

auth = SoapAuthenticator()

Expand Down
5 changes: 3 additions & 2 deletions client/sample/proxied_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@
@author: ilgar
"""
from pyzimbra import soap, sconstant, zconstant, pconstant
from pyzimbra import soap, sconstant, zconstant
from pyzimbra.soap_auth import SoapAuthenticator
from pyzimbra.soap_transport import SoapTransport
from pyzimbra.zclient import ZimbraClient
from sample.util import load_properties
import pconstant


def get_proxied_info():
p = load_properties()

transport = SoapTransport()
transport.debug = 1
transport.domains = p[pconstant.DOMAINS]
transport.soap_url = soap.soap_url(p[pconstant.HOSTNAME])
transport.proxy_url = soap.proxy_url(p[pconstant.PROXY_HOSTNAME],
p[pconstant.PROXY_USERNAME],
p[pconstant.PROXY_PASSWORD],
Expand Down
12 changes: 7 additions & 5 deletions client/sample/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@author: ilgar
"""
from ConfigParser import ConfigParser
from pyzimbra import pconstant
import pconstant


def load_properties():
Expand All @@ -36,16 +36,18 @@ def load_properties():
cfg.read(properties)

p = {}
p[pconstant.ADMIN_HOSTNAME] = cfg.get(pconstant.ADMIN, pconstant.HOST)
p[pconstant.ADMIN_ACCOUNT_NAME] = cfg.get(pconstant.ADMIN, pconstant.USERNAME)
p[pconstant.ADMIN_PASSWORD] = cfg.get(pconstant.ADMIN, pconstant.PASSWORD)

p[pconstant.DOMAIN] = cfg.get(pconstant.DOMAIN, pconstant.NAME)
p[pconstant.HOSTNAME] = cfg.get(pconstant.DOMAIN, pconstant.HOST)
p[pconstant.DOMAIN_KEY] = cfg.get(pconstant.DOMAIN, pconstant.KEY)
p[pconstant.DOMAINS] = {p[pconstant.DOMAIN]:
{pconstant.HOSTNAME: p[pconstant.HOSTNAME],
pconstant.KEY: p[pconstant.DOMAIN_KEY]}}
cfg.get(pconstant.DOMAIN, pconstant.KEY)}

p[pconstant.PROXY_SCHEME] = cfg.get(pconstant.PROXY, pconstant.SCHEME)
p[pconstant.PROXY_HOSTNAME] = cfg.get(pconstant.PROXY, pconstant.HOST)
p[pconstant.PROXY_PORT] = cfg.get(pconstant.PROXY, pconstant.SCHEME)
p[pconstant.PROXY_PORT] = cfg.get(pconstant.PROXY, pconstant.PORT)
p[pconstant.PROXY_USERNAME] = cfg.get(pconstant.PROXY, pconstant.USERNAME)
p[pconstant.PROXY_PASSWORD] = cfg.get(pconstant.PROXY, pconstant.PASSWORD)

Expand Down
15 changes: 14 additions & 1 deletion src/pyzimbra/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,25 @@ class Authenticator(object):
__metaclass__ = abc.ABCMeta

# --------------------------------------------------------------- properties
domains = property(lambda self: self._domains,
lambda self, v: setattr(self, '_domains', v))

# -------------------------------------------------------------------- bound
def __init__(self):
pass
self.domains = {}

# ------------------------------------------------------------------ unbound
@abc.abstractmethod
def authenticate_admin(self, transport, account_name, password):
"""
Authenticates administrator using username and password.
@param transport: transport to use for method calls
@param account_name: account name
@param password: account password
@return: AuthToken if authentication succeeded
@raise AuthException: if authentication fails
"""

@abc.abstractmethod
def authenticate(self, transport, account_name, password):
"""
Expand Down
6 changes: 3 additions & 3 deletions src/pyzimbra/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ class ZimbraClientTransport(object):
__metaclass__ = abc.ABCMeta

# --------------------------------------------------------------- properties
domains = property(lambda self: self._domains,
lambda self, v: setattr(self, '_domains', v))
soap_url = property(lambda self: self._soap_url,
lambda self, v: setattr(self, '_soap_url', v))
proxy_url = property(lambda self: self._proxy_url,
lambda self, v: setattr(self, '_proxy_url', v))
debug = property(lambda self: self._debug,
lambda self, v: setattr(self, '_debug', v))

# -------------------------------------------------------------------- bound
def __init__(self):
self.domains = {}
self.soap_url = None
self.proxy_url = None
self.debug = 0

Expand Down
3 changes: 3 additions & 0 deletions src/pyzimbra/sconstant.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
GetInfoRequest = 'GetInfoRequest'
GetInfoResponse = 'GetInfoResponse'

GetAccountRequest = 'GetAccountRequest'
GetAccountResponse = 'GetAccountResponse'

E_CODE = 'Code'
E_TRACE = 'Trace'

Expand Down
17 changes: 14 additions & 3 deletions src/pyzimbra/soap.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,24 @@
import urllib2


def admin_soap_url(hostname):
"""
@return: absolute zimbra administrative soap endpoint url
@raise SoapException: if hostname is empty
"""
if util.empty(hostname):
raise SoapException('Empty hostname')

return 'https://%s%s' % (hostname, zconstant.SOAP_ADMIN_URL)


def soap_url(hostname):
"""
@return: absolute zimbra soap endpoint url
@raise SoapException: if hostname is empty
"""
if util.empty(hostname):
raise SoapException('Empty hostname')
raise SoapException('Empty hostname')

return 'http://%s%s' % (hostname, zconstant.SOAP_URL)

Expand All @@ -48,10 +59,10 @@ def proxy_url(hostname, username=None, password=None, port=0, scheme='http'):
@raise SoapException: if scheme or hostname is empty
"""
if util.empty(hostname):
raise SoapException('Empty hostname')
raise SoapException('Empty hostname')

if util.empty(scheme):
raise SoapException('Empty scheme')
raise SoapException('Empty scheme')

hostport = hostname
if port > 0 and (scheme != 'http' or port != 80):
Expand Down
Loading

0 comments on commit 75750ea

Please sign in to comment.