Changeset View
Standalone View
pykolab/auth/__init__.py
Show All 22 Lines | |||||
import pykolab | import pykolab | ||||
import pykolab.base | import pykolab.base | ||||
from pykolab.translate import _ | from pykolab.translate import _ | ||||
log = pykolab.getLogger('pykolab.auth') | log = pykolab.getLogger('pykolab.auth') | ||||
conf = pykolab.getConf() | conf = pykolab.getConf() | ||||
class Auth(pykolab.base.Base): | class Auth(pykolab.base.Base): | ||||
Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
""" | """ | ||||
This is the Authentication and Authorization module for PyKolab. | This is the Authentication and Authorization module for PyKolab. | ||||
""" | """ | ||||
def __init__(self, domain=None): | def __init__(self, domain=None): | ||||
""" | """ | ||||
Initialize the authentication class. | Initialize the authentication class. | ||||
""" | """ | ||||
Show All 12 Lines | def authenticate(self, login): | ||||
if len(login) == 3: | if len(login) == 3: | ||||
# The realm has not been specified. See if we know whether or not | # The realm has not been specified. See if we know whether or not | ||||
# to use virtual_domains, as this may be a cause for the realm not | # to use virtual_domains, as this may be a cause for the realm not | ||||
# having been specified separately. | # having been specified separately. | ||||
use_virtual_domains = conf.get('imap', 'virtual_domains') | use_virtual_domains = conf.get('imap', 'virtual_domains') | ||||
# TODO: Insert debug statements | # TODO: Insert debug statements | ||||
#if use_virtual_domains == "userid": | #if use_virtual_domains == "userid": | ||||
Lint: PEP8 E265 block comment should start with '# ' Lint: PEP8 E265: block comment should start with '# ' | |||||
#print "# Derive domain from login[0]" | #print "# Derive domain from login[0]" | ||||
Lint: PEP8 E116 unexpected indentation (comment) Lint: PEP8 E116: unexpected indentation (comment) | |||||
Lint: PEP8 E265 block comment should start with '# ' Lint: PEP8 E265: block comment should start with '# ' | |||||
#elif not use_virtual_domains: | #elif not use_virtual_domains: | ||||
Lint: PEP8 E265 block comment should start with '# ' Lint: PEP8 E265: block comment should start with '# ' | |||||
#print "# Explicitly do not user virtual domains??" | #print "# Explicitly do not user virtual domains??" | ||||
Lint: PEP8 E116 unexpected indentation (comment) Lint: PEP8 E116: unexpected indentation (comment) | |||||
Lint: PEP8 E265 block comment should start with '# ' Lint: PEP8 E265: block comment should start with '# ' | |||||
#else: | #else: | ||||
Lint: PEP8 E265 block comment should start with '# ' Lint: PEP8 E265: block comment should start with '# ' | |||||
## Do use virtual domains, derive domain from login[0] | ## Do use virtual domains, derive domain from login[0] | ||||
Lint: PEP8 E116 unexpected indentation (comment) Lint: PEP8 E116: unexpected indentation (comment) | |||||
Lint: PEP8 E266 too many leading '#' for block comment Lint: PEP8 E266: too many leading '#' for block comment | |||||
#print "# Derive domain from login[0]" | #print "# Derive domain from login[0]" | ||||
Lint: PEP8 E116 unexpected indentation (comment) Lint: PEP8 E116: unexpected indentation (comment) | |||||
Lint: PEP8 E265 block comment should start with '# ' Lint: PEP8 E265: block comment should start with '# ' | |||||
if len(login[0].split('@')) > 1: | if len(login[0].split('@')) > 1: | ||||
domain = login[0].split('@')[1] | domain = login[0].split('@')[1] | ||||
elif len(login) >= 4: | elif len(login) >= 4: | ||||
domain = login[3] | domain = login[3] | ||||
else: | else: | ||||
domain = conf.get("kolab", "primary_domain") | domain = conf.get("kolab", "primary_domain") | ||||
# realm overrides domain | # realm overrides domain | ||||
if len(login) == 4: | if len(login) == 4: | ||||
domain = login[3] | domain = login[3] | ||||
retval = self._auth.authenticate(login, domain) | retval = self._auth.authenticate(login, domain) | ||||
return retval | return retval | ||||
def connect(self, domain=None): | def connect(self, domain=None): | ||||
""" | """ | ||||
Connect to the domain authentication backend using domain, or fall | Connect to the domain authentication backend using domain, or fall | ||||
back to the primary domain specified by the configuration. | back to the primary domain specified by the configuration. | ||||
""" | """ | ||||
log.debug(_("Called for domain %r") % (domain), level=9) | log.debug(_("Called for domain %r") % (domain), level=9) | ||||
if not self._auth == None: | if not self._auth == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
return | return | ||||
if domain == None: | if domain == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
if not self.domain == None: | if not self.domain == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
section = self.domain | section = self.domain | ||||
domain = self.domain | domain = self.domain | ||||
else: | else: | ||||
section = 'kolab' | section = 'kolab' | ||||
domain = conf.get('kolab', 'primary_domain') | domain = conf.get('kolab', 'primary_domain') | ||||
else: | else: | ||||
self.list_domains(domain) | self.list_domains(domain) | ||||
section = domain | section = domain | ||||
log.debug( | log.debug( | ||||
_("Using section %s and domain %s") % (section,domain), | _("Using section %s and domain %s") % (section,domain), | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
level=9 | level=9 | ||||
) | ) | ||||
if not self.domains == None and self.domains.has_key(domain): | if not self.domains == None and self.domains.has_key(domain): | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
section = self.domains[domain] | section = self.domains[domain] | ||||
domain = self.domains[domain] | domain = self.domains[domain] | ||||
log.debug( | log.debug( | ||||
_("Using section %s and domain %s") % (section,domain), | _("Using section %s and domain %s") % (section,domain), | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
level=9 | level=9 | ||||
) | ) | ||||
log.debug( | log.debug( | ||||
_("Connecting to Authentication backend for domain %s") % ( | _("Connecting to Authentication backend for domain %s") % ( | ||||
domain | domain | ||||
), | ), | ||||
level=8 | level=8 | ||||
) | ) | ||||
if not conf.has_section(section): | if not conf.has_section(section): | ||||
section = 'kolab' | section = 'kolab' | ||||
if not conf.has_option(section, 'auth_mechanism'): | if not conf.has_option(section, 'auth_mechanism'): | ||||
log.debug( | log.debug( | ||||
_("Section %s has no option 'auth_mechanism'") % (section), | _("Section %s has no option 'auth_mechanism'") % (section), | ||||
level=9 | level=9 | ||||
) | ) | ||||
section = 'kolab' | section = 'kolab' | ||||
else: | else: | ||||
log.debug( | log.debug( | ||||
_("Section %s has auth_mechanism: %r") % ( | _("Section %s has auth_mechanism: %r") % ( | ||||
section, | section, | ||||
conf.get(section,'auth_mechanism') | conf.get(section,'auth_mechanism') | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
), | ), | ||||
level=9 | level=9 | ||||
) | ) | ||||
# Get the actual authentication and authorization backend. | # Get the actual authentication and authorization backend. | ||||
if conf.get(section, 'auth_mechanism') == 'ldap': | if conf.get(section, 'auth_mechanism') == 'ldap': | ||||
log.debug(_("Starting LDAP..."), level=9) | log.debug(_("Starting LDAP..."), level=9) | ||||
from pykolab.auth import ldap | from pykolab.auth import ldap | ||||
Show All 11 Lines | def connect(self, domain=None): | ||||
self._auth.connect() | self._auth.connect() | ||||
def disconnect(self, domain=None): | def disconnect(self, domain=None): | ||||
""" | """ | ||||
Connect to the domain authentication backend using domain, or fall | Connect to the domain authentication backend using domain, or fall | ||||
back to the primary domain specified by the configuration. | back to the primary domain specified by the configuration. | ||||
""" | """ | ||||
if domain == None: | if domain == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
section = 'kolab' | section = 'kolab' | ||||
domain = conf.get('kolab', 'primary_domain') | domain = conf.get('kolab', 'primary_domain') | ||||
else: | else: | ||||
section = domain | section = domain | ||||
if not self._auth or self._auth == None: | if not self._auth or self._auth == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
return | return | ||||
self._auth._disconnect() | self._auth._disconnect() | ||||
del self._auth | del self._auth | ||||
self._auth = None | self._auth = None | ||||
def find_folder_resource(self, folder): | |||||
""" | |||||
Find one or more resources corresponding to the shared folder name. | |||||
""" | |||||
if not self._auth or self._auth == None: | |||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
self.connect() | |||||
result = self._auth.find_folder_resource(folder) | |||||
if isinstance(result, list) and len(result) == 1: | |||||
return result[0] | |||||
else: | |||||
return result | |||||
def find_recipient(self, address, domain=None): | def find_recipient(self, address, domain=None): | ||||
""" | """ | ||||
Find one or more entries corresponding to the recipient address. | Find one or more entries corresponding to the recipient address. | ||||
""" | """ | ||||
if not domain == None and not self.domain == domain: | if not domain == None and not self.domain == domain: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
self.connect(domain=domain) | self.connect(domain=domain) | ||||
if not self._auth or self._auth == None: | if not self._auth or self._auth == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
self.connect(domain=domain) | self.connect(domain=domain) | ||||
result = self._auth.find_recipient(address) | result = self._auth.find_recipient(address) | ||||
if isinstance(result, list) and len(result) == 1: | if isinstance(result, list) and len(result) == 1: | ||||
return result[0] | return result[0] | ||||
else: | else: | ||||
return result | return result | ||||
def find_resource(self, address): | def find_resource(self, address): | ||||
""" | """ | ||||
Find one or more resources corresponding to the recipient address. | Find one or more resources corresponding to the recipient address. | ||||
""" | """ | ||||
if not self._auth or self._auth == None: | if not self._auth or self._auth == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
self.connect() | self.connect() | ||||
result = self._auth.find_resource(address) | result = self._auth.find_resource(address) | ||||
if isinstance(result, list) and len(result) == 1: | if isinstance(result, list) and len(result) == 1: | ||||
return result[0] | return result[0] | ||||
else: | else: | ||||
return result | return result | ||||
def find_user(self, attr, value, **kw): | def find_user(self, attr, value, **kw): | ||||
return self._auth.search_entry_by_attribute(attr, value, **kw) | return self._auth.search_entry_by_attribute(attr, value, **kw) | ||||
def find_user_dn(self, login, kolabuser=False): | def find_user_dn(self, login, kolabuser=False): | ||||
return self._auth._find_user_dn(login, kolabuser); | return self._auth._find_user_dn(login, kolabuser); | ||||
Lint: PEP8 E703 statement ends with a semicolon Lint: PEP8 E703: statement ends with a semicolon | |||||
def list_recipient_addresses(self, user): | def list_recipient_addresses(self, user): | ||||
return self._auth.list_recipient_addresses(user) | return self._auth.list_recipient_addresses(user) | ||||
def extract_recipient_addresses(self, entry): | def extract_recipient_addresses(self, entry): | ||||
return self._auth.extract_recipient_addresses(entry) | return self._auth.extract_recipient_addresses(entry) | ||||
def list_delegators(self, user): | def list_delegators(self, user): | ||||
Show All 12 Lines | def list_domains(self, domain=None): | ||||
""" | """ | ||||
# Connect to the global namespace | # Connect to the global namespace | ||||
self.connect() | self.connect() | ||||
# Find the domains in the authentication backend. | # Find the domains in the authentication backend. | ||||
kolab_primary_domain = conf.get('kolab', 'primary_domain') | kolab_primary_domain = conf.get('kolab', 'primary_domain') | ||||
if self.domains == None: | if self.domains == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
try: | try: | ||||
domains = self._auth._list_domains(domain) | domains = self._auth._list_domains(domain) | ||||
except: | except: | ||||
if not self.domain == kolab_primary_domain: | if not self.domain == kolab_primary_domain: | ||||
return { self.domain: self.domain } | return { self.domain: self.domain } | ||||
Lint: PEP8 E201 whitespace after '{' Lint: PEP8 E201: whitespace after '{' | |||||
Lint: PEP8 E202 whitespace before '}' Lint: PEP8 E202: whitespace before '}' | |||||
else: | else: | ||||
domains = {} | domains = {} | ||||
# If no domains are found, the primary domain is used. | # If no domains are found, the primary domain is used. | ||||
if len(domains) < 1: | if len(domains) < 1: | ||||
self.domains = { kolab_primary_domain: kolab_primary_domain } | self.domains = { kolab_primary_domain: kolab_primary_domain } | ||||
Lint: PEP8 E201 whitespace after '{' Lint: PEP8 E201: whitespace after '{' | |||||
Lint: PEP8 E202 whitespace before '}' Lint: PEP8 E202: whitespace before '}' | |||||
else: | else: | ||||
self.domains = {} | self.domains = {} | ||||
for primary, secondaries in domains: | for primary, secondaries in domains: | ||||
self.domains[primary.lower()] = primary.lower() | self.domains[primary.lower()] = primary.lower() | ||||
for secondary in secondaries: | for secondary in secondaries: | ||||
self.domains[secondary.lower()] = primary.lower() | self.domains[secondary.lower()] = primary.lower() | ||||
return self.domains | return self.domains | ||||
Show All 39 Lines |
expected 2 blank lines, found 1