Created
November 15, 2014 05:10
-
-
Save bmcculley/b34a902cc4a6452bff6f to your computer and use it in GitHub Desktop.
Some basic tornado authentication
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
#!/usr/bin/env python | |
import tornado.auth | |
import tornado.escape | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.options | |
import tornado.web | |
class BaseHandler(tornado.web.RequestHandler): | |
def get_current_user(self): | |
return self.get_secure_cookie("user") | |
class MainHandler(BaseHandler): | |
def get(self): | |
if not self.current_user: | |
self.redirect("/login") | |
return | |
name = tornado.escape.xhtml_escape(self.current_user) | |
self.write("Hello, " + name) | |
class LoginHandler(BaseHandler): | |
def get(self): | |
self.write('<html><body><form action="/login" method="post">' | |
'Name: <input type="text" name="name">' | |
'<input type="submit" value="Sign in">' | |
'</form></body></html>') | |
def post(self): | |
self.set_secure_cookie("user", self.get_argument("name")) | |
self.redirect("/") | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
(r"/login", LoginHandler), | |
], cookie_secret="78bfc7faf7f284f602f4df4948751be3") | |
if __name__ == "__main__": | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment