import base64, urllib from zope.interface import Interface, implements from zope.schema import ASCIILine from zope.publisher.interfaces.http import IHTTPRequest from zope.i18nmessageid import MessageFactory _ = MessageFactory('krihvel') from zope.app.authentication.session import SessionCredentialsPlugin class ICookieCredentials(Interface): cookie_name = ASCIILine( title=_(u'Cookie name'), description=_(u'Name of the cookie for storing credentials.'), required=True ) class CookieCredentialsPlugin(SessionCredentialsPlugin): implements(ICookieCredentials) cookie_name = 'krihvel.auth' def extractCredentials(self, request): if not IHTTPRequest.providedBy(request): return login = request.get(self.loginfield, None) password = request.get(self.passwordfield, None) cookie = request.get(self.cookie_name, None) if login and password: # Special characters in login or password raise an error try: val = base64.encodestring('%s:%s' % (login, password)) request.response.setCookie(self.cookie_name, urllib.quote(val), path='/') except: return elif cookie: val = base64.decodestring(urllib.unquote(cookie)) login, password = val.split(':') else: return return {'login': login, 'password': password} def logout(self, request): if not IHTTPRequest.providedBy(request): return request.response.expireCookie(self.cookie_name, path='/')