<s> """<STR_LIT>""" <EOL> from google . appengine . api import urlfetch <EOL> from werkzeug import url_encode <EOL> from wtforms . validators import * <EOL> from wtforms . validators import ValidationError <EOL> from tipfy import current_handler <EOL> RECAPTCHA_VERIFY_SERVER = '<STR_LIT>' <EOL> class CsrfToken ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , values , message = None ) : <EOL> self . values = values <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> if field . csrf_token : <EOL> if field . csrf_token not in self . values : <EOL> if self . message is None : <EOL> self . message = '<STR_LIT>' <EOL> raise ValueError ( self . message ) <EOL> else : <EOL> self . values . remove ( field . csrf_token ) <EOL> class Recaptcha ( object ) : <EOL> """<STR_LIT>""" <EOL> _error_codes = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , message = u'<STR_LIT>' ) : <EOL> self . message = message <EOL> def __call__ ( self , form , field ) : <EOL> request = current_handler . request <EOL> challenge = request . form . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> response = request . form . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> remote_ip = request . remote_addr <EOL> if not challenge or not response : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> if not self . _validate_recaptcha ( challenge , response , remote_ip ) : <EOL> field . recaptcha_error = '<STR_LIT>' <EOL> raise ValidationError ( self . message ) <EOL> def _validate_recaptcha ( self , challenge , response , remote_addr ) : <EOL> """<STR_LIT>""" <EOL> private_key = current_handler . get_config ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> result = urlfetch . fetch ( url = RECAPTCHA_VERIFY_SERVER , <EOL> method = urlfetch . POST , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } , <EOL> payload = url_encode ( { <EOL> '<STR_LIT>' : private_key , <EOL> '<STR_LIT>' : remote_addr , <EOL> '<STR_LIT>' : challenge , <EOL> '<STR_LIT>' : response <EOL> } ) ) <EOL> if result . status_code != <NUM_LIT:200> : <EOL> return False <EOL> rv = [ l . strip ( ) for l in result . content . splitlines ( ) ] <EOL> if rv and rv [ <NUM_LIT:0> ] == '<STR_LIT:true>' : <EOL> return True <EOL> if len ( rv ) > <NUM_LIT:1> : <EOL> error = rv [ <NUM_LIT:1> ] <EOL> if error in self . _error_codes : <EOL> raise RuntimeError ( self . _error_codes [ error ] ) <EOL> return False </s>
