Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask-socketio + Flask-Login/Flask-OpenID trouble #82

Closed
TwoBitAlchemist opened this issue Dec 27, 2014 · 4 comments
Closed

Flask-socketio + Flask-Login/Flask-OpenID trouble #82

TwoBitAlchemist opened this issue Dec 27, 2014 · 4 comments
Labels

Comments

@TwoBitAlchemist
Copy link

I am trying to use the above in combination, and switching to a configuration that starts the gevent-socketio worker seems to have broken my otherwise working login.

Here is my login setup, taken almost directly from your Mega-Tutorial

### Flask-Login & Flask-OpenID ###                                              
@lm.user_loader                                                                 
def load_user(id):                                                              
    return User.query.get(int(id))                                            


@app.before_request                                                             
def before_request():                                                           
    g.user = current_user                                                       


@app.route('/login', methods=['GET', 'POST'])                                   
@oid.loginhandler                                                               
def login():                                                                    
    if g.user is not None and g.user.is_authenticated():                           
        return redirect(oid.get_next_url())                               
    form = LoginForm()                                                             
    if form.validate_on_submit():                                                  
        session['remember_me'] = form.remember_me.data                             
        return oid.try_login(form.openid.data, ask_for=('email',))              
    return render_template('login.html', form=form, providers=OPENID_PROVIDERS)

When I start the server using the app.run() method, this all works normally. Switching to either socketio.run(app) or gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker module:app causes the form POST to get turned into a GET request, which never validates because it is missing 'openid' and 'csrf_token'.

Update

Removing flask-openid works around this, although this not a great long-term solution in my case. (At least, I may have to find something to replace it.) I believe the problem is in @oid.loginhandler decoration but I have not looked into it.

@miguelgrinberg
Copy link
Owner

I'm going to guess that this problem is caused by an incompatibility between the gevent web server and the python-openid package.

Have you tried adding monkey patching? That might help, just add the following at the top of your main Python script:

from gevent import monkey
monkey.patch_all()

Make sure this is done above any other imports, put them in lines 1 & 2 above everything else.

@TwoBitAlchemist
Copy link
Author

It has been almost 2 weeks but I finally got a chance to make a serious effort to reincorporate python-openid (free time projects, you know). Just for reference, monkey patching did not work for me. Nor did changing the form to use GET. I doubt I will get this solved since I have made so little headway myself and python-openid may basically be abandoned. It seems the best solution for now is not to use that in conjunction with this or at all.

@miguelgrinberg
Copy link
Owner

It's unfortunate, but yes, there's probably no easy solution other than debugging python-openid when used with gevent.

@gordol
Copy link

gordol commented Jan 12, 2015

This example works just fine with socketio: https://github.com/mitsuhiko/flask-openid/blob/master/example/example.py

You can just replace the app.run with this:

from flask.ext.socketio import SocketIO
socketio = SocketIO(app)

if __name__ == '__main__':
    init_db()
    app.auto_reload = True
    socketio.run(app, host='0.0.0.0', port=8082)

And you'll need to update the sqlalchemy commit() stuff, but the OpenID aspect works just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants