<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import base64 <EOL> import json <EOL> import time <EOL> import zlib <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils import baseconv <EOL> from django . utils . crypto import constant_time_compare , salted_hmac <EOL> from django . utils . encoding import force_bytes , force_str , force_text <EOL> from django . utils . importlib import import_module <EOL> class BadSignature ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class SignatureExpired ( BadSignature ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def b64_encode ( s ) : <EOL> return base64 . urlsafe_b64encode ( s ) . strip ( b'<STR_LIT:=>' ) <EOL> def b64_decode ( s ) : <EOL> pad = b'<STR_LIT:=>' * ( - len ( s ) % <NUM_LIT:4> ) <EOL> return base64 . urlsafe_b64decode ( s + pad ) <EOL> def base64_hmac ( salt , value , key ) : <EOL> return b64_encode ( salted_hmac ( salt , value , key ) . digest ( ) ) <EOL> def get_cookie_signer ( salt = '<STR_LIT>' ) : <EOL> modpath = settings . SIGNING_BACKEND <EOL> module , attr = modpath . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> try : <EOL> mod = import_module ( module ) <EOL> except ImportError as e : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' % ( modpath , e ) ) <EOL> try : <EOL> Signer = getattr ( mod , attr ) <EOL> except AttributeError as e : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' % ( modpath , e ) ) <EOL> return Signer ( '<STR_LIT>' + settings . SECRET_KEY , salt = salt ) <EOL> class JSONSerializer ( object ) : <EOL> """<STR_LIT>""" <EOL> def dumps ( self , obj ) : <EOL> return json . dumps ( obj , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) . encode ( '<STR_LIT>' ) <EOL> def loads ( self , data ) : <EOL> return json . loads ( data . decode ( '<STR_LIT>' ) ) <EOL> def dumps ( obj , key = None , salt = '<STR_LIT>' , serializer = JSONSerializer , compress = False ) : <EOL> """<STR_LIT>""" <EOL> data = serializer ( ) . dumps ( obj ) <EOL> is_compressed = False <EOL> if compress : <EOL> compressed = zlib . compress ( data ) <EOL> if len ( compressed ) < ( len ( data ) - <NUM_LIT:1> ) : <EOL> data = compressed <EOL> is_compressed = True <EOL> base64d = b64_encode ( data ) <EOL> if is_compressed : <EOL> base64d = b'<STR_LIT:.>' + base64d <EOL> return TimestampSigner ( key , salt = salt ) . sign ( base64d ) <EOL> def loads ( s , key = None , salt = '<STR_LIT>' , serializer = JSONSerializer , max_age = None ) : <EOL> """<STR_LIT>""" <EOL> base64d = force_bytes ( TimestampSigner ( key , salt = salt ) . unsign ( s , max_age = max_age ) ) <EOL> decompress = False <EOL> if base64d [ : <NUM_LIT:1> ] == b'<STR_LIT:.>' : <EOL> base64d = base64d [ <NUM_LIT:1> : ] <EOL> decompress = True <EOL> data = b64_decode ( base64d ) <EOL> if decompress : <EOL> data = zlib . decompress ( data ) <EOL> return serializer ( ) . loads ( data ) <EOL> class Signer ( object ) : <EOL> def __init__ ( self , key = None , sep = '<STR_LIT::>' , salt = None ) : <EOL> self . sep = str ( sep ) <EOL> self . key = str ( key or settings . SECRET_KEY ) <EOL> self . salt = str ( salt or <EOL> '<STR_LIT>' % ( self . __class__ . __module__ , self . __class__ . __name__ ) ) <EOL> def signature ( self , value ) : <EOL> signature = base64_hmac ( self . salt + '<STR_LIT>' , value , self . key ) <EOL> return force_str ( signature ) <EOL> def sign ( self , value ) : <EOL> value = force_str ( value ) <EOL> return str ( '<STR_LIT>' ) % ( value , self . sep , self . signature ( value ) ) <EOL> def unsign ( self , signed_value ) : <EOL> signed_value = force_str ( signed_value ) <EOL> if not self . sep in signed_value : <EOL> raise BadSignature ( '<STR_LIT>' % self . sep ) <EOL> value , sig = signed_value . rsplit ( self . sep , <NUM_LIT:1> ) <EOL> if constant_time_compare ( sig , self . signature ( value ) ) : <EOL> return force_text ( value ) <EOL> raise BadSignature ( '<STR_LIT>' % sig ) <EOL> class TimestampSigner ( Signer ) : <EOL> def timestamp ( self ) : <EOL> return baseconv . base62 . encode ( int ( time . time ( ) ) ) <EOL> def sign ( self , value ) : <EOL> value = force_str ( value ) <EOL> value = str ( '<STR_LIT>' ) % ( value , self . sep , self . timestamp ( ) ) <EOL> return super ( TimestampSigner , self ) . sign ( value ) <EOL> def unsign ( self , value , max_age = None ) : <EOL> result = super ( TimestampSigner , self ) . unsign ( value ) <EOL> value , timestamp = result . rsplit ( self . sep , <NUM_LIT:1> ) <EOL> timestamp = baseconv . base62 . decode ( timestamp ) <EOL> if max_age is not None : <EOL> age = time . time ( ) - timestamp <EOL> if age > max_age : <EOL> raise SignatureExpired ( <EOL> '<STR_LIT>' % ( age , max_age ) ) <EOL> return value </s>
