<s> """<STR_LIT>""" <EOL> import base64 <EOL> import hmac <EOL> import os <EOL> import time <EOL> from google . appengine . api import users <EOL> from simian import settings <EOL> XSRF_DELIMITER = '<STR_LIT>' <EOL> XSRF_VALID_TIME = <NUM_LIT> <EOL> def XsrfTokenGenerate ( action , user = None , timestamp = None ) : <EOL> """<STR_LIT>""" <EOL> if not user : <EOL> user = users . get_current_user ( ) . email ( ) <EOL> if not timestamp : <EOL> timestamp = time . time ( ) <EOL> timestr = str ( timestamp ) <EOL> try : <EOL> secret = settings . XSRF_SECRET <EOL> except AttributeError : <EOL> secret = os . urandom ( <NUM_LIT:16> ) . encode ( '<STR_LIT>' ) [ : <NUM_LIT:20> ] <EOL> settings . XSRF_SECRET = secret <EOL> secret = str ( secret ) <EOL> h = hmac . new ( secret , XSRF_DELIMITER . join ( [ user , action , timestr ] ) ) <EOL> return base64 . b64encode ( '<STR_LIT>' . join ( [ h . digest ( ) , XSRF_DELIMITER , timestr ] ) ) <EOL> def XsrfTokenValidate ( token , action , user = None , timestamp = None , time_ = time ) : <EOL> """<STR_LIT>""" <EOL> if not token : <EOL> return False <EOL> if not user : <EOL> user = users . get_current_user ( ) . email ( ) <EOL> if not timestamp : <EOL> try : <EOL> _ , timestr = base64 . b64decode ( token ) . split ( XSRF_DELIMITER , <NUM_LIT:1> ) <EOL> timestamp = float ( timestr ) <EOL> except ( ValueError , TypeError ) : <EOL> return False <EOL> if timestamp + XSRF_VALID_TIME < time_ . time ( ) : <EOL> return False <EOL> if token != XsrfTokenGenerate ( action , user , timestamp ) : <EOL> return False <EOL> return True </s>
