Tornado 的多种身份验证选项

发布于 2024-09-26 05:36:32 字数 1081 浏览 10 评论 0原文

刚刚开始使用 Tornado,希望提供多种身份验证方法。目前,我的应用程序使用tornado.auth.GoogleMixin 与Google 的混合OpenID/oAuth 运行良好,未经身份验证的用户会自动发送到Google 的身份验证页面。

如果未经身份验证的用户想要使用其他选项(即本地身份验证或tornado.auth.TwitterMixin),我如何实现在登录处理程序中选择身份验证机制的逻辑?

我将装饰器“tornado.web.authenticated”添加到所有公开的方法中,这是我的登录处理程序类(几乎直接来自 Tornado 示例),它当前正在与 Google 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('/')

感谢任何关于解决方案。谢谢

Just started playing with Tornado and want to offer multiple methods of authentication. Currently my app is working fine with Google's hybrid OpenID/oAuth using tornado.auth.GoogleMixin and the unauthenticated users are automatically sent to Google's auth page.

If an unauthenticated user wants to use another option (ie. local auth or tornado.auth.TwitterMixin), how can I implement the logic to choose an auth mechanism within the login handler?

I added the decorator 'tornado.web.authenticated' to all of my exposed methods, and here is the my login handler class (pretty much straight from the Tornado examples) which is currently working with Google 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('/')

Appreciate any suggestions for a solution. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤芳又自赏 2024-10-03 05:36:32

我认为最简单的方法是将 AuthLoginHandler 更改为更具体的内容,例如 GoogleAuthHandler,并为此创建适当的路由:

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

等等。

然后只需在页面上创建

<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 提供商 URL,但我猜如果您给他们一个 google / facebook / twitter 图标或点击的东西,那通常不会让大多数人感到困惑。

I think the easiest way to do it would be to change the AuthLoginHandler to something more specific, like GoogleAuthHandler, and create an appropriate route for that:

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

etc.

Then simply create links to each authentication provider on the page ala:

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

If you wanted to make it fancier, you could provide the providers as a select box, or if you wanted to get REALLY fancy, you could parse their 'openid' URL (e.g., if username.google.com, self.redirect("/login/google"), but that assumes that users know their OpenID provider URLs, which is usually not the case. I'd guess if you gave them a google / facebook / twitter icon or something to click on that would confuse the least number of people.

千柳 2024-10-03 05:36:32

我自己也遇到过这个问题,但情况略有不同。

一种解决方案实际上是做这样的事情。

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)
        #...

I came upon this problem myself but in a slightly different circumstance.

One solution is actually to do something like this.

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)
        #...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文