Skip to content

Instantly share code, notes, and snippets.

@dmitric
Created January 17, 2013 04:54
Show Gist options
  • Save dmitric/4553767 to your computer and use it in GitHub Desktop.
Save dmitric/4553767 to your computer and use it in GitHub Desktop.
Old examples of auth in tornado
class FacebookGraphLoginHandler(BaseHandler, tornado.auth.FacebookGraphMixin):
@tornado.web.asynchronous
def get(self):
my_url = (self.request.protocol + "://" + self.request.host +"/login/facebook?next="+tornado.escape.url_escape(self.get_argument("next", "/")))
if self.get_argument("code", False):
self.get_authenticated_user(redirect_uri=my_url,client_id=self.settings["facebook_api_key"],client_secret=self.settings["facebook_secret"],code=self.get_argument("code"),callback=self._on_auth,extra_fields=['email'])
return
self.authorize_redirect(redirect_uri=my_url,client_id=self.settings["facebook_api_key"],extra_params={"scope": "read_stream,email,offline_access"})
def _on_auth(self, user):
if not user:
raise tornado.web.HTTPError(500, "Facebook auth failed")
facebook_id = long(user['id'])
login_info = self.logins.find_one({'facebook_id': facebook_id})
if not login_info:
#generate a user_id
user_id = ObjectId()
timestamp = datetime.datetime.utcnow()
temp_username = "{0}{1}{2}".format(re.sub("[\W\d]+", "", user['first_name'].lower().strip()), user['last_name'][0].lower(),unicode(uuid.uuid4())[:5])
new_user = {"_id": user_id,"name": user['name'],"first_name": user['first_name'],"last_name": user['last_name'],'facebook_access_token': user['access_token'],'created': timestamp, 'updated': timestamp, 'username': temp_username,'role':'user', 'email': user['email']}
self.users.insert(new_user, safe=True)
self.logins.insert({"user_id": user_id, 'facebook_id': facebook_id, 'email': user['email'],'created':timestamp, 'updated': timestamp,'username': temp_username}, safe=True)
self.set_secure_cookie(options.cookie_user, pickle.dumps(new_user))
self.redirect(self.get_argument("next", "/"))
else:
user_info = self.users.find_one({'_id': login_info['user_id']})
self.set_secure_cookie(options.cookie_user, pickle.dumps(user_info))
self.redirect(self.get_argument("next", "/"))
class TwitterHandler(BaseHandler, tornado.auth.TwitterMixin):
@tornado.web.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()
def _on_auth(self, user):
if not user:
raise tornado.web.HTTPError(500, "Twitter auth failed")
twitter_id = user['id']
login_info = self.logins.find_one({'twitter_id': twitter_id})
if not login_info:
user_id = ObjectId()
timestamp = datetime.datetime.utcnow()
names = user['name'].split()
last_name = None
if len(names) > 1:
last_name = names[1]
first_name = names[0]
temp_username = '@{0}'.format(user['username'])
new_user = {"_id": user_id,"name": user['name'],
"first_name": first_name,
"last_name": last_name,
'twitter_access_token': user['access_token'],
'created': timestamp,
'updated': timestamp,
'username': temp_username,
'role':'user'}
self.users.insert(new_user, safe=True)
self.logins.insert({"user_id": user_id,
'twitter_id': twitter_id,
'created': timestamp,
'updated': timestamp,
'username': temp_username}, safe=True)
self.set_secure_cookie(options.cookie_user, pickle.dumps(new_user))
self.redirect(self.get_argument("next", "/"))
else:
user_info = self.users.find_one({'_id': login_info['user_id']})
self.set_secure_cookie(options.cookie_user, pickle.dumps(user_info))
self.redirect(self.get_argument("next", "/"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment