import copy from tornado web import asynchronous from tornado auth imp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import copy
from tornado.web import asynchronous
from tornado.auth import GoogleMixin, TwitterMixin, FacebookMixin, FriendFeedMixin
from breeze.core.base import BaseHandler
from breeze.core.env import env
from breeze.utils.json import as_dict
class BaseAuthHandler(object):
@asynchronous
def _on_auth(self, user):
self.user = user
if not user:
raise tornado.web.HTTPError(500, 'Auth failed')
env.db.get(user['name'], self._on_server_accepted)
def _on_server_accepted(self, response):
result = as_dict(response.body)
if 'error' in result:
data = copy.copy(self.user)
data.update(dict(
_id = self.user['name'],
type = 'user'
))
env.db.put(data, self._on_create_user)
else:
user_id = result['_id']
self._on_login(user_id)
def _on_create_user(self, response):
result = as_dict(response.body)
user_id = result['id']
self._on_login(user_id)
def _on_login(self, user_id):
self.set_secure_cookie('user', str(user_id))
self.redirect(self.get_argument('next', '/'))
class AuthGoogleHandler(BaseHandler, BaseAuthHandler, GoogleMixin):
@asynchronous
def get(self):
if self.get_argument('openid.mode', None):
self.get_authenticated_user(self.async_callback(self._on_auth))
return
self.authenticate_redirect()
class AuthTwitterHandler(BaseHandler, BaseAuthHandler, TwitterMixin):
@asynchronous
def get(self):
if self.get_argument('oauth_token', None):
self.get_authenticated_user(self.async_callback(self._on_auth))
return
self.authenticate_redirect()
class AuthFacebookHandler(BaseHandler, BaseAuthHandler, FacebookMixin):
@asynchronous
def get(self):
if self.get_argument('auth_token', None):
self.get_authenticated_user(self.async_callback(self._on_auth))
return
self.authenticate_redirect()
class AuthFriendFeedHandler(BaseHandler, BaseAuthHandler, FriendFeedMixin):
@asynchronous
def get(self):
if self.get_argument('oauth_token', None):
self.get_authenticated_user(self.async_callback(self._on_auth))
return
self.authorize_redirect()
class AuthLogoutHandler(BaseHandler):
def get(self):
self.clear_cookie("user")
self.redirect(self.get_argument("next", "/"))