Commit 111da6bd authored by shenshuo's avatar shenshuo

添加ldap处理模块

parent 3fe42dfe
......@@ -9,13 +9,14 @@ from distutils.core import setup
setup(
name='opssdk',
version='0.0.11',
version='0.0.12',
packages=['opssdk', 'opssdk.logs', 'opssdk.operate', 'opssdk.install', 'opssdk.get_info', 'opssdk.utils', 'websdk'],
url='https://github.com/ss1917/ops_sdk/',
license='',
install_requires=['fire', 'shortuuid', 'pymysql===0.9.3', 'sqlalchemy===1.2.15', 'python3-pika===0.9.14', 'PyJWT',
'Crypto===1.4.1', 'requests', 'redis===2.10.6', 'tornado===5.0',
'aliyun-python-sdk-core-v3===2.8.6', 'aliyun-python-sdk-dysmsapi','python-dateutil===2.7.5'],
'aliyun-python-sdk-core-v3===2.8.6', 'aliyun-python-sdk-dysmsapi','python-dateutil===2.7.5',
'ldap3===2.6' ],
author='shenshuo',
author_email='191715030@qq.com',
description='SDK of the operation and maintenance script'
......
......@@ -142,3 +142,14 @@ const.STORAGE_NAME = "STORAGE_NAME"
const.STORAGE_PATH = "STORAGE_PATH"
const.STORAGE_KEY_ID = "STORAGE_KEY_ID"
const.STORAGE_KEY_SECRET = "STORAGE_KEY_SECRET"
### LDAP
const.LDAP_SERVER_HOST = "LDAP_SERVER_HOST"
const.LDAP_SERVER_PORT = "LDAP_SERVER_PORT"
const.LDAP_ADMIN_DN = "LDAP_ADMIN_DN"
const.LDAP_ADMIN_PASSWORD = "LDAP_ADMIN_PASSWORD"
const.LDAP_SEARCH_BASE = "LDAP_SEARCH_BASE"
const.LDAP_SEARCH_FILTER = "LDAP_SEARCH_FILTER"
const.LDAP_ATTRIBUTES = "LDAP_ATTRIBUTES"
const.LDAP_USE_SSL = "LDAP_USE_SSL"
const.LDAP_ENABLE = "LDAP_ENABLE"
\ No newline at end of file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Contact : 191715030@qq.com
Author : shenshuo
Date : 2019/4/17
Desc : 对接LDAP登录认证
"""
from ldap3 import Server, Connection, ALL, SUBTREE, ServerPool
class LdapApi:
def __init__(self, ldap_server_host, ldap_admin_dn, ldap_admin_password, ldap_server_port=389, ldap_use_ssl=False):
self._ldap_admin_dn = ldap_admin_dn
self._ldap_admin_password = ldap_admin_password
# ldap_server_pool = ServerPool(["172.16.0.102",'172.16.0.103'])
self.ldap_server = Server(ldap_server_host, port=ldap_server_port, use_ssl=ldap_use_ssl)
def ldap_server_test(self):
try:
conn = Connection(self.ldap_server, user=self._ldap_admin_dn, password=self._ldap_admin_password,
check_names=True, lazy=False, raise_exceptions=False)
conn.open()
conn.bind()
return True
except Exception as e:
print("auth fail {}".format(e))
return False
def ldap_auth(self, username, password, search_base, search_filter='cn'):
if not self.ldap_server_test():
return False, None, None
conn = Connection(self.ldap_server, user=self._ldap_admin_dn, password=self._ldap_admin_password,
check_names=True, lazy=False, raise_exceptions=False)
conn.open()
conn.bind()
res = conn.search(search_base=search_base,
search_filter='({}={})'.format(search_filter, username),
search_scope=SUBTREE,
attributes=['cn', 'givenName', 'email', 'mail', 'sAMAccountName'],
paged_size=5)
if res:
entry = conn.response[0]
dn = entry['dn']
attr_dict = entry['attributes']
# check password by dn
try:
conn2 = Connection(self.ldap_server, user=dn, password=password, check_names=True, lazy=False,
raise_exceptions=False)
conn2.bind()
if conn2.result["description"] == "success":
if attr_dict["email"]:
email = attr_dict["email"][0]
elif attr_dict["mail"]:
email = attr_dict["mail"][0]
else:
email = None
return True, username, email
else:
print("auth fail")
return False, None, None
except Exception as e:
print("auth fail {}".format(e))
return False, None, None
else:
return False, None, None
if __name__ == "__main__":
obj = LdapApi('172.16.0.102', 'cn=Manager,DC=shinezone,DC=com', '070068')
print(obj.ldap_server_test())
print('____________')
print(obj.ldap_auth("yanghongfei", "123456", 'ou=opendevops,dc=shinezone,dc=com', 'cn'))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment