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

TypeError on loading PyramidStrategy #390

Closed
lvqier opened this issue Sep 17, 2014 · 12 comments
Closed

TypeError on loading PyramidStrategy #390

lvqier opened this issue Sep 17, 2014 · 12 comments

Comments

@lvqier
Copy link

lvqier commented Sep 17, 2014

Hi, I am going to use python-social-auth with Pyramid, but meet the following problem:

PyramidStrategy takes 3 arguments on init: https://github.com/omab/python-social-auth/blob/master/social/strategies/pyramid_strategy.py#L22
But load_strategy only passes 2 arguments: https://github.com/omab/python-social-auth/blob/master/social/apps/pyramid_app/utils.py#L24

It will raises TypeError which makes python-social-auth never working with Pyramid:


...
  File "/opt/webenv/local/lib/python2.7/site-packages/pyramid_debugtoolbar-2.2-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 165, in toolbar_tween
    return handler(request)
  File "/opt/webenv/local/lib/python2.7/site-packages/pyramid/tweens.py", line 21, in excview_tween
    response = handler(request)
  File "/opt/webenv/local/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 82, in tm_tween
    reraise(*exc_info)
  File "/opt/webenv/local/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 63, in tm_tween
    response = handler(request)
  File "/opt/webenv/local/lib/python2.7/site-packages/pyramid/router.py", line 163, in handle_request
    response = view_callable(context, request)
  File "/opt/webenv/local/lib/python2.7/site-packages/pyramid/config/views.py", line 329, in attr_view
    return view(context, request)
  File "/opt/webenv/local/lib/python2.7/site-packages/pyramid/config/views.py", line 305, in predicate_wrapper
    return view(context, request)
  File "/opt/webenv/local/lib/python2.7/site-packages/pyramid/config/views.py", line 385, in viewresult_to_response
    result = view(context, request)
  File "/opt/webenv/local/lib/python2.7/site-packages/pyramid/config/views.py", line 501, in _requestonly_view
    response = view(request)
  File "/opt/webenv/local/lib/python2.7/site-packages/python_social_auth-0.2.1-py2.7.egg/social/apps/pyramid_app/utils.py", line 46, in wrapper
    request.strategy = load_strategy()
  File "/opt/webenv/local/lib/python2.7/site-packages/python_social_auth-0.2.1-py2.7.egg/social/apps/pyramid_app/utils.py", line 25, in load_strategy
    return get_strategy(get_helper('STRATEGY'), get_helper('STORAGE'))
  File "/opt/webenv/local/lib/python2.7/site-packages/python_social_auth-0.2.1-py2.7.egg/social/strategies/utils.py", line 15, in get_strategy
    return Strategy(Storage, *args, **kwargs)
TypeError: __init__() takes at least 3 arguments (2 given)
@amilstead
Copy link

I've run into this issue during development as well. I'm planning on setting up a pull request soon, but in the interim, I've just patched their pyramid_app strategy in order to get things running.

I've got a monkey patch for it that seems to get things up and running (see below). After you've applied the patch, make sure to change the function for your "login_user" the take the parameters (backend, user, social_user) (see the example code)

Here's the patch (call it at the top of your app/init.py file with all the config setup):

from functools import wraps

from pyramid.httpexceptions import HTTPNotFound
from social.strategies.utils import get_strategy
from social.apps.pyramid_app import utils


def patch():

    def load_strategy(request):
        return get_strategy(
            utils.get_helper('STRATEGY'),
            utils.get_helper('STORAGE'),
            request
        )

    utils.load_strategy = load_strategy

    def psa(redirect_uri=None):
        def decorator(func):
            @wraps(func)
            def wrapper(request, *args, **kwargs):
                backend = request.matchdict.get('backend')
                if not backend:
                    return HTTPNotFound('Missing backend')

                uri = redirect_uri
                if uri and not uri.startswith('/'):
                    uri = request.route_url(uri, backend=backend)

                request.strategy = load_strategy(request)
                request.backend = utils.load_backend(
                    request.strategy,
                    backend,
                    uri
                )
                return func(request, *args, **kwargs)
            return wrapper
        return decorator
    utils.psa = psa

@amilstead
Copy link

Another issue with their pyramid implementation seems to be that the storage engine they're using (sqlalchemy_orm) isn't flushing the ORM session after the commit, so the user_id never actually makes it into the request's session.

@lvqier
Copy link
Author

lvqier commented Oct 25, 2014

Thanks @amilstead . It works after I did some thing like your patch.

omab added a commit that referenced this issue Nov 2, 2014
@omab
Copy link
Owner

omab commented Nov 2, 2014

I've pushed the fix to the psa() and load_strategy() calls.

@amilstead, I'm not sure what you mean.

@amilstead
Copy link

@omab I was experiencing some funniness with the model that was coming through inside login callback functions.

Specifically, I narrowed down that in this file: https://github.com/omab/python-social-auth/blob/master/social/storage/sqlalchemy_orm.py#L40 there should be a call to cls._session().flush() in order for the newly created ORM instance to have a DB generated primary key.

Upon new users signing up/registering via the PSA system on a pyramid framework, a user object will be passed to the "SOCIAL_AUTH_LOGIN_FUNCTION" (defined in settings). That user, if the session it was created under had never been flushed, will end up with an id of None or possibly be None itself.

You can test this by using the example project https://github.com/omab/python-social-auth/blob/master/examples/pyramid_example/example in a simple dev environment to see what I'm talking about (one thing you'll have to change is the parameters to login_user, it should be: def login_user(backend, user, social_user).

@jeromelefeuvre
Copy link
Contributor

+1 @amilstead

Working to integrate this on pyramid+sqlalchemy and have the same problem ;(

@omab
Copy link
Owner

omab commented Mar 17, 2015

I've pushed the flush() change to that line, it seem to work fine now, do you mind taking a look?

@jeromelefeuvre
Copy link
Contributor

Hey @omab ,

I just tested and it's failed. I try to create a test for that. To reproduce the test, start from a empty db (or remove users table) and try to login (I took twitter as example), Done page failed because request.user is none:

'NoneType' object has no attribute 'username'

@omab
Copy link
Owner

omab commented Mar 19, 2015

@jeromelefeuvre, I've pushed another fix. I was able to reproduce the issue with a fresh db (thanks), the last fix seems to do the trick, could you test again please?

@jeromelefeuvre
Copy link
Contributor

+1. You rocks !

@jeromelefeuvre
Copy link
Contributor

My question is "How to logout in pyramid ?"
I tried to remove request.session['user_id'] but if I do that, it create new user each time I connect.
First user is created with good username but on the second login, it create a second user with username 'jeromelefeuvreb378405578'

@omab
Copy link
Owner

omab commented Mar 19, 2015

Removing the sessions value should be enough to logout the user, but that second user with the extra hash, means that it's failing to detect the already created user in the previous login. Let me check it.

jeromelefeuvre added a commit to jeromelefeuvre/python-social-auth that referenced this issue Mar 23, 2015
jeromelefeuvre added a commit to jeromelefeuvre/python-social-auth that referenced this issue Mar 23, 2015
@omab omab closed this as completed Dec 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants