Exemple #1
0
def before_request():
    """
    Store API anonymous cookie in session or if it exists, check if it has expired
    """
    g.request_start_time = time.time()
    g.request_time = lambda: "{:.3f}s".format(
        (time.time() - g.request_start_time))
    if 'cookies' not in session:
        session['cookies'] = {}
    if 'auth' not in session or is_expired(session['auth']):
        session['auth'] = api.bootstrap()
Exemple #2
0
def before_request():
    """
    Store API anonymous cookie in session or if it exists, check if it has expired
    """
    if request.path in ('/ready', '/alive'):
        # Do not bootstrap readiness/liveness probes
        return
    g.request_start_time = time.time()
    g.request_time = lambda: "{:.3f}s".format(
        (time.time() - g.request_start_time))
    if 'cookies' not in session:
        session['cookies'] = {}
    if request.cookies.get('session'):
        # Re-use BBB session, if it is valid, the same BBB token will be returned by bootstrap
        # thus if the user was authenticated, it will use the user token
        session['cookies']['session'] = request.cookies.get('session')
    if 'auth' not in session or is_expired(session['auth']):
        user_agent = request.headers.get('User-Agent')
        remote_ip = get_remote_address()
        #### For testing purposes:
        #user_agent = "Googlebot"
        #remote_ip = "66.249.66.1" # crawl-66-249-66-1.googlebot.com.
        #user_agent = "DuckDuckBot"
        #remote_ip = "50.16.241.117"
        #remote_ip = "127.0.0.1"
        evaluation = crawlers.evaluate(remote_ip, user_agent)
        if evaluation == crawlers.VERIFIED_BOT:
            # Extremely high rate limit
            session['auth'] = {
                'access_token': app.config['VERIFIED_BOTS_ACCESS_TOKEN'],
                'expire_in': "2050-01-01T00:00:00",
                'bot': True
            }
        elif evaluation == crawlers.UNVERIFIABLE_BOT:
            # Slightly higher rate limit
            session['auth'] = {
                'access_token': app.config['UNVERIFIABLE_BOTS_ACCESS_TOKEN'],
                'expire_in': "2050-01-01T00:00:00",
                'bot': True
            }
        elif evaluation == crawlers.POTENTIAL_MALICIOUS_BOT:
            # Rate limits as a regular user with the advantage that there is no bootstrap
            session['auth'] = {
                'access_token': app.config['MALICIOUS_BOTS_ACCESS_TOKEN'],
                'expire_in': "2050-01-01T00:00:00",
                'bot': True
            }
        else:
            session['auth'] = api.bootstrap()
Exemple #3
0
def before_request():
    """
    Store API anonymous cookie in session or if it exists, check if it has expired
    """
    if request.path in ('/ready', '/alive'):
        # Do not bootstrap readiness/liveness probes
        return
    g.request_start_time = time.time()
    g.request_time = lambda: "{:.3f}s".format((time.time() - g.request_start_time))
    if 'cookies' not in session:
        session['cookies'] = {}

    if 'auth' not in session or is_expired(session['auth']):
        user_agent = request.headers.get('User-Agent')
        remote_ip = get_remote_address()
        #### For testing purposes:
        #user_agent = "Googlebot"
        #remote_ip = "66.249.66.1" # crawl-66-249-66-1.googlebot.com.
        #user_agent = "DuckDuckBot"
        #remote_ip = "50.16.241.117"
        #remote_ip = "127.0.0.1"
        evaluation = crawlers.evaluate(remote_ip, user_agent)
        if evaluation == crawlers.VERIFIED_BOT:
            # Extremely high rate limit
            RequestsManager.init(auth={'access_token': app.config['VERIFIED_BOTS_ACCESS_TOKEN'], 'expire_in': "2050-01-01T00:00:00", 'bot': True}, cookies={})
        elif evaluation == crawlers.UNVERIFIABLE_BOT:
            # Slightly higher rate limit
            RequestsManager.init(auth={'access_token': app.config['UNVERIFIABLE_BOTS_ACCESS_TOKEN'], 'expire_in': "2050-01-01T00:00:00", 'bot': True}, cookies={})
        elif evaluation == crawlers.POTENTIAL_MALICIOUS_BOT:
            # Rate limits as a regular user with the advantage that there is no bootstrap
            RequestsManager.init(auth={'access_token': app.config['MALICIOUS_BOTS_ACCESS_TOKEN'], 'expire_in': "2050-01-01T00:00:00", 'bot': True}, cookies={})

    if not RequestsManager.is_initialized():
        if request.cookies.get('session'):
            # - Re-use BBB session, if it is valid, the same BBB token will be returned by bootstrap
            # thus if the user was authenticated, it will use the user token
            # - Always bootstrap, otherwise the browser may end up logged in with different
            # users in BBB and core
            # - Ignore any previous bootstrapped access token
            RequestsManager.init(auth={}, cookies={'session': request.cookies.get('session')})
        elif 'auth' not in session:
            # No BBB or core session, API will bootstrap
            RequestsManager.init(auth={}, cookies={})
        else:
            # We have a core session and no BBB session, this is the only situation
            # API will not bootstrap
            RequestsManager.init(auth=session['auth'], cookies={})