2010-10-07 26 views
8

剛剛開始玩Tornado並希望提供多種身份驗證方法。目前,我的應用使用tornado.auth.GoogleMixin與Google的混合OpenID/oAuth配合使用,未經身份驗證的用戶會自動發送到Google的身份驗證頁面。Tornado提供多種身份驗證選項

如果未經身份驗證的用戶想要使用其他選項(即。本地身份驗證或tornado.auth.TwitterMixin),我如何實現邏輯以在登錄處理程序中選擇身份驗證機制?

我說「tornado.web.authenticated」我所有的公開的方法裝飾,這裏是正在與谷歌的OpenID/OAuth的工作我的登錄處理程序類(從龍捲風的例子幾乎直):

class AuthLoginHandler(BaseHandler, tornado.auth.GoogleMixin): 
    @tornado.web.asynchronous 
    def get(self): 

     if self.get_argument('openid.mode', None): 
      self.get_authenticated_user(self.async_callback(self._on_auth)) 
      return 

     ## redirect after auth 
     self.authenticate_redirect() 

    def _on_auth(self, user): 
     ## auth fail 
     if not user: 
      raise tornado.web.HTTPError(500, 'Google auth failed') 

     ## auth success 
     identity = self.get_argument('openid.identity', None) 

     ## set identity in cookie 
     self.set_secure_cookie('identity', tornado.escape.json_encode(identity)) 
     self.redirect('/') 

感謝有關解決方案的任何建議。由於

回答

11

我認爲這樣做是將AuthLoginHandler更改爲更具體的東西,像GoogleAuthHandler,創造一個適當的路線最簡單的方法:

(r"/login/google/", GoogleAuthHandler), 
(r"/login/facebook/", FacebookAuthHandler), 

然後簡單創建鏈接到頁面上的每個身份驗證提供商ala:

<a href="/login/google/>Login with Google</a> 
<a href="/login/facebook/">Login with Facebook</a> 

如果您想讓它更有趣,可以將提供商作爲選擇或者如果你想獲得真正的幻想,你可以解析他們的'openid'URL(例如,如果username.google.com,self.redirect(「/ login/google」),但假設用戶知道他們的OpenID提供者網址通常不是這種情況。我想如果你給他們一個谷歌/臉譜/嘰嘰喳喳圖標或點擊,會混淆最少數量的人。

+0

感謝您的幫助。 – joet3ch 2010-10-13 18:22:49

0

我自己遇到了這個問題,但情況稍有不同。

一個解決方案實際上是做這樣的事情。

class AuthLoginHandler(BaseHandler, tornado.auth.GoogleMixin, tornado.auth.TwitterMixin): 

    def get(self): 
     if want_google: 
      tornado.auth.GoogleMixin.get_authenticated_user(self) 
      #... 
     elif want_twitter: 
      tornado.auth.TwitterMixin.get_authenticated_user(self) 
     #... 
相關問題