1 # Copyright (C) 2014 Ipsilon Contributors, see COPYING for license
3 from ipsilon.login.common import LoginFormBase, LoginManagerBase
4 from ipsilon.util.plugin import PluginObject
5 from ipsilon.util.log import Log
6 from ipsilon.util import config as pconfig
7 from ipsilon.info.infoldap import InfoProvider as LDAPInfo
11 class LDAP(LoginFormBase, Log):
13 def __init__(self, site, mgr, page):
14 super(LDAP, self).__init__(site, mgr, page)
17 def _ldap_connect(self):
19 tls = self.lm.tls.lower()
22 tls_req_opt = ldap.OPT_X_TLS_NEVER
24 tls_req_opt = ldap.OPT_X_TLS_DEMAND
26 tls_req_opt = ldap.OPT_X_TLS_ALLOW
28 tls_req_opt = ldap.OPT_X_TLS_TRY
29 if tls_req_opt is not None:
30 ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, tls_req_opt)
32 conn = ldap.initialize(self.lm.server_url)
35 if not self.lm.server_url.startswith("ldaps"):
39 def _authenticate(self, username, password):
41 conn = self._ldap_connect()
42 dn = self.lm.bind_dn_tmpl % {'username': username}
43 conn.simple_bind_s(dn, password)
45 # Bypass info plugins to optimize data retrieval
46 if self.lm.get_user_info:
49 if not self.ldap_info:
50 self.ldap_info = LDAPInfo(self._site)
52 return self.ldap_info.get_user_data_from_conn(conn, dn)
56 def POST(self, *args, **kwargs):
57 username = kwargs.get("login_name")
58 password = kwargs.get("login_password")
63 if username and password:
65 userattrs = self._authenticate(username, password)
67 except Exception, e: # pylint: disable=broad-except
68 errmsg = "Authentication failed"
69 self.error("Exception raised: [%s]" % repr(e))
71 errmsg = "Username or password is missing"
75 return self.lm.auth_successful(self.trans, username, 'password',
78 context = self.create_tmpl_context(
81 error_password=not password,
82 error_username=not username
84 # pylint: disable=star-args
85 return self._template('login/form.html', **context)
88 class LoginManager(LoginManagerBase):
90 def __init__(self, *args, **kwargs):
91 super(LoginManager, self).__init__(*args, **kwargs)
96 self.service_name = 'ldap'
97 self.description = """
98 Form based login Manager that uses a simple bind LDAP operation to perform
104 'The LDAP server url.',
105 'ldap://example.com'),
108 'Template to turn username into DN.',
109 'uid=%(username)s,ou=People,dc=example,dc=com'),
112 'Get user info via ldap using user credentials',
116 'What TLS level show be required',
117 ['Demand', 'Allow', 'Try', 'Never', 'NoTLS'],
121 'Text used to ask for the username at login time.',
125 'Text used to ask for the password at login time.',
129 'Text used to guide the user at login time.',
130 'Provide your Username and Password')
135 return self.get_config_value('help text')
138 def username_text(self):
139 return self.get_config_value('username text')
142 def password_text(self):
143 return self.get_config_value('password text')
146 def server_url(self):
147 return self.get_config_value('server url')
151 return self.get_config_value('tls')
154 def get_user_info(self):
155 return self.get_config_value('get user info')
158 def bind_dn_tmpl(self):
159 return self.get_config_value('bind dn template')
161 def get_tree(self, site):
162 self.page = LDAP(site, self, 'login/ldap')
166 class Installer(object):
168 def __init__(self, *pargs):
173 def install_args(self, group):
174 group.add_argument('--ldap', choices=['yes', 'no'], default='no',
175 help='Configure LDAP authentication')
176 group.add_argument('--ldap-server-url', action='store',
177 help='LDAP Server Url')
178 group.add_argument('--ldap-bind-dn-template', action='store',
179 help='LDAP Bind DN Template')
181 def configure(self, opts):
182 if opts['ldap'] != 'yes':
185 # Add configuration data to database
186 po = PluginObject(*self.pargs)
189 po.wipe_config_values()
192 if 'ldap_server_url' in opts:
193 config['server url'] = opts['ldap_server_url']
194 if 'ldap_bind_dn_template' in opts:
195 config['bind dn template'] = opts['ldap_bind_dn_template']
196 config['tls'] = 'Demand'
197 po.save_plugin_config(config)
199 # Update global config to add login plugin
201 po.save_enabled_state()