I am attempting to authenticate a GUID that is being passed in the url via the web API interface. However I am not managing to pass GUID to my Authenticate class.
Note: by authenticating I mean making sure it is a 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 my customer app looks like 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 is 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 the request looks like this:
Problem: I like to retrieve a GUID in class Authenticate and make sure it is valid. At the moment I keep getting the error you see in the screenshot and my logs read: 'There is no guid!'
How can I pass guid from request to the Authenticate class?
Thanks
kwargs
in self. Doguid = self.kwargs.get('guid', None)
kwargs
of the DRF view inauthenticate()
. I have updated my ans. Another solution is to pass thekwargs
of the view toauthenticate()
when it is called.