#NEWLINE# def generate_csrf( secret_key = None , time_limit = None ) : #NEWLINE# #INDENT# 'Generate csrf token code.\n\n    :param secret_key: A secret key for mixing in the token,\n                       default is Flask.secret_key.\n    :param time_limit: Token valid in the time limit,\n                       default is 3600s.\n    ' #NEWLINE# if ( not secret_key ) : #NEWLINE# #INDENT# secret_key = current_app . config . get ( 'WTF_CSRF_SECRET_KEY' , current_app . secret_key ) #NEWLINE# #UNINDENT# if ( not secret_key ) : #NEWLINE# #INDENT# raise Exception ( 'Must provide secret_key to use csrf.' ) #NEWLINE# #UNINDENT# if ( time_limit is None ) : #NEWLINE# #INDENT# time_limit = current_app . config . get ( 'WTF_CSRF_TIME_LIMIT' , 3600 ) #NEWLINE# #UNINDENT# if ( 'csrf_token' not in session ) : #NEWLINE# #INDENT# session [ 'csrf_token' ] = hashlib . sha1 ( os . urandom ( 64 ) ) . hexdigest ( ) #NEWLINE# #UNINDENT# if time_limit : #NEWLINE# #INDENT# expires = ( time . time ( ) + time_limit ) #NEWLINE# csrf_build = ( '%s%s' % ( session [ 'csrf_token' ] , expires ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# expires = '' #NEWLINE# csrf_build = session [ 'csrf_token' ] #NEWLINE# #UNINDENT# hmac_csrf = hmac . new ( to_bytes ( secret_key ) , to_bytes ( csrf_build ) , digestmod = hashlib . sha1 ) . hexdigest ( ) #NEWLINE# return ( '%s##%s' % ( expires , time_limit ) )