from django.contrib.auth.models import AnonymousUser
from django.utils.deprecation import MiddlewareMixin

from django_jwt.auth import JWTAuthentication
from django_jwt.settings_utils import get_setting


class JWTAuthenticationMiddleware(MiddlewareMixin):
    def process_request(self, request):
        key = request.COOKIES.get(get_setting('JWT_CLIENT.COOKIE_NAME'), None)
        user = None
        if key is not None:
            try:
                user = JWTAuthentication.authenticate_credentials(key)
            except JWTAuthentication.JWTException:
                pass
        return user or AnonymousUser()
