Package repoze :: Package who :: Package plugins :: Module cookie
[hide private]

Source Code for Module repoze.who.plugins.cookie

 1  import binascii 
 2   
 3  from paste.request import get_cookies 
 4   
 5  from zope.interface import implements 
 6   
 7  from repoze.who.interfaces import IIdentifier 
 8   
9 -class InsecureCookiePlugin(object):
10 11 implements(IIdentifier) 12
13 - def __init__(self, cookie_name, cookie_path='/'):
14 self.cookie_name = cookie_name 15 self.cookie_path = cookie_path
16 17 # IIdentifier
18 - def identify(self, environ):
19 cookies = get_cookies(environ) 20 cookie = cookies.get(self.cookie_name) 21 22 if cookie is None: 23 return None 24 25 try: 26 auth = cookie.value.decode('base64') 27 except binascii.Error: # can't decode 28 return None 29 30 try: 31 login, password = auth.split(':', 1) 32 return {'login':login, 'password':password} 33 except ValueError: # not enough values to unpack 34 return None
35 36 # IIdentifier
37 - def forget(self, environ, identity):
38 # return a expires Set-Cookie header 39 expired = ('%s=""; Path=%s; Expires=Sun, 10-May-1971 11:59:00 GMT' % 40 (self.cookie_name, self.cookie_path)) 41 return [('Set-Cookie', expired)]
42 43 # IIdentifier
44 - def remember(self, environ, identity):
45 cookie_value = '%(login)s:%(password)s' % identity 46 cookie_value = cookie_value.encode('base64').rstrip() 47 cookies = get_cookies(environ) 48 existing = cookies.get(self.cookie_name) 49 value = getattr(existing, 'value', None) 50 if value != cookie_value: 51 # return a Set-Cookie header 52 set_cookie = '%s=%s; Path=%s;' % (self.cookie_name, cookie_value, 53 self.cookie_path) 54 return [('Set-Cookie', set_cookie)]
55
56 - def __repr__(self):
57 return '<%s %s>' % (self.__class__.__name__, 58 id(self)) #pragma NO COVERAGE
59
60 -def make_plugin(cookie_name='repoze.who.plugins.cookie', cookie_path='/'):
61 plugin = InsecureCookiePlugin(cookie_name, cookie_path) 62 return plugin
63