def verifyredditcodeposted(request): code = request.POST['code'] sig = request.POST['sig'] if code != tornado_cookies.validate_cookie('code', sig): log.error("verifyredditcodeposted signature failed with (code, sig)", code, sig) raise ValueError('Invalid signature') content = get_reddit_content() comments = content[1]['data']['children'] comments_with_code = [] for c in comments: try: if code in c['data']['body']: comments_with_code.append(c) except KeyError: log.debug("verifyredditcodeposted could not parse comment", c) continue if not comments_with_code: log.debug("verifyredditcodeposted found no comment with code", code) return dict(success=False) comments_with_code.sort(key=lambda x:x['data']['created_utc']) original_comment = comments_with_code[0] username = original_comment['data']['author'] return dict(success=True, username=username, usersig=tornado_cookies.generate_secure_cookie('username', username))
def clean(self): username = self.cleaned_data['username'] usersig = self.cleaned_data['usersig'] if username != tornado_cookies.validate_cookie('username', usersig): raise forms.ValidationError('Invalid signature -- please start over :(') return self.cleaned_data