zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    课堂练习
    日程管理测试用例
    日程管理APP的测试计划和测试矩阵
    日程管理Bug Report
    图书管理系统活动图
    团队如何做决定?
    课堂练习
    课堂练习(NABCD Model)
    课堂练习
    日程管理的测试计划和测试矩阵
  • 原文地址:https://www.cnblogs.com/jcici/p/11912902.html
Copyright © 2011-2022 走看看