3

I am having an issue where I have a page (A) that redirects to page (B) after doing some work in a flask websocket. Page (B) has another websocket that does more lengthy work 10-20 minutes and then has a button to continue to the third page (C) that has some results. The issue i'm facing is when on page (B), and the work has completed and i'm ready to click the button to continue to page (C), If enough time has passed (15+ min, unsure exactly), when I click the button it refreshes to page (A) and all my data is gone. If a reasonably short amount of time has passed the page continues to page (C) just fine. I need to extend this time to several hours preferably.

I am unsure what is wrong so I have tried modifying the session data in my before_request

from datetime import datetime, timedelta, timezone
from flask import redirect, request, session, url_for, jsonify, g
from config import *
from dateutil import parser


#  Page Functions
@app.before_request
def before_request():
    # function runs before the request for a page to see if the user is logged in or not

    session['fresh'] = True
    g.user = None
    g.ldap_groups = None
    if 'user_id' in session:
        g.user = session['user_id']
    if 'samlUserdata' in session:
        if 'ldapGroups' in session['samlUserdata']:
            g.ldap_groups = session['samlUserdata']['ldapGroups']
    if 'samlExpiration' in session and request.endpoint != 'login':
        if session['samlExpiration'] < datetime.now():
            session.clear()
            return redirect(url_for('login'))
        if 'IdentityExpires' in session:
            now = datetime.now(timezone.utc)
            if parser.parse(session['IdentityExpires']) < now:
                session.clear()
                return redirect(url_for('login'))
        else:
            session['samlExpiration'] = datetime.now() + timedelta(minutes=SESSION_LENGTH)
        # Manage session timeout
    session.permanent = True
    session.permanent_session_lifetime = timedelta(days=1)
    session.modified = True

But this does not fix the problem.

my instantiation of flask is as follows:

from flask import Flask
from flask_socketio import SocketIO
from config import *

app = Flask(__name__)
app.config.from_object('config')
socketio = SocketIO(app,
                    async_mode=async_mode,
                    message_queue=app.config['CELERY_BROKER_URL'],
                    ping_timeout=SOCKETIO_PING_TIMEOUT,
                    ping_interval=SOCKETIO_PING_INTERVAL)

Any guidance would be appreciated!

user1601716
  • 1,893
  • 4
  • 24
  • 53

1 Answers1

3

I figured out how to resolve this based on the before_request function. My logic was wrong.

here is the new function that made it work for me.

@app.before_request
def before_request():
    # function runs before the request for a page to see if the user is logged in or not

    session['fresh'] = True
    g.user = None
    g.ldap_groups = None
    if 'user_id' in session:
        g.user = session['user_id']
    if 'samlUserdata' in session:
        if 'ldapGroups' in session['samlUserdata']:
            g.ldap_groups = session['samlUserdata']['ldapGroups']
    if 'samlExpiration' in session and request.endpoint != 'login':
        if session['samlExpiration'] < datetime.now():
            session.clear()
            return redirect(url_for('login'))
        if 'IdentityExpires' in session:
            now = datetime.now(timezone.utc)
            if parser.parse(session['IdentityExpires']) < now:
                session.clear()
                return redirect(url_for('login'))
    else:
        session['samlExpiration'] = datetime.now() + timedelta(minutes=SESSION_LENGTH)
user1601716
  • 1,893
  • 4
  • 24
  • 53