~vishvananda/nova/network-refactor

« back to all changes in this revision

Viewing changes to vendor/tornado/demos/auth/authdemo.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright 2009 Facebook
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
# not use this file except in compliance with the License. You may obtain
 
7
# a copy of the License at
 
8
#
 
9
#     http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
# License for the specific language governing permissions and limitations
 
15
# under the License.
 
16
 
 
17
import tornado.auth
 
18
import tornado.escape
 
19
import tornado.httpserver
 
20
import tornado.ioloop
 
21
import tornado.options
 
22
import tornado.web
 
23
 
 
24
from tornado.options import define, options
 
25
 
 
26
define("port", default=8888, help="run on the given port", type=int)
 
27
 
 
28
 
 
29
class Application(tornado.web.Application):
 
30
    def __init__(self):
 
31
        handlers = [
 
32
            (r"/", MainHandler),
 
33
            (r"/auth/login", AuthHandler),
 
34
        ]
 
35
        settings = dict(
 
36
            cookie_secret="32oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
 
37
            login_url="/auth/login",
 
38
        )
 
39
        tornado.web.Application.__init__(self, handlers, **settings)
 
40
 
 
41
 
 
42
class BaseHandler(tornado.web.RequestHandler):
 
43
    def get_current_user(self):
 
44
        user_json = self.get_secure_cookie("user")
 
45
        if not user_json: return None
 
46
        return tornado.escape.json_decode(user_json)
 
47
 
 
48
 
 
49
class MainHandler(BaseHandler):
 
50
    @tornado.web.authenticated
 
51
    def get(self):
 
52
        name = tornado.escape.xhtml_escape(self.current_user["name"])
 
53
        self.write("Hello, " + name)
 
54
 
 
55
 
 
56
class AuthHandler(BaseHandler, tornado.auth.GoogleMixin):
 
57
    @tornado.web.asynchronous
 
58
    def get(self):
 
59
        if self.get_argument("openid.mode", None):
 
60
            self.get_authenticated_user(self.async_callback(self._on_auth))
 
61
            return
 
62
        self.authenticate_redirect()
 
63
    
 
64
    def _on_auth(self, user):
 
65
        if not user:
 
66
            raise tornado.web.HTTPError(500, "Google auth failed")
 
67
        self.set_secure_cookie("user", tornado.escape.json_encode(user))
 
68
        self.redirect("/")
 
69
 
 
70
 
 
71
def main():
 
72
    tornado.options.parse_command_line()
 
73
    http_server = tornado.httpserver.HTTPServer(Application())
 
74
    http_server.listen(options.port)
 
75
    tornado.ioloop.IOLoop.instance().start()
 
76
 
 
77
 
 
78
if __name__ == "__main__":
 
79
    main()