Python Flask会话:每个HTTPS请求的新会话

时间:2019-03-06 09:17:44

标签: python session flask onelogin

我正在通过一次登录集成我的Web应用程序。第一次,当用户登陆到我的主页时,我重定向到一个登录名,任何后续重定向都基于当前会话或不存在会话。

对于每个新请求,会话检查似乎为空,并且一直循环进入onelogin。

注意:一切都在我的本地环境中运行,因为它只是一个HTTP请求。部署到AWS开发环境中的相同对象存在循环到一个登录名的问题。循环大约一分钟后的另一点突然出现,必须重定向到的页面立即出现。

关闭一些灯以解决此问题。让我知道在了解这个问题时是否缺少任何信息。

PFB下面的代码。

`

from flask_sslify import SSLify
application = Flask(__name__)
sslify = SSLify(application)

@app.before_request
def before_request():    
    if request.endpoint !='main' and  'samlNameId' not in session:
        req = prepare_flask_request(request)
        auth = init_saml_auth(req)    
        return redirect(auth.login())

@app.route('/', methods='GET,POST')
def main():
    return index()

@app.route('/requestAccess', methods='GET,POST')
def request_access():    
    return render_template('request-access-form.html', apis=_apis, email_to=email_to)


def index():
    req = prepare_flask_request(request)
    auth = init_saml_auth(req)

    try:

        if 'slo' in request.args:
            name_id = None
            session_index = None
            if 'samlNameId' in session:
                name_id = session['samlNameId']
            if 'samlSessionIndex' in session:
                session_index = session['samlSessionIndex']
            return redirect(auth.logout(name_id=name_id, session_index=session_index))
        elif 'acs' in request.args:            
            auth.process_response()
            errors = auth.get_errors()
            if len(errors) == 0:
                session['samlUserdata'] = auth.get_attributes()
                session['samlNameId'] = auth.get_nameid()
                session['samlSessionIndex'] = auth.get_session_index()               
                self_url = OneLogin_Saml2_Utils.get_self_url(req)                
                if 'RelayState' in request.form and self_url +"/" != request.form['RelayState']:
                    return redirect(auth.redirect_to(request.form['RelayState']))
                return render_template('index.html')
            else:
                logger.error("Onelogin?acs returned with the following errors : " + ','.join(errors))
        elif 'sls' in request.args:
            logger.info("User with name %s and sessionIndex %s logged out successfully", session['samlNameId'],session['samlSessionIndex'])
            dscb = lambda: session.clear()
            url = auth.process_slo(delete_session_cb=dscb)
            errors = auth.get_errors()
            if len(errors) == 0:
                if url is not None:
                    logger.info("Url returned from Onelogin ? sls" + url)
                    return redirect(url)

    except Exception as e:
        logger.error(e)
        return redirect(auth.login())

    return redirect(auth.login())

def init_saml_auth(req):
    auth = OneLogin_Saml2_Auth(req, custom_base_path=app.config['SAML_PATH'])
    return auth


def prepare_flask_request(request):
    # If server is behind proxys or balancers use the HTTP_X_FORWARDED fields
    url_data = urlparse(request.url)    
    prepared = {
        'https': 'on' ,
        'http_host': request.host,
        'script_name': request.path,
        'get_data': request.args.copy(),        
        'post_data': request.form.copy()
    }
    if url_data.port is not None:
        prepared['server_port'] = url_data.port

    return prepared

`

0 个答案:

没有答案