Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 817433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:59:34+00:00 2026-05-15T01:59:34+00:00

I need to write authentication function with asynchronous callback from remote Auth API. Simple

  • 0

I need to write authentication function with asynchronous callback from remote Auth API. Simple authentication with login is working well, but authorization with cookie key, does not work. It should checks if in cookies present key “lp_login”, fetch API url like async and execute on_response function.

The code almost works, but I see two problems. First, in on_response function I need to setup secure cookie for authorized user on every page. In code user_id returns correct ID, but line: self.set_secure_cookie(“user”, user_id) does’t work. Why it can be?

And second problem. During async fetch API url, user’s page has loaded before on_response setup cookie with key “user” and the page will has an unauthorized section with link to login or sign on. It will be confusing for users. To solve it, I can stop loading page for user who trying to load first page of site. Is it possible to do and how? Maybe the problem has more correct way to solve it?

class BaseHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get_current_user(self):
        user_id = self.get_secure_cookie("user")
        user_cookie = self.get_cookie("lp_login")
        if user_id:
            self.set_secure_cookie("user", user_id)
            return Author.objects.get(id=int(user_id))
        elif user_cookie:
            url = urlparse("http://%s" % self.request.host)
            domain = url.netloc.split(":")[0]
            try:
                username, hashed_password = urllib.unquote(user_cookie).rsplit(',',1)
            except ValueError:
                # check against malicious clients
                return None
            else:
                url = "http://%s%s%s/%s/" % (domain, "/api/user/username/", username, hashed_password)
                http = tornado.httpclient.AsyncHTTPClient()
                http.fetch(url, callback=self.async_callback(self.on_response))
        else:
            return None

    def on_response(self, response):
        answer = tornado.escape.json_decode(response.body)
        username = answer['username']
        if answer["has_valid_credentials"]:
            author = Author.objects.get(email=answer["email"])
            user_id = str(author.id)
            print user_id # It returns needed id
            self.set_secure_cookie("user", user_id) # but session can's setup
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:59:36+00:00Added an answer on May 15, 2026 at 1:59 am

    It seems you cross-posted this on the tornado mailing list here

    One of the problems you are running into is that you can’t start the async call inside of get_current_user, you can only start an async call from something that happens inside of get or post.

    I’ve not tested it, but i think this should get you close to what you are looking for.

    #!/bin/python
    import tornado.web
    import tornado.http
    import tornado.escape
    import functools
    import logging
    import urllib
    
    import Author
    
    def upgrade_lp_login_cookie(method):
        @functools.wraps(method)
        def wrapper(self, *args, **kwargs):
            if not self.current_user and self.get_cookie('lp_login'):
                self.upgrade_lp_login(self.async_callback(method, self, *args, **kwargs))
            else:
                return method(self, *args, **kwargs)
        return wrapper
    
    
    class BaseHandler(tornado.web.RequestHandler):
        def get_current_user(self):
            user_id = self.get_secure_cookie("user")
            if user_id:
                return Author.objects.get(id=int(user_id))
    
        def upgrade_lp_login(self, callback):
            lp_login = self.get_cookie("lp_login")
            try:
                username, hashed_password = urllib.unquote(lp_login).rsplit(',',1)
            except ValueError:
                # check against malicious clients
                logging.info('invalid lp_login cookie %s' % lp_login)
                return callback()
    
            url = "http://%(host)s/api/user/username/%s/%s" % (self.request.host, 
                                                            urllib.quote(username), 
                                                            urllib.quote(hashed_password))
            http = tornado.httpclient.AsyncHTTPClient()
            http.fetch(url, self.async_callback(self.finish_upgrade_lp_login, callback))
    
        def finish_upgrade_lp_login(self, callback, response):
            answer = tornado.escape.json_decode(response.body)
            # username = answer['username']
            if answer['has_valid_credentials']:
                # set for self.current_user, overriding previous output of self.get_current_user()
                self._current_user = Author.objects.get(email=answer["email"])
                # set the cookie for next request
                self.set_secure_cookie("user", str(self.current_user.id))
    
            # now chain to the real get/post method
            callback()
    
        @upgrade_lp_login_cookie
        def get(self):
            self.render('template.tmpl')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 422k
  • Answers 422k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 1 var stats = from st in store.Stats join b… May 15, 2026 at 11:13 am
  • Editorial Team
    Editorial Team added an answer In Ruby 1.9, you have Method#source_location: require 'yaml' p YAML.method(:load).source_location… May 15, 2026 at 11:13 am
  • Editorial Team
    Editorial Team added an answer It looks like there is an ugly workaround for this,… May 15, 2026 at 11:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.