<s> from django . conf import settings <EOL> from django . core . exceptions import SuspiciousOperation <EOL> from django . utils import simplejson as json <EOL> from django . utils . hashcompat import sha_constructor <EOL> import hmac <EOL> from formwizard import storage <EOL> sha_hmac = sha_constructor <EOL> class CookieStorage ( storage . BaseStorage ) : <EOL> encoder = json . JSONEncoder ( separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CookieStorage , self ) . __init__ ( * args , ** kwargs ) <EOL> self . data = self . load_data ( ) <EOL> if self . data is None : <EOL> self . init_data ( ) <EOL> def unsign_cookie_data ( self , data ) : <EOL> if data is None : <EOL> return None <EOL> bits = data . split ( '<STR_LIT:$>' , <NUM_LIT:1> ) <EOL> if len ( bits ) == <NUM_LIT:2> : <EOL> if bits [ <NUM_LIT:0> ] == self . get_cookie_hash ( bits [ <NUM_LIT:1> ] ) : <EOL> return bits [ <NUM_LIT:1> ] <EOL> raise SuspiciousOperation ( '<STR_LIT>' ) <EOL> def load_data ( self ) : <EOL> data = self . request . COOKIES . get ( self . prefix , None ) <EOL> cookie_data = self . unsign_cookie_data ( data ) <EOL> if cookie_data is None : <EOL> return None <EOL> return json . loads ( cookie_data , cls = json . JSONDecoder ) <EOL> def update_response ( self , response ) : <EOL> if self . data : <EOL> response . set_cookie ( self . prefix , self . create_cookie_data ( self . data ) ) <EOL> else : <EOL> response . delete_cookie ( self . prefix ) <EOL> return response <EOL> def create_cookie_data ( self , data ) : <EOL> encoded_data = self . encoder . encode ( self . data ) <EOL> cookie_data = '<STR_LIT>' % ( self . get_cookie_hash ( encoded_data ) , <EOL> encoded_data ) <EOL> return cookie_data <EOL> def get_cookie_hash ( self , data ) : <EOL> return hmac . new ( '<STR_LIT>' % ( settings . SECRET_KEY , self . prefix ) , <EOL> data , sha_hmac ) . hexdigest ( ) </s>
