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

Django REST token authentication #31

Open
rbuzu opened this issue Jul 17, 2017 · 4 comments
Open

Django REST token authentication #31

rbuzu opened this issue Jul 17, 2017 · 4 comments

Comments

@rbuzu
Copy link

rbuzu commented Jul 17, 2017

Hi,

I'm trying to implement authentication using tokens generated by django-rest-framework.

Using this gist: https://gist.github.com/leonardoo/9574251b3c7eefccd84fc38905110ce4

I've managed to do this:

Consumer

class APIDemultiplexer(RestTokenConsumerMixin, WebsocketDemultiplexer):
    rest_user = True

    def connect(self, message, **kwargs):
        Group('users').add(message.reply_channel)

        # user here is correct instance of User from given token
        print(message.user)
        Group('users').send({
            'text': json.dumps({
                "accepted": "True",
            })
        })
    consumers = {
        'campaigns': CampaignBinding.consumer
    }

Resource binding

class CampaignBinding(ResourceBinding):

    model = Campaign
    stream = "campaigns"
    serializer_class = CampaignSerializer
    queryset = Campaign.objects.all()
    permission_classes = (AllowAny,)
   
    def get_serializer_context(self):
        # user here is Anonymous
        print(self.user)
        # I need user instance inside self.message
        return {'request': self.message}

My request
ws://localhost:8000/?token=ef0ef25fcfc75c0db91bbbccc062f0ab5786dc34

The thing is that I can access User instance only inside Consumer and User instance is not propagated to ResourceBinding. Is there some obvious way to access ser from token inside ResourceBinding?

Any help would be appreciated.

@Robinsondev
Copy link

Hi There @rbuzu @linuxlewis I was wondering if you had this figured out.

Stuck with the same issue
Thanks a lot!

@gromsterus
Copy link

Hi @rbuzu

Problem with channel_session_user attribute of channels.binding.base.Binding class, by default it True, need False.

When channel_session_user = True, binding handler wraps by channels.auth.channel_session_user decorator, and it resets the your token user ;(

@KevinMalot
Copy link

Hey @gromsterus

I'm struggling with the same problem.
Can we have more details about your solution please ?
Thanks a lot

@maxblax
Copy link

maxblax commented Jul 24, 2018

Would be adorable of you indeed @gromsterus
In need for this feature.

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

5 participants