_log.info('Initiating TLS')
self.conn.start_tls_s()
+ def _get_email(self, server, username):
+ results = self.conn.search_s(server['LDAP_SEARCH_BASE'],
+ ldap.SCOPE_SUBTREE, 'uid={0}'
+ .format(username),
+ [server['EMAIL_SEARCH_FIELD']])
+
+ try:
+ email = results[0][1][server['EMAIL_SEARCH_FIELD']][0]
+ except KeyError:
+ email = None
+
+ return email
+
def login(self, username, password):
for k, v in self.ldap_settings.iteritems():
try:
self._connect(v)
user_dn = v['LDAP_USER_DN_TEMPLATE'].format(username=username)
self.conn.simple_bind_s(user_dn, password.encode('utf8'))
- return username
+ email = self._get_email(v, username)
+ return username, email
except ldap.LDAPError, e:
_log.info(e)
finally:
- _log.info('Unbinding {0}.').format(v['LDAP_SERVER_URI'])
+ _log.info('Unbinding {0}.'.format(v['LDAP_SERVER_URI']))
self.conn.unbind()
- return False
+ return False, None
if request.method == 'POST' and login_form.validate():
l = LDAP()
- username = l.login(login_form.username.data, login_form.password.data)
+ username, email = l.login(login_form.username.data,
+ login_form.password.data)
if username:
user = User.query.filter_by(
'instance.'))
return redirect(request, 'index')
- register_form = forms.RegisterForm(request.form,
- username=username)
+ register_form = forms.RegisterForm(username=username,
+ email=email)
return render_to_response(
request,