python:grok-1.4-ldapauth
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
python:grok-1.4-ldapauth [2011/10/20 10:07] – jenad | python:grok-1.4-ldapauth [2017/11/15 09:08] (aktuell) – gelöscht jenad | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ==== LDAP-Auth in grok-1.4 ==== | ||
- | === Motivaton === | ||
- | |||
- | Nur User des zentralen Verzeichnisses dürfen diese Webseiten anschauen. Sie müssen sich vorher mit ihrer uid anmelden. Das Attribut " | ||
- | |||
- | === Voraussetzungen === | ||
- | |||
- | grok-1.4 | ||
- | python-ldap | ||
- | |||
- | bitte testen Sie unbedingt mit der grok-Konsole, | ||
- | |||
- | < | ||
- | bin/ | ||
- | >>> | ||
- | </ | ||
- | |||
- | === Implementation === | ||
- | app.py | ||
- | <code python> | ||
- | import grok | ||
- | import rolesAndPerms | ||
- | import ldapauth | ||
- | |||
- | from demo import resource | ||
- | |||
- | from zope.pluggableauth.authentication import PluggableAuthentication | ||
- | from zope.authentication.interfaces import IAuthentication | ||
- | |||
- | |||
- | class Demo(grok.Application, | ||
- | grok.local_utility( | ||
- | | ||
- | setup=ldapauth.setup_authentication, | ||
- | ) | ||
- | |||
- | class Index(grok.View): | ||
- | grok.require(" | ||
- | def update(self): | ||
- | resource.style.need() | ||
- | |||
- | |||
- | from zope.interface import Interface | ||
- | |||
- | class MyLogin(grok.Form): | ||
- | grok.context(Interface) | ||
- | grok.require(' | ||
- | |||
- | @grok.action(' | ||
- | def handle_login(self, | ||
- | self.redirect(self.request.form.get(' | ||
- | | ||
- | from zope.pluggableauth.interfaces import ILogout | ||
- | from zope.authentication.interfaces import IUnauthenticatedPrincipal | ||
- | |||
- | from zope.component import getUtility | ||
- | class Logout(grok.View): | ||
- | grok.context(Interface) | ||
- | grok.require(' | ||
- | |||
- | def update(self): | ||
- | if not IUnauthenticatedPrincipal.providedBy(self.request.principal): | ||
- | auth = getUtility(IAuthentication) | ||
- | ILogout(auth).logout(self.request) | ||
- | def render(self): | ||
- | return " | ||
- | |||
- | </ | ||
- | |||
- | rolesAndPerms.py | ||
- | <code python> | ||
- | import grok | ||
- | from zope.pluggableauth.interfaces import IAuthenticatedPrincipalCreated | ||
- | from zope.securitypolicy.interfaces import IRolePermissionManager, | ||
- | |||
- | @grok.subscribe(IAuthenticatedPrincipalCreated) | ||
- | def update_principal_info_from_ldap(event): | ||
- | | ||
- | app = grok.getSite() | ||
- | prm = IPrincipalRoleManager(app) | ||
- | | ||
- | |||
- | |||
- | class ViewDemo(grok.Permission): | ||
- | grok.name(' | ||
- | grok.title(' | ||
- | |||
- | class Viewer(grok.Role): | ||
- | """ | ||
- | A Viewer can view. | ||
- | """ | ||
- | grok.name(' | ||
- | grok.title(' | ||
- | grok.description(' | ||
- | grok.permissions(' | ||
- | # alternatively, | ||
- | # grok.permissions( | ||
- | # ' | ||
- | |||
- | </ | ||
- | |||
- | ldapauth.py | ||
- | <code python> | ||
- | 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.pluggableauth.plugins.session import SessionCredentialsPlugin | ||
- | from zope.pluggableauth.interfaces import ICredentialsPlugin | ||
- | |||
- | class MyCreds(grok.GlobalUtility, | ||
- | grok.provides(ICredentialsPlugin) | ||
- | grok.name(' | ||
- | |||
- | loginpagename = ' | ||
- | loginfield = ' | ||
- | passwordfield = ' | ||
- | |||
- | |||
- | from zope.pluggableauth.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.pluggableauth.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.password.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(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 | ||
- | </ | ||
- | |||
- | app_templates/ | ||
- | <code xml> | ||
- | < | ||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | <form action="" | ||
- | method=" | ||
- | enctype=" | ||
- | |||
- | <table class=" | ||
- | < | ||
- | | ||
- | <tr> | ||
- | <td class=" | ||
- | | ||
- | <label for=" | ||
- | <span class=" | ||
- | </ | ||
- | </td> | ||
- | <td class=" | ||
- | <div class=" | ||
- | | ||
- | </td> | ||
- | </tr> | ||
- | | ||
- | | ||
- | <tr> | ||
- | <td class=" | ||
- | | ||
- | <label for=" | ||
- | <span class=" | ||
- | </ | ||
- | </td> | ||
- | <td class=" | ||
- | <div class=" | ||
- | | ||
- | </td> | ||
- | </tr> | ||
- | | ||
- | </ | ||
- | </ | ||
- | |||
- | <div id=" | ||
- | <span class=" | ||
- | <input type=" | ||
- | </ | ||
- | </ | ||
- | |||
- | <input tal: | ||
- | | ||
- | |||
- | </ | ||
- | |||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | index.pt | ||
- | <code html> | ||
- | < | ||
- | < | ||
- | </ | ||
- | < | ||
- | < | ||
- | |||
- | < | ||
- | Edit < | ||
- | this page.</ | ||
- | <img tal: | ||
- | <p> | ||
- | <span i18n: | ||
- | < | ||
- | i18n: | ||
- | </ | ||
- | |||
- | <a href=" | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | |||
- | resource.py | ||
- | <code python> | ||
- | from fanstatic import Library, Resource | ||
- | |||
- | library = Library(' | ||
- | |||
- | style = Resource(library, | ||
- | </ |
python/grok-1.4-ldapauth.1319098048.txt.gz · Zuletzt geändert: 2024/08/07 13:35 (Externe Bearbeitung)