Commit | Line | Data |
---|---|---|
daf29c01 RE |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. | |
3 | # | |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | import ldap | |
17 | import logging | |
18 | ||
19 | from mediagoblin import mg_globals | |
daf29c01 RE |
20 | |
21 | _log = logging.getLogger(__name__) | |
22 | ||
23 | ||
24 | class LDAP(object): | |
c4513740 | 25 | def __init__(self): |
daf29c01 | 26 | self.ldap_settings = mg_globals.global_config['plugins']['mediagoblin.plugins.ldap'] |
daf29c01 RE |
27 | |
28 | def _connect(self, server): | |
11782c00 RE |
29 | _log.info('Connecting to {0}.'.format(server['LDAP_SERVER_URI'])) |
30 | self.conn = ldap.initialize(server['LDAP_SERVER_URI']) | |
31 | ||
32 | if server['LDAP_START_TLS'] == 'true': | |
33 | _log.info('Initiating TLS') | |
34 | self.conn.start_tls_s() | |
daf29c01 RE |
35 | |
36 | def login(self, username, password): | |
37 | for k, v in self.ldap_settings.iteritems(): | |
38 | try: | |
daf29c01 | 39 | self._connect(v) |
11782c00 | 40 | user_dn = v['LDAP_USER_DN_TEMPLATE'].format(username=username) |
daf29c01 | 41 | self.conn.simple_bind_s(user_dn, password.encode('utf8')) |
c4513740 | 42 | return username |
daf29c01 RE |
43 | |
44 | except ldap.LDAPError, e: | |
45 | _log.info(e) | |
46 | ||
11782c00 RE |
47 | finally: |
48 | _log.info('Unbinding {0}.').format(v['LDAP_SERVER_URI']) | |
49 | self.conn.unbind() | |
50 | ||
c4513740 | 51 | return False |