Back to index

plone3  3.1.7
InlineAuthHelper.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: CookieAuthHelper
00016 
00017 $Id: InlineAuthHelper.py 70851 2006-10-20 19:25:56Z jens $
00018 """
00019 
00020 from base64 import encodestring, decodestring
00021 from urllib import quote
00022 
00023 from AccessControl.SecurityInfo import ClassSecurityInfo
00024 from OFS.Folder import Folder
00025 from App.class_init import default__class_init__ as InitializeClass
00026 
00027 from zope.interface import Interface
00028 
00029 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00030 from Products.PageTemplates.ZopePageTemplate import manage_addPageTemplate
00031 
00032 from Products.PluggableAuthService.interfaces.plugins import \
00033         ILoginPasswordHostExtractionPlugin
00034 from Products.PluggableAuthService.interfaces.plugins import \
00035         IChallengePlugin
00036 from Products.PluggableAuthService.interfaces.plugins import \
00037         ICredentialsUpdatePlugin
00038 from Products.PluggableAuthService.interfaces.plugins import \
00039         ICredentialsResetPlugin
00040 from Products.PluggableAuthService.plugins.BasePlugin import BasePlugin
00041 from Products.PluggableAuthService.utils import classImplements
00042 
00043 class IInlineAuthHelper(Interface):
00044     """ Marker interface.
00045     """
00046 
00047 
00048 manage_addInlineAuthHelperForm = PageTemplateFile(
00049     'www/iaAdd', globals(), __name__='manage_addInlineAuthHelperForm')
00050 
00051 
00052 def addInlineAuthHelper( dispatcher
00053                        , id
00054                        , title=None
00055                        , REQUEST=None
00056                        ):
00057     """ Add an Inline Auth Helper to a Pluggable Auth Service. """
00058     iah = InlineAuthHelper(id, title)
00059     dispatcher._setObject(iah.getId(), iah)
00060 
00061     if REQUEST is not None:
00062         REQUEST['RESPONSE'].redirect( '%s/manage_workspace'
00063                                       '?manage_tabs_message='
00064                                       'InlineAuthHelper+added.'
00065                                     % dispatcher.absolute_url() )
00066 
00067 
00068 class InlineAuthHelper(Folder, BasePlugin):
00069     """ Multi-plugin for managing details of Inline Authentication. """
00070     meta_type = 'Inline Auth Helper'
00071     security = ClassSecurityInfo()
00072 
00073     _properties = ( { 'id'    : 'title'
00074                     , 'label' : 'Title'
00075                     , 'type'  : 'string'
00076                     , 'mode'  : 'w'
00077                     },
00078                   )
00079 
00080     manage_options = ( BasePlugin.manage_options[:1]
00081                      + Folder.manage_options[:1]
00082                      + Folder.manage_options[2:]
00083                      )
00084 
00085     def __init__(self, id, title=None):
00086         self.id = self._id = id
00087         self.title = title
00088         self.body = BASIC_LOGIN_FORM
00089 
00090     security.declarePrivate('extractCredentials')
00091     def extractCredentials(self, request):
00092         """ Extract credentials from cookie or 'request'. """
00093         creds = {}
00094 
00095         # Look in the request for the names coming from the login form
00096         login = request.get('__ac_name', '')
00097         password = request.get('__ac_password', '')
00098 
00099         if login:
00100             creds['login'] = login
00101             creds['password'] = password
00102 
00103         if creds:
00104             creds['remote_host'] = request.get('REMOTE_HOST', '')
00105 
00106             try:
00107                 creds['remote_address'] = request.getClientAddr()
00108             except AttributeError:
00109                 creds['remote_address'] = request.get('REMOTE_ADDR', '')
00110 
00111         return creds
00112 
00113     security.declarePrivate('challenge')
00114     def challenge(self, request, response, **kw):
00115         """ Challenge the user for credentials. """
00116         response.setStatus('200')
00117         response.setBody(self.body)
00118 
00119         # Keep HTTPResponse.exception() from further writing on the
00120         # response body, without using HTTPResponse.write()
00121         response._locked_status = True
00122         response.setBody = self._setBody # Keep response.exception
00123         return True
00124 
00125     # Methods to override on response
00126 
00127     def _setBody(self, body, *args, **kw):
00128         pass
00129 
00130 classImplements( InlineAuthHelper
00131                , IInlineAuthHelper
00132                , ILoginPasswordHostExtractionPlugin
00133                , IChallengePlugin
00134                )
00135 
00136 InitializeClass(InlineAuthHelper)
00137 
00138 
00139 BASIC_LOGIN_FORM = """<html>
00140   <head>
00141     <title> Login Form </title>
00142   </head>
00143 
00144   <body>
00145 
00146     <h3> Please log in </h3>
00147 
00148     <form method="post">
00149       <table cellpadding="2">
00150         <tr>
00151           <td><b>Login:</b> </td>
00152           <td><input type="text" name="__ac_name" size="30" /></td>
00153         </tr>
00154         <tr>
00155           <td><b>Password:</b></td>
00156           <td><input type="password" name="__ac_password" size="30" /></td>
00157         </tr>
00158         <tr>
00159           <td colspan="2">
00160             <br />
00161             <input type="submit" value=" Log In " />
00162           </td>
00163         </tr>
00164       </table>
00165 
00166     </form>
00167 
00168   </body>
00169 
00170 </html>
00171 """
00172