setting a cookie for user in a custom pipeline after completing registeration

269 views
Skip to first unread message

Mehrdad Pazooki

unread,
Jun 27, 2014, 1:44:17 PM6/27/14
to python-so...@googlegroups.com
Hi,

I want to set a value in user cookies for TokenAuthentication that I use in DRF. so later I can use that for authorizing requests coming from my frontend.
how can I do that? I want to set these values {'internal_acces_token': '...'}

Matías Aguirre

unread,
Jun 27, 2014, 1:50:28 PM6/27/14
to Mehrdad Pazooki, python-social-auth
Check Django docs for this: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse.set_cookie

Is this question realted to python-social-auth?

Matías

Excerpts from Mehrdad Pazooki's message of 2014-06-27 14:44:17 -0300:
--
Matías Aguirre (matias...@gmail.com)

Mehrdad Pazooki

unread,
Jun 27, 2014, 1:54:06 PM6/27/14
to python-so...@googlegroups.com, pazo...@gmail.com
is that the right way of doing this? is there a place after authentication is done by PSA that I can return an access_token to my client using cookies or http headers?
I'm not sure whether it should happen in pipelines or not...

Matías Aguirre

unread,
Jun 27, 2014, 2:06:45 PM6/27/14
to Mehrdad Pazooki, python-social-auth
Now that the question is directly related to PSA I can answer it in a better
way.

By default PSA will return redirects to user, so by default you won't be able
to set cookies unless you override the default views with your own version, you
can write a simple view like the one detailed here http://psa.matiasaguirre.net/docs/use_cases.html#signup-by-oauth-access-token
and return the cookies/headers that you need. I'm assuming that you are using
some ajax-based authentication flow.

Excerpts from Mehrdad Pazooki's message of 2014-06-27 14:54:06 -0300:
> > Matías Aguirre (matias...@gmail.com <javascript:>)
> >

--
Matías Aguirre (matias...@gmail.com)

Mehrdad Pazooki

unread,
Jun 27, 2014, 2:22:32 PM6/27/14
to Matías Aguirre, python-social-auth
I don't think that's what I want. I have AngularJS for my front end.
There are two types of user registration that I have, one internal with filling a form and the other social using PSA.
I want to use tokenauthentication for both, for the internal one it's easy since I have username and password they can get a token by making a POST request to an api end-point and receive a token.
but for the users who registered with PSA I don't have a password so what I want to do is as soon as their authentication with a provider is done I want to send an access_token back to them.

The solution you mentioned is for when they alread have an access_token. but that's not the case here they don't have an access_token. I want to give them an access_token. am i missing a concept here?

Matías Aguirre

unread,
Jun 27, 2014, 2:58:00 PM6/27/14
to Mehrdad Pazooki, python-social-auth
What if you redirect the user to a custom view where you set those access_token
and return it to the client?

@login_required
def set_token(request):
# Set the user token here
# Return the token to the user

And define:

SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/your/set-token/view'

Makes sense?
Matías

Excerpts from Mehrdad Pazooki's message of 2014-06-27 15:21:52 -0300:
--
Matías Aguirre (matias...@gmail.com)

Mehrdad Pazooki

unread,
Jun 27, 2014, 5:03:43 PM6/27/14
to Matías Aguirre, python-social-auth
Thanks Matias! got it done.

For the benefit of others and if you notice there could be an improvement...:

views.py

from rest_framework.views import APIView

class TokenView(APIView):
    authentication_classes = (SessionAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, format=None):
        if not request.user.is_authenticated:
            return Response({'status': 'Not Authenticated'})
        account = Account.objects.get(email=request.user.email)
        token = account.auth_token.key
        provider = account.social_auth.get().provider
        serializer = TokenSerializer(data=request.DATA)
        if serializer.is_valid():
            return Response({'status': 'OK', 'token': token, 'provider': provider})
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


serializers.py
from rest_framework.serializers import Serializer

class TokenSerializer(serializers.Serializer):

    class Meta:
        model = Account

urls.py

url(r'social-auth', views.TokenView.as_view()),

Stanislav Petriakov

unread,
Dec 28, 2017, 8:38:48 AM12/28/17
to python-social-auth
For those who are curious with partials. You can use @partial decorator and add your func with redirect as last one to pipeline.

SOCIAL_AUTH_PIPELINE = (
...
    'path.to.redirect_to_my',
)

@partial
def redirect_to_my(*args, **kwargs):
    response = redirect('/my/')
    response.set_cookie('YOUR_KEY', YOUR_VALUE, max_age=1209600)
    return response

пятница, 27 июня 2014 г., 20:44:17 UTC+3 пользователь Mehrdad Pazooki написал:
Reply all
Reply to author
Forward
0 new messages