Python使用LDAP做用戶認(rèn)證的方法
LDAP(Light Directory Access Portocol)是輕量目錄訪問協(xié)議,基于X.500標(biāo)準(zhǔn),支持TCP/IP。
LDAP目錄以樹狀的層次結(jié)構(gòu)來存儲(chǔ)數(shù)據(jù)。每個(gè)目錄記錄都有標(biāo)識(shí)名(Distinguished Name,簡稱DN),用來讀取單個(gè)記錄,
一般是這樣的:
cn=username,ou=people,dc=test,dc=com
幾個(gè)關(guān)鍵字的含義如下:
- base dn:LDAP目錄樹的最頂部,也就是樹的根,是上面的dc=test,dc=com部分,一般使用公司的域名,也可以寫做o=test.com,前者更靈活一些。
- dc::Domain Component,域名部分。
- ou:Organization Unit,組織單位,用于將數(shù)據(jù)區(qū)分開。
- cn:Common Name,一般使用用戶名。
- uid:用戶id,與cn的作用類似。
- sn:Surname, 姓。
- rdn:Relative dn,dn中與目錄樹的結(jié)構(gòu)無關(guān)的部分,通常存在cn或者uid這個(gè)屬性里。
所以上面的dn代表一條記錄,代表一位在test.com公司people部門的用戶username。
python-ldap
python一般使用python-ldap庫操作ldap,文檔:https://www.python-ldap.org/en/latest/index.html。
下載:
pip install python-ldap
還要安裝一些環(huán)境,ubuntu:
apt-get install build-essential python3-dev python2.7-dev \ libldap2-dev libsasl2-dev slapd ldap-utils python-tox \ lcov valgrind
CentOS:
yum groupinstall "Development tools" yum install openldap-devel python-devel
獲取LDAP地址后即可與LDAP建立連接:
import ldap ldapconn = ldap.initialize('ldap://192.168.1.111:389')
綁定用戶,可用于用戶驗(yàn)證,用戶名必須是dn:
ldapconn.simple_bind_s('cn=username,ou=people,dc=test,dc=com', pwd)
成功認(rèn)證時(shí)會(huì)返回一個(gè)tuple:
(97, [], 1, [])
驗(yàn)證失敗會(huì)報(bào)異常ldap.INVALID_CREDENTIALS:
{'desc': u'Invalid credentials'}
注意驗(yàn)證時(shí)傳空值驗(yàn)證也是可以通過的,注意要對dn和pwd進(jìn)行檢查。
查詢LDAP用戶信息時(shí),需要登錄管理員RootDN帳號(hào):
ldapconn.simple_bind_s('cn=admin,dc=test,dc=com', 'adminpwd') searchScope = ldap.SCOPE_SUBTREE searchFilter = 'cn=username' base_dn = 'ou=people,dc=test,dc=com' print ldapconn.search_s(base_dn, searchScope, searchFilter, None)
添加用戶add_s(dn, modlist),dn為要添加的條目dn,modlist為存儲(chǔ)信息:
dn = 'cn=test,ou=people,dc=test,dc=com' modlist = [ ('objectclass', ['person', 'organizationalperson'], ('cn', ['test']), ('uid', [''testuid]), ('userpassword', ['pwd']), ] result = ldapconn.add_s(dn, modlist)
添加成功會(huì)返回元組:
(105, [], 2, [])
失敗會(huì)報(bào)ldap.LDAPError異常
Django使用LDAP驗(yàn)證
一個(gè)很簡單的LDAP驗(yàn)證Backend:
import ldap class LDAPBackend(object): """ Authenticates with ldap. """ _connection = None _connection_bound = False def authenticate(self, username=None, passwd=None, **kwargs): if not username or not passwd: return None if self._authenticate_user_dn(username, passwd): user = self._get_or_create_user(username, passwd) return user else: return None @property def connection(self): if not self._connection_bound: self._bind() return self._get_connection() def _bind(self): self._bind_as( LDAP_CONFIG['USERNAME'], LDAP_CONFIG['PASSWORD'], True ) def _bind_as(self, bind_dn, bind_password, sticky=False): self._get_connection().simple_bind_s( bind_dn, bind_password ) self._connection_bound = sticky def _get_connection(self): if not self._connection: self._connection = ldap.initialize(LDAP_CONFIG['HOST']) return self._connection def _authenticate_user_dn(self, username, passwd): bind_dn = 'cn=%s,%s' % (username, LDAP_CONFIG['BASE_DN']) try: self._bind_as(bind_dn, passwd, False) return True except ldap.INVALID_CREDENTIALS: return False def _get_or_create_user(self, username, passwd): # 獲取或者新建User return user
不想自己寫的話,django與flask都有現(xiàn)成的庫:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)身份證實(shí)名認(rèn)證的方法實(shí)例
- 微信小程序python用戶認(rèn)證的實(shí)現(xiàn)
- python連接mongodb密碼認(rèn)證實(shí)例
- python pycurl驗(yàn)證basic和digest認(rèn)證的方法
- python2.7+selenium2實(shí)現(xiàn)淘寶滑塊自動(dòng)認(rèn)證功能
- Python3中使用urllib的方法詳解(header,代理,超時(shí),認(rèn)證,異常處理)
- 將Python的Django框架與認(rèn)證系統(tǒng)整合的方法
- Python使用htpasswd實(shí)現(xiàn)基本認(rèn)證授權(quán)的例子
- Python如何實(shí)現(xiàn)后端自定義認(rèn)證并實(shí)現(xiàn)多條件登陸
相關(guān)文章
Python Pandas 對列/行進(jìn)行選擇,增加,刪除操作
這篇文章主要介紹了Python Pandas 對列/行進(jìn)行選擇,增加,刪除操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Python中循環(huán)引用(import)失敗的解決方法
在python中常常會(huì)遇到循環(huán)import即circular import的問題,下面這篇文章主要給大家介紹了關(guān)于Python中循環(huán)引用(import)失敗的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04