python3 ldap认证
python3 ldap认证
#! /usr/bin/python # -*- coding:utf-8 -*- # Author: panb import logging from ldap3 import Server, Connection, ALL logger = logging.getLogger("oauth") LDAP = { "server": "172.27.27.220", "port": 389, "use_ssl": False, "domain": "jcici.com", "base": "ou=People,dc=jcici,dc=com" } class LdapAdmin(object): def __init__(self): """ init """ self.host = LDAP['server'] self.port = LDAP.get('port', 389) self.use_ssl = LDAP.get('use_ssl', False) self.domain = LDAP['domain'] self.base = LDAP['base'] self.search_filter = "uid={uid}" def login(self, username, password): """ 登录 :return: """ server = Server(host=self.host, port=self.port, use_ssl=self.use_ssl, connect_timeout=15, get_info=ALL) try: conn = Connection(server, user=f"uid={username},{self.base}", password=password, check_names=True, lazy=False, auto_bind=True, receive_timeout=30 ) except Exception as e: err_msg = f'LDAP 认证失败:{e}' logger.error(err_msg) return False else: msg = conn.result print(msg) conn.unbind() return True # print(server.info) # print(server.schema) # _username = (conn.extend.standard.who_am_i()) # print(_username) ldap_ins = LdapAdmin() resp = ldap_ins.login("panbiao", "123456") print(resp)