90 lines
2.9 KiB
Python
Executable File
90 lines
2.9 KiB
Python
Executable File
#!/usr/bin/env python
|
|
#
|
|
# Copyright 2009 Facebook
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import tornado.auth
|
|
import tornado.escape
|
|
import tornado.httpserver
|
|
import tornado.ioloop
|
|
import tornado.web
|
|
|
|
from tornado import gen
|
|
from tornado.options import define, options, parse_command_line
|
|
|
|
define("port", default=8888, help="run on the given port", type=int)
|
|
|
|
|
|
class Application(tornado.web.Application):
|
|
def __init__(self):
|
|
handlers = [
|
|
(r"/", MainHandler),
|
|
(r"/auth/login", AuthHandler),
|
|
(r"/auth/logout", LogoutHandler),
|
|
]
|
|
settings = dict(
|
|
cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
|
|
login_url="/auth/login",
|
|
)
|
|
tornado.web.Application.__init__(self, handlers, **settings)
|
|
|
|
|
|
class BaseHandler(tornado.web.RequestHandler):
|
|
def get_current_user(self):
|
|
user_json = self.get_secure_cookie("authdemo_user")
|
|
if not user_json: return None
|
|
return tornado.escape.json_decode(user_json)
|
|
|
|
|
|
class MainHandler(BaseHandler):
|
|
@tornado.web.authenticated
|
|
def get(self):
|
|
name = tornado.escape.xhtml_escape(self.current_user["name"])
|
|
self.write("Hello, " + name)
|
|
self.write("<br><br><a href=\"/auth/logout\">Log out</a>")
|
|
|
|
|
|
class AuthHandler(BaseHandler, tornado.auth.GoogleMixin):
|
|
@gen.coroutine
|
|
def get(self):
|
|
if self.get_argument("openid.mode", None):
|
|
user = yield self.get_authenticated_user()
|
|
self.set_secure_cookie("authdemo_user",
|
|
tornado.escape.json_encode(user))
|
|
self.redirect("/")
|
|
return
|
|
self.authenticate_redirect()
|
|
|
|
|
|
class LogoutHandler(BaseHandler):
|
|
def get(self):
|
|
# This logs the user out of this demo app, but does not log them
|
|
# out of Google. Since Google remembers previous authorizations,
|
|
# returning to this app will log them back in immediately with no
|
|
# interaction (unless they have separately logged out of Google in
|
|
# the meantime).
|
|
self.clear_cookie("authdemo_user")
|
|
self.write('You are now logged out. '
|
|
'Click <a href="/">here</a> to log back in.')
|
|
|
|
def main():
|
|
parse_command_line()
|
|
http_server = tornado.httpserver.HTTPServer(Application())
|
|
http_server.listen(options.port)
|
|
tornado.ioloop.IOLoop.instance().start()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|