Can you post and share your project on github?

Em dom, 1 de dez de 2019 04:10, Yery cs <[email protected]> escreveu:

> If I don't use request.user in middleware, all apis works well with basic
> authentication.
> So I guess, it is not related with basic authentication.
>
> On Sun, Dec 1, 2019 at 8:30 AM Wanderley S <[email protected]> wrote:
>
>> Please send a screenshot with the headers
>>
>> Em sáb, 30 de nov de 2019 18:19, Yery cs <[email protected]> escreveu:
>>
>>> I use postman to test api.
>>> Can you tell me how to test api for you?
>>>
>>>
>>> On Sun, Dec 1, 2019 at 5:11 AM Yery cs <[email protected]> wrote:
>>>
>>>> Is there any relation with python version?
>>>>
>>>> On Sun, Dec 1, 2019 at 5:08 AM Oleg Nykolyn <[email protected]> wrote:
>>>>
>>>>> Last idea - could it possibly be that you set header in a different
>>>>> way ?
>>>>> It's quite mysterious that same code work differently, so maybe reason
>>>>> is in client's request...
>>>>> I use following command(https://httpie.org as http client):
>>>>> ```
>>>>>
>>>>> http GET 127.0.0.1:8000/api/test/  Authorization:Bearer\
>>>>> "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNTc1MTQ3MTkyLCJleHAiOjE1NzUxNDc0OTIsInVzZXJfaWQiOjEsIm9yaWdfaWF0IjoxNTc1MTQ3MTkyfQ.tCDJ3nwe3k5xImxwzePbGitPGV4b_3A1EaF0hkQxzhc"
>>>>>
>>>>> ```
>>>>>
>>>>>
>>>>> On Sat, Nov 30, 2019 at 10:59 PM Yery cs <[email protected]> wrote:
>>>>>
>>>>>> Yes. I have extracted from project you sent.
>>>>>> I have checked two files and all changes can be seen.
>>>>>>
>>>>>> On Sun, Dec 1, 2019 at 4:57 AM Oleg Nykolyn <[email protected]>
>>>>>> wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> I get following output:
>>>>>>>
>>>>>>> [30/Nov/2019 20:53:35] *"POST /api/test/ HTTP/1.1" 405 41*
>>>>>>>
>>>>>>> admin
>>>>>>>
>>>>>>> None
>>>>>>>
>>>>>>> ***************
>>>>>>>
>>>>>>> [30/Nov/2019 20:54:34] "GET /api/test/ HTTP/1.1" 200 18
>>>>>>>
>>>>>>> Have you extracted code from archive attached to previous message ?
>>>>>>> It contains changes in middleware.py and settings.py - extra middleware 
>>>>>>> is
>>>>>>> added there:
>>>>>>>
>>>>>>> ```
>>>>>>>
>>>>>>> MIDDLEWARE = [
>>>>>>>
>>>>>>>     'django.middleware.security.SecurityMiddleware',
>>>>>>>
>>>>>>>     'django.contrib.sessions.middleware.SessionMiddleware',
>>>>>>>
>>>>>>>     'django.middleware.common.CommonMiddleware',
>>>>>>>
>>>>>>>     'django.middleware.csrf.CsrfViewMiddleware',
>>>>>>>
>>>>>>>     'django.contrib.auth.middleware.AuthenticationMiddleware',
>>>>>>>
>>>>>>>     'django.contrib.messages.middleware.MessageMiddleware',
>>>>>>>
>>>>>>>     'django.middleware.clickjacking.XFrameOptionsMiddleware',
>>>>>>>
>>>>>>>     'middleware.middleware.AuthenticationTokenMiddleware',
>>>>>>>
>>>>>>>     'middleware.middleware.PermissionMiddleware'
>>>>>>>
>>>>>>> ]
>>>>>>>
>>>>>>> ```
>>>>>>>
>>>>>>>
>>>>>>> On Sat, Nov 30, 2019 at 10:50 PM Yery cs <[email protected]>
>>>>>>> wrote:
>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> On Sun, Dec 1, 2019 at 4:47 AM Yery cs <[email protected]>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> Hello. thanks for your help.
>>>>>>>>> Unfortunately, I still get AnonymousUser.
>>>>>>>>> Can you check once more again?
>>>>>>>>>
>>>>>>>>> On Sun, Dec 1, 2019 at 4:40 AM Oleg Nykolyn <[email protected]>
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> Hi,
>>>>>>>>>>
>>>>>>>>>> Here is custom middleware, which works for this case, also
>>>>>>>>>> attached project with applied changes -  it now show "admin" instead 
>>>>>>>>>> of
>>>>>>>>>> AnonymousUser.
>>>>>>>>>> Reason why DRF doesn't show user in middleware - DRF does not
>>>>>>>>>> provide it's own middleware for this and sets user only when it 
>>>>>>>>>> processes
>>>>>>>>>> views. So any other middleware won't be able to see requests.user, 
>>>>>>>>>> unless
>>>>>>>>>> Django's middleware will set it (not DRF!), but this means 
>>>>>>>>>> cookie/session
>>>>>>>>>> auth.
>>>>>>>>>> I've spent a few day on same issue once, it's unfortunate that
>>>>>>>>>> DRF doesn't provide built-in middleware for this, but workaround is 
>>>>>>>>>> quite
>>>>>>>>>> simple and reliable.
>>>>>>>>>> ```
>>>>>>>>>>
>>>>>>>>>> from rest_framework_jwt.authentication import
>>>>>>>>>> JSONWebTokenAuthentication
>>>>>>>>>>
>>>>>>>>>> from django.utils.functional import SimpleLazyObject
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> # Workaround simialr to
>>>>>>>>>> https://github.com/GetBlimp/django-rest-framework-jwt/issues/45
>>>>>>>>>> # noqa
>>>>>>>>>>
>>>>>>>>>> class AuthenticationTokenMiddleware:
>>>>>>>>>>
>>>>>>>>>>     """Authentication middleware which return user from token."""
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>     def __init__(self, get_response):
>>>>>>>>>>
>>>>>>>>>>         """Initializer."""
>>>>>>>>>>
>>>>>>>>>>         self.get_response = get_response
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>     def __call__(self, request):
>>>>>>>>>>
>>>>>>>>>>         """Response."""
>>>>>>>>>>
>>>>>>>>>>         user = request.user
>>>>>>>>>>
>>>>>>>>>>         request.user = SimpleLazyObject(lambda:
>>>>>>>>>> self.get_token_user(request,
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>   user))
>>>>>>>>>>
>>>>>>>>>>         return self.get_response(request)
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>     def get_token_user(self, request, user):
>>>>>>>>>>
>>>>>>>>>>         """Return user from DRF token."""
>>>>>>>>>>
>>>>>>>>>>         try:
>>>>>>>>>>
>>>>>>>>>>             authenticator = JSONWebTokenAuthentication()
>>>>>>>>>>
>>>>>>>>>>             return authenticator.authenticate(request)[0]
>>>>>>>>>>
>>>>>>>>>>         except Exception:
>>>>>>>>>>
>>>>>>>>>>             return user
>>>>>>>>>> ```
>>>>>>>>>>
>>>>>>>>>> On Sat, Nov 30, 2019 at 10:34 PM Wanderley S <[email protected]>
>>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>>> Nothing to be sorry about.
>>>>>>>>>>> I'll take a look at your code later, since I'm on mobile now.
>>>>>>>>>>>
>>>>>>>>>>> Can you show me the code where you make the request?
>>>>>>>>>>>
>>>>>>>>>>> You're using curl or some javascript client?
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> Em sáb, 30 de nov de 2019 17:10, Yery cs <[email protected]>
>>>>>>>>>>> escreveu:
>>>>>>>>>>>
>>>>>>>>>>>> Hello, thanks for you reply.
>>>>>>>>>>>> Sorry for my poor post. I understand concepts of Django
>>>>>>>>>>>> authentication.
>>>>>>>>>>>> I am using DRF jwt.
>>>>>>>>>>>> I have added my custom middleware after Authentication
>>>>>>>>>>>> middleware in settings file.
>>>>>>>>>>>> And I am printing request.user in console like this `print
>>>>>>>>>>>> (request.user)` in my custom middleware.
>>>>>>>>>>>> But I get still AnonymousUser.
>>>>>>>>>>>> Can you tell me why it is, and how can I fix it?
>>>>>>>>>>>> Thank you.
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>> --
>>>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>>>> Google Groups "Django REST framework" group.
>>>>>>>>>>>> To unsubscribe from this group and stop receiving emails from
>>>>>>>>>>>> it, send an email to
>>>>>>>>>>>> [email protected].
>>>>>>>>>>>> To view this discussion on the web visit
>>>>>>>>>>>> https://groups.google.com/d/msgid/django-rest-framework/CALCmTPEd-rLHmS%2BazDR2Zj-NR7ZtkbVhEEQcvdER%3Dbq0%3DU-5vA%40mail.gmail.com
>>>>>>>>>>>> <https://groups.google.com/d/msgid/django-rest-framework/CALCmTPEd-rLHmS%2BazDR2Zj-NR7ZtkbVhEEQcvdER%3Dbq0%3DU-5vA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>>>>>>>> .
>>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>>> Google Groups "Django REST framework" group.
>>>>>>>>>>> To unsubscribe from this group and stop receiving emails from
>>>>>>>>>>> it, send an email to
>>>>>>>>>>> [email protected].
>>>>>>>>>>> To view this discussion on the web visit
>>>>>>>>>>> https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ3efg3%2BBzgcxeTSCZwP6pw%3Dk3NoecUsJ0QNN_7nREErSQ%40mail.gmail.com
>>>>>>>>>>> <https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ3efg3%2BBzgcxeTSCZwP6pw%3Dk3NoecUsJ0QNN_7nREErSQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>>>>>>> .
>>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>> Google Groups "Django REST framework" group.
>>>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>>>> send an email to
>>>>>>>>>> [email protected].
>>>>>>>>>> To view this discussion on the web visit
>>>>>>>>>> https://groups.google.com/d/msgid/django-rest-framework/CALqbk1Rx7pQ35-2BZ3rS0tFMwzYLe%2B5wHNS8Z%3DgSjxD6ttc%2Brw%40mail.gmail.com
>>>>>>>>>> <https://groups.google.com/d/msgid/django-rest-framework/CALqbk1Rx7pQ35-2BZ3rS0tFMwzYLe%2B5wHNS8Z%3DgSjxD6ttc%2Brw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>>>>>> .
>>>>>>>>>>
>>>>>>>>> --
>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>> Groups "Django REST framework" group.
>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>> send an email to [email protected]
>>>>>>>> .
>>>>>>>> To view this discussion on the web visit
>>>>>>>> https://groups.google.com/d/msgid/django-rest-framework/CALCmTPEQCBjUPv_ZF2%2BSn6Xw-f%2BvjvbrFF22XwK0L0ktR7Zd8Q%40mail.gmail.com
>>>>>>>> <https://groups.google.com/d/msgid/django-rest-framework/CALCmTPEQCBjUPv_ZF2%2BSn6Xw-f%2BvjvbrFF22XwK0L0ktR7Zd8Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>>>> .
>>>>>>>>
>>>>>>> --
>>>>>>> You received this message because you are subscribed to the Google
>>>>>>> Groups "Django REST framework" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>> send an email to [email protected].
>>>>>>> To view this discussion on the web visit
>>>>>>> https://groups.google.com/d/msgid/django-rest-framework/CALqbk1QxctvtFSpcsAtczng-H2Xjc9DCBzXC2jNSnycmHTHqpQ%40mail.gmail.com
>>>>>>> <https://groups.google.com/d/msgid/django-rest-framework/CALqbk1QxctvtFSpcsAtczng-H2Xjc9DCBzXC2jNSnycmHTHqpQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>>> .
>>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Django REST framework" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected].
>>>>>> To view this discussion on the web visit
>>>>>> https://groups.google.com/d/msgid/django-rest-framework/CALCmTPF03Ks5bKeZHzbQ7L6XbssdjmvEhtrCPdnru8Z%3DqV8NJA%40mail.gmail.com
>>>>>> <https://groups.google.com/d/msgid/django-rest-framework/CALCmTPF03Ks5bKeZHzbQ7L6XbssdjmvEhtrCPdnru8Z%3DqV8NJA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>> .
>>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Django REST framework" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/django-rest-framework/CALqbk1TqBUe351y39eZ%3D%3DWQeLkwXULSK81jCHOWdtFjRPim%3DRA%40mail.gmail.com
>>>>> <https://groups.google.com/d/msgid/django-rest-framework/CALqbk1TqBUe351y39eZ%3D%3DWQeLkwXULSK81jCHOWdtFjRPim%3DRA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django REST framework" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-rest-framework/CALCmTPHCq5vfM%2BKmUA8BaE1GOcavOJkSSxDi%2B2bw1G7Ak79kNg%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/django-rest-framework/CALCmTPHCq5vfM%2BKmUA8BaE1GOcavOJkSSxDi%2B2bw1G7Ak79kNg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django REST framework" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ2spbAJED7JXfV_9a-s4xZ0fhp5vR9d_4G-dUyMo4QB-A%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ2spbAJED7JXfV_9a-s4xZ0fhp5vR9d_4G-dUyMo4QB-A%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/CALCmTPFaqgLcQBGKj5fHugZeWd9KEkQWJnjtW4zTk8SSaw4BOw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-rest-framework/CALCmTPFaqgLcQBGKj5fHugZeWd9KEkQWJnjtW4zTk8SSaw4BOw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ3A_zU5%2Bo_VuwMgS6o%3DdKR%2BUaDhrkcqyRvWp1fWX08SNw%40mail.gmail.com.

Reply via email to