Pregunta

Acabo de comenzar a jugar con Tornado y quiero ofrecer múltiples métodos de autenticación. Actualmente, mi aplicación funciona bien con el híbrido OpenID/OAuth de Google usando tornado.auth.googlemixin y los usuarios no autenticados se envían automáticamente a la página de autenticación de Google.

Si un usuario no autenticado quiere usar otra opción (es decir, auth o tornado.auth.twittermixin), ¿cómo puedo implementar la lógica para elegir un mecanismo de autenticación dentro del controlador de inicio de sesión?

Agregué el decorador 'tornado.web.authenticated' a todos mis métodos expuestos, y aquí está la clase My Login Handler (prácticamente directamente de los ejemplos de tornados) que actualmente está trabajando con 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('/')

Aprecio cualquier sugerencia para una solución. Gracias

¿Fue útil?

Solución

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.

Otros consejos

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)
        #...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top