Skip to content

Django REST token authentication #31

@rbuzu

Description

@rbuzu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions