1

I'm trying to set a Custom Authentication class on Django Rest Framework, but all client requests return the same error:

{"detail":"Authentication credentials were not provided."}

So, debugging the Custom class and printing the request.META atributte it has no X_USERNAME key.

CustomAuth class:

 class TecnicoAuthentication(authentication.BaseAuthentication):

 def authenticate(self, request):
    username = request.META.get('X_USERNAME')
    if not username:
        return None
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        raise exceptions.AuthenticationFailed('No such user')
    return (user,None)

on settings.py file:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES' : (
            'os_hotlink.auth.TecnicoAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
            'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_RENDERER_CLASSES': (
            'rest_framework.renderers.JSONRenderer',
    )

}

on apache config file:

 WSGIPassAuthorization On

And finally the request method using httpie:

http -a user:pass http://127.0.0.1/
1
  • the request method with httpie is incorrect, that is for basic authentication, you have created a custom authentication class, and it is looking for a custom request header called X_username Commented Feb 6, 2017 at 18:06

1 Answer 1

2

It is looking for a custom request header named 'X_USERNAME'.

So you need to define the user in a custom request header called X_USERNAME in your HTTpie request...

I think that if you have the username 'user' then you should try sending it as this:

http 127.0.0.1 X_USERNAME:user

HTTP headers

To set custom headers you can use the Header:Value notation:

$ http example.org  User-Agent:PoopyPants  'Cookie:valued-visitor=yes;whatever=whatever;etc=etc'  \
    X_USERNAME:user  Referer:http://stackoverflow.com/


GET / HTTP/1.1
Accept: */*

Accept-Encoding: gzip, deflate
Cookie: valued-visitor=yes;whatever=whatever;etc=etc
Host: 127.0.0.1
Referer: http://stackoverflow.com/
User-Agent: PoopyPants
X_USERNAME: user
Sign up to request clarification or add additional context in comments.

9 Comments

X_USERNAME is a http header attribute passed through wsgi and captured by drf api
But on this case, i cannot capture the attribute for any reason.
Yes my conclusion would be that it's not passing the header?
that is exactly my trouble
are you on apache?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.