python:setup_grok_for_ldap_authentication
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| python:setup_grok_for_ldap_authentication [2011/09/20 11:19] – Externe Bearbeitung 127.0.0.1 | python:setup_grok_for_ldap_authentication [2017/11/15 08:08] (aktuell) – gelöscht jenad | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | == Motivation == | ||
| - | Die python-Module " | ||
| - | |||
| - | http:// | ||
| - | |||
| - | == der Code (grok-1.0)== | ||
| - | |||
| - | es werden einige Konstanten in einer externen config-Datei vorrausgesetzt. | ||
| - | |||
| - | Das Utility muß in der app registriert werden: | ||
| - | |||
| - | |||
| - | <code python> | ||
| - | class MyApp(grok.Application, | ||
| - | .... | ||
| - | # registrierung der ldapauth und der plugins, | ||
| - | | ||
| - | | ||
| - | | ||
| - | ) | ||
| - | | ||
| - | .... | ||
| - | </ | ||
| - | |||
| - | |||
| - | Konfigurationseinstellungen werden in einer Datei config.py gespeichert. | ||
| - | |||
| - | Datei ldapauth.py: | ||
| - | |||
| - | <code python> | ||
| - | import config | ||
| - | import grok | ||
| - | |||
| - | def setup_authentication(pau): | ||
| - | """ | ||
| - | |||
| - | Sets up an IAuthenticatorPlugin and | ||
| - | ICredentialsPlugin (for the authentication mechanism) | ||
| - | """ | ||
| - | pau.credentialsPlugins = [' | ||
| - | pau.authenticatorPlugins = [' | ||
| - | pau.prefix = u' | ||
| - | |||
| - | from zope.app.authentication.session import SessionCredentialsPlugin | ||
| - | from zope.app.authentication.interfaces import ICredentialsPlugin | ||
| - | |||
| - | class MyCreds(grok.GlobalUtility, | ||
| - | grok.provides(ICredentialsPlugin) | ||
| - | grok.name(' | ||
| - | | ||
| - | loginpagename = ' | ||
| - | loginfield = ' | ||
| - | passwordfield = ' | ||
| - | |||
| - | |||
| - | from zope.app.authentication.interfaces import IAuthenticatorPlugin | ||
| - | |||
| - | class UserAuthenticatorPlugin(grok.GlobalUtility): | ||
| - | grok.provides(IAuthenticatorPlugin) | ||
| - | grok.name(' | ||
| - | |||
| - | def authenticateCredentials(self, | ||
| - | if not isinstance(credentials, | ||
| - | return None | ||
| - | if not (' | ||
| - | return None | ||
| - | account = self.getAccount(credentials[' | ||
| - | |||
| - | if account is None: | ||
| - | return None | ||
| - | if not account.checkPassword(credentials[' | ||
| - | return None | ||
| - | return PrincipalInfo(id=account.name, | ||
| - | | ||
| - | | ||
| - | |||
| - | def principalInfo(self, | ||
| - | account = self.getAccount(id) | ||
| - | if account is None: | ||
| - | return None | ||
| - | return PrincipalInfo(id=account.name, | ||
| - | | ||
| - | | ||
| - | |||
| - | def getAccount(self, | ||
| - | # ... look up the account object and return it ... | ||
| - | return Account(login) | ||
| - | |||
| - | |||
| - | from zope.app.authentication.interfaces import IPrincipalInfo | ||
| - | |||
| - | class PrincipalInfo(object): | ||
| - | grok.implements(IPrincipalInfo) | ||
| - | |||
| - | def __init__(self, | ||
| - | self.id = id | ||
| - | self.title = title | ||
| - | self.description = description | ||
| - | self.credentialsPlugin = None | ||
| - | self.authenticatorPlugin = None | ||
| - | |||
| - | |||
| - | import ldap | ||
| - | from zope import component | ||
| - | from zope.app.authentication.interfaces import IPasswordManager | ||
| - | |||
| - | class Account(object): | ||
| - | """ | ||
| - | checkpassword bereit, um das passwort zu testen | ||
| - | | ||
| - | todo: das passwort wird bei jedem seitenaufruf ueberprueft, | ||
| - | hier sollte irgendwas in der session gemerkt werden (user is | ||
| - | authenticated oder sowas) | ||
| - | | ||
| - | """ | ||
| - | def __init__(self, | ||
| - | self.name = name | ||
| - | self.password = None | ||
| - | | ||
| - | def checkPassword(self, | ||
| - | """ | ||
| - | auth-quellen gemacht werden | ||
| - | | ||
| - | in diesem fall gegen ldap | ||
| - | """ | ||
| - | | ||
| - | lconn=ldap.initialize(' | ||
| - | if True: | ||
| - | lconn.simple_bind_s() | ||
| - | r=lconn.search_s(config.LDAPSEARCHBASE, | ||
| - | ldap.SCOPE_SUBTREE, | ||
| - | ' | ||
| - | [' | ||
| - | if len(r)==1: | ||
| - | dn, | ||
| - | try: | ||
| - | lconn.simple_bind_s(dn, | ||
| - | except: | ||
| - | return False | ||
| - | self.cn=atts[' | ||
| - | return True | ||
| - | else: | ||
| - | return False | ||
| - | </ | ||
python/setup_grok_for_ldap_authentication.1316517559.txt.gz · Zuletzt geändert: (Externe Bearbeitung)
