Skip to content

Instantly share code, notes, and snippets.

@FZambia
Created February 5, 2013 19:56
Show Gist options
  • Save FZambia/4717123 to your computer and use it in GitHub Desktop.
Save FZambia/4717123 to your computer and use it in GitHub Desktop.
tornado Persona auth handler
class PersonaAuthHandler(BaseHandler):
@tornado.web.asynchronous
def post(self):
assertion = self.get_argument('assertion')
http_client = tornado.httpclient.AsyncHTTPClient()
domain = 'localhost' # MAKE SURE YOU CHANGE THIS
url = 'https://browserid.org/verify'
data = {
'assertion': assertion,
'audience': domain,
}
response = http_client.fetch(
url,
method='POST',
body=urllib.urlencode(data),
callback=self.async_callback(self._on_response)
)
def _on_response(self, response):
data = tornado.escape.json_decode(response.body)
if data['status'] != 'okay':
raise tornado.web.HTTPError(400, "Failed assertion test")
email = data['email']
user, _created = User.objects.get_or_create(email=email)
self.set_secure_cookie('user', tornado.escape.json_encode(user.as_dict()))
self.finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment