Search code examples
python-social-auth

python social auth load strategy and authenticate user manually with release 0.1.26


I used python social auth for social authentication in the last 2 months and it was great. I needed QQ support, hence installed newest git commit (23e4e289ec426732324af106c7c2e24efea34aeb - not part of a release). until now i used to authenticate the user using the following code:

    # setup redirect uri in order to load strategy
    uri = redirect_uri = "social:complete"
    if uri and not uri.startswith('/'):
        uri = reverse(redirect_uri, args=(backend,))

    # load the strategy
    try:
        strategy = load_strategy(
            request=request, backend=backend,
            redirect_uri=uri, **kwargs
        )
        strategy = load_strategy(request=bundle.request)
    except MissingBackend:
        raise ImmediateHttpResponse(HttpNotFound('Backend not found'))

    # get the backend for the strategy
    backend = strategy.backend

    # check backend type and set token accordingly
    if isinstance(backend, BaseOAuth1):
        token = {
            'oauth_token': bundle.data.get('access_token'),
            'oauth_token_secret': bundle.data.get('access_token_secret'),
        }
    elif isinstance(backend, BaseOAuth2):
        token = bundle.data.get('access_token')
    else:
        raise ImmediateHttpResponse(HttpBadRequest('Wrong backend type'))

    # authenticate the user
    user = strategy.backend.do_auth(token)

which worked fine.

In the latest release this behaviour has changed, and an exception is raised since the "load_strategy" method has changed.

I can't seem to find any documentation on how to do it with the new release.

Any help would be appreciated!

Omri.


Solution

  • The last changes in the repository changed the importance of the strategy, instead of being the main entity to perform the authentication, it's just a helper class to glue the framework with the backends. Try with this snippet to load the strategy and the backend:

    from social.apps.django_app.utils import load_strategy, load_backend
    
    strategy = load_strategy(request)
    backend = load_backend(strategy, backend, uri)
    ...
    user = backend.do_auth(token)