0

It is the same thread as this question.

The problem is that the output values on the local and EB instances are different.

users/views.py

class UsersViewSet(ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def get_permissions(self):
        if self.action == "list":
            permission_classes = [IsAdminUser]
        elif self.action == "create" or self.action == "retrieve":
            permission_classes = [AllowAny]
        elif self.action == "destroy":
            permission_classes = [IsAdminOrSelf]
        else:
            permission_classes = [IsSelf]

        return [permission() for permission in permission_classes]

users/permissions.py

class IsAdminUser(BasePermission):
    """
    Allows access only to admin users.
    """

    def has_permission(self, request, view):
        print("=" * 50)
        print(dir(request))
        print(request.authenticators)
        print(request.auth)
        print(request.data)
        print(request.user)
        print("=" * 50)
        return bool(request.user and request.user.is_admin)

The main code is as above, and the result of sending GET user list request to the server is as follows.

EB

[Wed Jul 01 21:58:26.498909 2020] [:error] [pid
 8264] ========================================
==========
[Wed Jul 01 21:58:26.499000 2020] [:error] [pid 8264] ['DATA', 'FILES', 'POST', 'QUERY_PARAMS', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_auth', '_authenticate', '_authenticator', '_content_type', '_data', '_default_negotiator', '_files', '_full_data', '_load_data_and_files', '_load_stream', '_not_authenticated', '_parse', '_request', '_stream', '_supports_form_parsing', '_user', 'accepted_media_type', 'accepted_renderer', 'auth', 'authenticators', 'content_type', 'data', 'force_plaintext_errors', 'negotiator', 'parser_context', 'parsers', 'query_params', 'stream', 'successful_authenticator', 'user', 'version', 'versioning_scheme']
[Wed Jul 01 21:58:26.499016 2020] [:error] [pid 8264] [<rest_framework_simplejwt.authentication.JWTAuthentication object at 0x7fa4e7c578d0>]
[Wed Jul 01 21:58:26.499024 2020] [:error] [pid 8264] None
[Wed Jul 01 21:58:26.499472 2020] [:error] [pid 8264] <QueryDict: {}>
[Wed Jul 01 21:58:26.499487 2020] [:error] [pid 8264] AnonymousUser
[Wed Jul 01 21:58:26.499493 2020] [:error] [pid 8264] ==================================================

Local

==================================================
['DATA', 'FILES', 'POST', 'QUERY_PARAMS', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_auth', '_authenticate', '_authenticator', '_content_type', '_data', '_default_negotiator', '_files', '_full_data', '_load_data_and_files', '_load_stream', '_not_authenticated', '_parse', '_request', '_stream', '_supports_form_parsing', '_user', 'accepted_media_type', 'accepted_renderer', 'auth', 'authenticators', 'content_type', 'data', 'force_plaintext_errors', 'negotiator', 'parser_context', 'parsers', 'query_params', 'stream', 'successful_authenticator', 'user', 'version', 'versioning_scheme']
[<rest_framework_simplejwt.authentication.JWTAuthentication object at 0x7f8cb8688e10>]
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTkzNjI2MDk4LCJqdGkiOiI3ZWNmMGZkZDJlMjk0MzRjOWExYmRhNWM0ZDY3NWQwOSIsInVzZXJfaWQiOjF9.AqPthXqfErjhT9rnknRzRIhvU5eAG8k0SsnaPLgTlSc
<QueryDict: {}>
1 : tim - tim@mole.land
==================================================
[01/Jul/2020 21:56:30] "GET /api/v1/users/ HTTP/1.1" 200 1768

Why doesn't the EB instance receive the value of the Authorization header?

Tim
  • 637
  • 1
  • 5
  • 9
  • Are you sure that your view has `authentication_classes` classes? – JPG Jul 01 '20 at 13:24
  • Nothing is specified. Is that the problem? Then why was there no problem locally? – Tim Jul 01 '20 at 14:36
  • Probably, Yes. try to set `authentication_classes` in your view – JPG Jul 01 '20 at 14:48
  • As far as I know, this is the default. authentication_classes = [, ] (http://www.cdrf.co/3.9/rest_framework.viewsets/ModelViewSet.html) – Tim Jul 01 '20 at 15:06
  • Should I add it like this? authentication_classes = [, , ] – Tim Jul 01 '20 at 15:08
  • I am not sure about the default thing, but you should add ***corresponding authentication class*** to your view in order to get authenticated. https://www.django-rest-framework.org/api-guide/authentication/ – JPG Jul 01 '20 at 15:10
  • I've tried... I still can't read the token. – Tim Jul 01 '20 at 15:19

0 Answers0