|
1 | 1 | import logging
|
2 | 2 | import six
|
3 | 3 | import time
|
4 |
| -import ldap |
5 | 4 | from saml2 import SAMLError
|
6 | 5 | from saml2.aes import AESCipher
|
7 | 6 | from saml2.httputil import Response
|
@@ -231,33 +230,38 @@ def __call__(self, **kwargs):
|
231 | 230 | else:
|
232 | 231 | pass # TODO
|
233 | 232 |
|
234 |
| - |
235 |
| -class LDAPAuthn(UsernamePasswordMako): |
236 |
| - def __init__(self, srv, ldapsrv, return_to, |
237 |
| - dn_pattern, mako_template, template_lookup): |
238 |
| - """ |
239 |
| - :param srv: The server instance |
240 |
| - :param ldapsrv: Which LDAP server to us |
241 |
| - :param return_to: Where to send the user after authentication |
242 |
| - :return: |
243 |
| - """ |
244 |
| - UsernamePasswordMako.__init__(self, srv, mako_template, template_lookup, |
245 |
| - None, return_to) |
246 |
| - |
247 |
| - self.ldap = ldap.initialize(ldapsrv) |
248 |
| - self.ldap.protocol_version = 3 |
249 |
| - self.ldap.set_option(ldap.OPT_REFERRALS, 0) |
250 |
| - self.dn_pattern = dn_pattern |
251 |
| - |
252 |
| - def _verify(self, pwd, user): |
253 |
| - """ |
254 |
| - Verifies the username and password agains a LDAP server |
255 |
| - :param pwd: The password |
256 |
| - :param user: The username |
257 |
| - :return: AssertionError if the LDAP verification failed. |
258 |
| - """ |
259 |
| - _dn = self.dn_pattern % user |
260 |
| - try: |
261 |
| - self.ldap.simple_bind_s(_dn, pwd) |
262 |
| - except Exception: |
263 |
| - raise AssertionError() |
| 233 | +try: |
| 234 | + import ldap |
| 235 | + |
| 236 | + class LDAPAuthn(UsernamePasswordMako): |
| 237 | + def __init__(self, srv, ldapsrv, return_to, |
| 238 | + dn_pattern, mako_template, template_lookup): |
| 239 | + """ |
| 240 | + :param srv: The server instance |
| 241 | + :param ldapsrv: Which LDAP server to us |
| 242 | + :param return_to: Where to send the user after authentication |
| 243 | + :return: |
| 244 | + """ |
| 245 | + UsernamePasswordMako.__init__(self, srv, mako_template, template_lookup, |
| 246 | + None, return_to) |
| 247 | + |
| 248 | + self.ldap = ldap.initialize(ldapsrv) |
| 249 | + self.ldap.protocol_version = 3 |
| 250 | + self.ldap.set_option(ldap.OPT_REFERRALS, 0) |
| 251 | + self.dn_pattern = dn_pattern |
| 252 | + |
| 253 | + def _verify(self, pwd, user): |
| 254 | + """ |
| 255 | + Verifies the username and password agains a LDAP server |
| 256 | + :param pwd: The password |
| 257 | + :param user: The username |
| 258 | + :return: AssertionError if the LDAP verification failed. |
| 259 | + """ |
| 260 | + _dn = self.dn_pattern % user |
| 261 | + try: |
| 262 | + self.ldap.simple_bind_s(_dn, pwd) |
| 263 | + except Exception: |
| 264 | + raise AssertionError() |
| 265 | +except ImportError: |
| 266 | + class LDAPAuthn(UserAuthnMethod): |
| 267 | + pass |
0 commit comments