Created
February 5, 2013 19:56
-
-
Save FZambia/4717123 to your computer and use it in GitHub Desktop.
tornado Persona auth handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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