Back to index

plone3  3.1.7
SessionAuthHelper.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 """ Class: SessionAuthHelper
00016 
00017 $Id: SessionAuthHelper.py 70851 2006-10-20 19:25:56Z jens $
00018 """
00019 
00020 from AccessControl.SecurityInfo import ClassSecurityInfo
00021 from App.class_init import default__class_init__ as InitializeClass
00022 
00023 from zope.interface import Interface
00024 
00025 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00026 
00027 from Products.PluggableAuthService.interfaces.plugins import \
00028         ILoginPasswordHostExtractionPlugin
00029 from Products.PluggableAuthService.interfaces.plugins import \
00030         ICredentialsUpdatePlugin
00031 from Products.PluggableAuthService.interfaces.plugins import \
00032         ICredentialsResetPlugin
00033 from Products.PluggableAuthService.plugins.BasePlugin import BasePlugin
00034 from Products.PluggableAuthService.utils import classImplements
00035 
00036 class ISessionAuthHelper(Interface):
00037     """ Marker interface.
00038     """
00039 
00040 
00041 manage_addSessionAuthHelperForm = PageTemplateFile(
00042     'www/saAdd', globals(), __name__='manage_addSessionAuthHelperForm')
00043 
00044 
00045 def manage_addSessionAuthHelper(dispatcher, id, title=None, REQUEST=None):
00046     """ Add a Session Auth Helper to a Pluggable Auth Service. """
00047     sp = SessionAuthHelper(id, title)
00048     dispatcher._setObject(sp.getId(), sp)
00049 
00050     if REQUEST is not None:
00051         REQUEST['RESPONSE'].redirect( '%s/manage_workspace'
00052                                       '?manage_tabs_message='
00053                                       'SessionAuthHelper+added.'
00054                                     % dispatcher.absolute_url() )
00055 
00056 
00057 class SessionAuthHelper(BasePlugin):
00058     """ Multi-plugin for managing details of Session Authentication. """
00059     meta_type = 'Session Auth Helper'
00060     security = ClassSecurityInfo()
00061 
00062 
00063     def __init__(self, id, title=None):
00064         self._setId(id)
00065         self.title = title
00066 
00067     security.declarePrivate('extractCredentials')
00068     def extractCredentials(self, request):
00069         """ Extract basic auth credentials from 'request'. """
00070         creds = {}
00071 
00072         # Looking into the session first...
00073         name = request.SESSION.get('__ac_name', '')
00074         password = request.SESSION.get('__ac_password', '')
00075 
00076         if name:
00077             creds[ 'login' ] = name
00078             creds[ 'password' ] = password
00079         else:
00080             # Look into the request now
00081             login_pw = request._authUserPW()
00082 
00083             if login_pw is not None:
00084                 name, password = login_pw
00085                 creds[ 'login' ] = name
00086                 creds[ 'password' ] = password
00087                 request.SESSION.set('__ac_name', name)
00088                 request.SESSION.set('__ac_password', password)
00089 
00090         if creds:
00091             creds['remote_host'] = request.get('REMOTE_HOST', '')
00092 
00093             try:
00094                 creds['remote_address'] = request.getClientAddr()
00095             except AttributeError:
00096                 creds['remote_address'] = request.get('REMOTE_ADDR', '')
00097 
00098         return creds
00099 
00100     security.declarePrivate('updateCredentials')
00101     def updateCredentials(self, request, response, login, new_password):
00102         """ Respond to change of credentials. """
00103         request.SESSION.set('__ac_name', login)
00104         request.SESSION.set('__ac_password', new_password)
00105 
00106     security.declarePrivate('resetCredentials')
00107     def resetCredentials(self, request, response):
00108         """ Empty out the currently-stored session values """
00109         request.SESSION.set('__ac_name', '')
00110         request.SESSION.set('__ac_password', '')
00111 
00112 classImplements( SessionAuthHelper
00113                , ISessionAuthHelper
00114                , ILoginPasswordHostExtractionPlugin
00115                , ICredentialsUpdatePlugin
00116                , ICredentialsResetPlugin
00117                )
00118 
00119 InitializeClass(SessionAuthHelper)
00120