pythonldap实现登录

下面一段代码是小编给大家介绍的python ldap实现登录实例代码,一起看看吧

ldap_config = {
‘ldap_path’: ‘ldap://xx.xx.xx.xx:389’,
‘base_dn’: ‘ou=users,dc=ledo,dc=com’,
‘ldap_user’: ‘u,
‘ldap_pass’: ‘111111.0’,
‘original_pass’: ‘111111.0’
}
ldap_message = {
0: 0, #’ok’
1: 1, #’用户名或密码错误’
2: 2, #ldap验证异常’
}
import ldap
import base64
import hashlib
from config_message import ldap_config, ldap_message
class ldap_api(object):
_ldap_path = ldap_config[‘ldap_path’]
_base_dn = ldap_config[‘base_dn’]
_ldap_user = ldap_config[‘ldap_user’]
_ldap_pass = ldap_config[‘ldap_pass’]
_original_pass = ldap_config[‘original_pass’]
# 连接ldap服务器
def __init__(self):
try:
self.ldapconn = ldap.initialize(self._ldap_path)
self.ldapconn.protocal_version = ldap.version3
self.ldapconn.simple_bind(self._ldap_user, self._ldap_pass)
except ldap.ldaperror, e:
print e
# 验证用户登录
def ldap_check_login(self, username, password):
obj = self.ldapconn
searchscope = ldap.scope_subtree
# searchfilter = ‘(&(cn=’+username+’)(userpassword=’+password+’))’
searchfilter = ‘ustatus’: ldap_message[1], ‘data’: ”}
dic = result_data[0][1]
l_realname = dic[‘sn’][0]
l_password = dic[‘userpassword’][0]
md_password = ldap_api.hash_md5(password)
if l_password in (password, md_password):
return {‘status’: ldap_message[0], ‘data’: l_realname}
else:
return {‘status’: ldap_message[1], ‘data’: ”}
except ldap.ldaperror, e:
return {‘status’: ldap_message[2], ‘data’: ”}
@staticmethod
def hash_md5(data):
md = hashlib.md5()
md.update(str(data))
a = md.digest()
b = ‘{md5}’ + base64.b64encode(a)
return b

更多python ldap实现登录相关文章请关注php中文网!

Posted in 未分类

发表评论