diff options
author | Rebecka Gulliksson <rebecka.gulliksson@umu.se> | 2015-12-08 11:19:12 +0100 |
---|---|---|
committer | Rebecka Gulliksson <rebecka.gulliksson@umu.se> | 2015-12-08 11:19:12 +0100 |
commit | 90fb449ce8ffca3e218bd1123757d89286b3b037 (patch) | |
tree | b143998ae44b8c92a9a4d866f00129d98be214f7 /src/saml2/authn.py | |
parent | 0b1da5ad7e0ac5fe72d04f986361200b81762cdc (diff) | |
download | pysaml2-90fb449ce8ffca3e218bd1123757d89286b3b037.tar.gz |
Only define LDAP authn support if the library can be imported.
Diffstat (limited to 'src/saml2/authn.py')
-rw-r--r-- | src/saml2/authn.py | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/src/saml2/authn.py b/src/saml2/authn.py index 1c3006d8..1f2d02cf 100644 --- a/src/saml2/authn.py +++ b/src/saml2/authn.py @@ -1,7 +1,6 @@ import logging import six import time -import ldap from saml2 import SAMLError from saml2.aes import AESCipher from saml2.httputil import Response @@ -231,33 +230,38 @@ class AuthnMethodChooser(object): else: pass # TODO - -class LDAPAuthn(UsernamePasswordMako): - def __init__(self, srv, ldapsrv, return_to, - dn_pattern, mako_template, template_lookup): - """ - :param srv: The server instance - :param ldapsrv: Which LDAP server to us - :param return_to: Where to send the user after authentication - :return: - """ - UsernamePasswordMako.__init__(self, srv, mako_template, template_lookup, - None, return_to) - - self.ldap = ldap.initialize(ldapsrv) - self.ldap.protocol_version = 3 - self.ldap.set_option(ldap.OPT_REFERRALS, 0) - self.dn_pattern = dn_pattern - - def _verify(self, pwd, user): - """ - Verifies the username and password agains a LDAP server - :param pwd: The password - :param user: The username - :return: AssertionError if the LDAP verification failed. - """ - _dn = self.dn_pattern % user - try: - self.ldap.simple_bind_s(_dn, pwd) - except Exception: - raise AssertionError() +try: + import ldap + + class LDAPAuthn(UsernamePasswordMako): + def __init__(self, srv, ldapsrv, return_to, + dn_pattern, mako_template, template_lookup): + """ + :param srv: The server instance + :param ldapsrv: Which LDAP server to us + :param return_to: Where to send the user after authentication + :return: + """ + UsernamePasswordMako.__init__(self, srv, mako_template, template_lookup, + None, return_to) + + self.ldap = ldap.initialize(ldapsrv) + self.ldap.protocol_version = 3 + self.ldap.set_option(ldap.OPT_REFERRALS, 0) + self.dn_pattern = dn_pattern + + def _verify(self, pwd, user): + """ + Verifies the username and password agains a LDAP server + :param pwd: The password + :param user: The username + :return: AssertionError if the LDAP verification failed. + """ + _dn = self.dn_pattern % user + try: + self.ldap.simple_bind_s(_dn, pwd) + except Exception: + raise AssertionError() +except ImportError: + class LDAPAuthn(UserAuthnMethod): + pass |