Skip to content

Commit 90fb449

Browse files
author
Rebecka Gulliksson
committed
Only define LDAP authn support if the library can be imported.
1 parent 0b1da5a commit 90fb449

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

src/saml2/authn.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
import six
33
import time
4-
import ldap
54
from saml2 import SAMLError
65
from saml2.aes import AESCipher
76
from saml2.httputil import Response
@@ -231,33 +230,38 @@ def __call__(self, **kwargs):
231230
else:
232231
pass # TODO
233232

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

Comments
 (0)