Skip to content

Instantly share code, notes, and snippets.

@geeknam
Created November 2, 2014 12:10
Show Gist options
  • Select an option

  • Save geeknam/045ab1b9a99b8089bf90 to your computer and use it in GitHub Desktop.

Select an option

Save geeknam/045ab1b9a99b8089bf90 to your computer and use it in GitHub Desktop.

Revisions

  1. geeknam created this gist Nov 2, 2014.
    37 changes: 37 additions & 0 deletions psa_mobile.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    from social.actions import do_complete
    from social.apps.django_app import load_strategy
    from social.apps.django_app.default.models import UserSocialAuth
    from social.apps.django_app.views import _do_login
    from social.backends.utils import load_backends
    from social.exceptions import MissingBackend



    def psa_mobile(request, *args, **kwargs):
    """
    Endpoint used by mobile app to authenticate using OAuth2
    access tokens
    """
    provider = request.POST.get('provider')
    access_token = request.POST.get('access_token')
    data = {'success': False}
    try:
    request.social_strategy = load_strategy(
    request=request, backend=provider,
    redirect_uri=None, *args, **kwargs
    )
    except MissingBackend:
    raise Http404('Backend not found')

    try:
    user = request.social_strategy.backend.do_auth(access_token)
    if user:
    _do_login(request.social_strategy, user)
    data['success'] = True
    except HTTPError:
    pass
    except AttributeError:
    # redirect to register endpoint

    # Return response here
    return data