Python Django Rest Framework authentication -
i attempting authenticate guid being passed in url via web api interface. not managing pass guid authenticate class.
note: authenticating mean making sure valid guid
my urls.py:
url(r'^customer_address/(?p<guid>[a-z0-9-]+)/(?p<address_id>[a-z0-9-]+)/$', views.customeraddressview.as_view()), my views.py:
class customeraddressview(generics.retrieveapiview): lookup_field = "address_id" queryset = customeraddress.objects.all() serializer_class = customeraddressserializer my settings.py:
rest_framework = { 'default_authentication_classes': ( 'customer.authenticate.authenticate', ), 'default_permission_classes': ( 'rest_framework.permissions.isauthenticated', ) } authenticate class in customer app looks this:
class authenticate(authentication.baseauthentication) : def authenticate(self, request): request = request._request guid = getattr(request, 'guid', none) my_logger.debug(guid) if not guid: my_logger.debug('there no guid!') return none try: user = customer.objects.get(guid=guid,brand=1) except customer.doesnotexist: raise exceptions.authenticationfailed('no such user') return none and request looks this:

problem: retrieve guid in class authenticate , make sure valid. @ moment keep getting error see in screenshot , logs read: 'there no guid!'
how can pass guid request authenticate class?
thanks
you can this:
class authenticate(authentication.baseauthentication) : def authenticate(self, request): request = request._request # bit hacky way value of guid guid = request.path.split('/')[-3] my_logger.debug(guid) if not guid: my_logger.debug('there no guid!') return none try: user = customer.objects.get(guid=guid,brand=1) except customer.doesnotexist: raise exceptions.authenticationfailed('no such user') return none this bit hacky since trying access guid splitting request.path '/' , access third-last index of list obtained after splitting.
i have checked, self has no access kwargs in drf views, can't access kwargs here.
another solution pass kwargs in drf view explicitly when authenticate() called overriding authentication process of drf.
Comments
Post a Comment