<s> import os <EOL> import json <EOL> import time <EOL> from werkzeug . contrib . securecookie import SecureCookie <EOL> from . core import Middleware <EOL> DEFAULT_EXPIRY = <NUM_LIT> <EOL> NEVER = object ( ) <EOL> class JSONCookie ( SecureCookie ) : <EOL> serialization_method = json <EOL> class SignedCookieMiddleware ( Middleware ) : <EOL> def __init__ ( self , <EOL> arg_name = '<STR_LIT>' , <EOL> cookie_name = None , <EOL> secret_key = None , <EOL> domain = None , <EOL> path = '<STR_LIT:/>' , <EOL> secure = False , <EOL> http_only = False , <EOL> data_expiry = DEFAULT_EXPIRY , <EOL> cookie_expiry = None ) : <EOL> self . arg_name = arg_name <EOL> self . provides = ( arg_name , ) <EOL> if cookie_name is None : <EOL> cookie_name = '<STR_LIT>' % arg_name <EOL> self . cookie_name = cookie_name <EOL> self . secret_key = secret_key or self . _get_random ( ) <EOL> self . domain = domain <EOL> self . path = path <EOL> self . secure = secure <EOL> self . http_only = http_only <EOL> self . data_expiry = data_expiry <EOL> self . cookie_expiry = cookie_expiry <EOL> def request ( self , next , request ) : <EOL> cookie = JSONCookie . load_cookie ( request , <EOL> key = self . cookie_name , <EOL> secret_key = self . secret_key ) <EOL> response = next ( ** { self . arg_name : cookie } ) <EOL> if self . data_expiry is not NEVER : <EOL> cookie [ '<STR_LIT>' ] = time . time ( ) + self . data_expiry <EOL> cookie . save_cookie ( response , <EOL> key = self . cookie_name , <EOL> domain = self . domain , <EOL> path = self . path , <EOL> secure = self . secure , <EOL> httponly = self . http_only ) <EOL> return response <EOL> def _get_random ( self ) : <EOL> return os . urandom ( <NUM_LIT:20> ) <EOL> def __repr__ ( self ) : <EOL> cn = self . __class__ . __name__ <EOL> return ( '<STR_LIT>' <EOL> % ( cn , self . arg_name , self . cookie_name ) ) </s>
