<s> """<STR_LIT>""" <EOL> import base64 <EOL> import exceptions <EOL> import hmac <EOL> import json <EOL> import logging <EOL> import time <EOL> from google . appengine . api import mail <EOL> from google . appengine . ext import deferred <EOL> from cauliflowervest . server import crypto <EOL> from cauliflowervest . server import models <EOL> from cauliflowervest . server import settings <EOL> JSON_PREFIX = "<STR_LIT>" <EOL> XSRF_DELIMITER = '<STR_LIT>' <EOL> XSRF_VALID_TIME = <NUM_LIT> <EOL> def _Send ( recipients , subject , body , sender , reply_to , bcc_recipients ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> message = mail . EmailMessage ( <EOL> to = recipients , <EOL> sender = sender or settings . DEFAULT_EMAIL_SENDER , <EOL> subject = subject , <EOL> body = body ) <EOL> if reply_to or settings . DEFAULT_EMAIL_REPLY_TO : <EOL> message . reply_to = reply_to or settings . DEFAULT_EMAIL_REPLY_TO <EOL> except mail . InvalidEmailError : <EOL> logging . warning ( '<STR_LIT>' ) <EOL> return <EOL> if bcc_recipients : <EOL> message . bcc = bcc_recipients <EOL> message . send ( ) <EOL> def SendEmail ( recipients , subject , body , sender = None , reply_to = None , <EOL> bcc_recipients = None , defer = True ) : <EOL> """<STR_LIT>""" <EOL> if settings . DEVELOPMENT : <EOL> logging . warning ( '<STR_LIT>' ) <EOL> logging . info ( <EOL> '<STR_LIT>' , <EOL> recipients , subject , body ) <EOL> else : <EOL> if defer : <EOL> deferred . defer ( <EOL> _Send , recipients , subject , body , sender , reply_to , bcc_recipients ) <EOL> else : <EOL> _Send ( recipients , subject , body , sender , reply_to , bcc_recipients ) <EOL> def XsrfTokenGenerate ( action , user = None , timestamp = None ) : <EOL> """<STR_LIT>""" <EOL> if not user : <EOL> user = models . GetCurrentUser ( ) . email <EOL> if not timestamp : <EOL> timestamp = time . time ( ) <EOL> timestr = str ( timestamp ) <EOL> secret = crypto . ENCRYPTION_KEY_TYPES [ settings . KEY_TYPE_DEFAULT_XSRF ] ( ) <EOL> h = hmac . new ( secret , XSRF_DELIMITER . join ( ( user , action , timestr ) ) ) <EOL> return base64 . urlsafe_b64encode ( <EOL> '<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 = models . GetCurrentUser ( ) . email <EOL> if not timestamp : <EOL> try : <EOL> token = token . encode ( '<STR_LIT:utf-8>' ) <EOL> _ , timestr = base64 . urlsafe_b64decode ( token ) . split ( XSRF_DELIMITER , <NUM_LIT:1> ) <EOL> timestamp = float ( timestr ) <EOL> except ValueError : <EOL> logging . exception ( '<STR_LIT>' , token ) <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 <EOL> def ToSafeJson ( obj ) : <EOL> """<STR_LIT>""" <EOL> return JSON_PREFIX + json . dumps ( obj ) <EOL> def FromSafeJson ( data ) : <EOL> """<STR_LIT>""" <EOL> if not data . startswith ( JSON_PREFIX ) : <EOL> raise exceptions . ValueError <EOL> return json . loads ( data [ len ( JSON_PREFIX ) : ] ) </s>
