# Path: atlassian_jwt_auth/frameworks/common/asap.py
# def _process_asap_token(request, backend, settings, verifier=None):
#     """ Verifies an ASAP token, validates the claims, and returns an error
#     response"""
#     logger = logging.getLogger('asap')
#     token = backend.get_asap_token(request)
#     error_response = None
#     if token is None and not settings.ASAP_REQUIRED and (
#             settings.ASAP_REQUIRED is not None):
#         return
#     try:
#         if token is None:
#             raise NoTokenProvidedError
#         if verifier is None:
#             verifier = backend.get_verifier(settings=settings)
#         asap_claims = verifier.verify_jwt(
#             token,
#             settings.ASAP_VALID_AUDIENCE,
#             leeway=settings.ASAP_VALID_LEEWAY,
#         )
# 
#         _verify_issuers(asap_claims, settings.ASAP_VALID_ISSUERS)
#         backend.set_asap_claims_for_request(request, asap_claims)
#     except NoTokenProvidedError:
#         logger.info('No token provided')
#         error_response = backend.get_401_response(
#             'Unauthorized', request=request
#         )
#     except PublicKeyRetrieverException as e:
#         if e.status_code not in (403, 404):
#             # Any error other than "not found" is a problem and should
#             # be dealt with elsewhere.
#             # Note that we treat 403 like 404 to account for the fact
#             # that a server configured to secure directory listings
#             # will return 403 for a missing file to avoid leaking
#             # information.
#             raise
#             logger.warning('Could not retrieve the matching public key')
#         error_response = backend.get_401_response(
#             'Unauthorized: Key not found', request=request
#         )
#     except InvalidIssuerError:
#         logger.warning('Invalid token - issuer')
#         error_response = backend.get_403_response(
#             'Forbidden: Invalid token issuer', request=request
#         )
#     except InvalidTokenError:
#         logger.warning('Invalid token')
#         error_response = backend.get_401_response(
#             'Unauthorized: Invalid token', request=request
#         )
#     except JtiUniquenessException:
#         logger.warning('Invalid token - duplicate jti')
#         error_response = backend.get_401_response(
#             'Unauthorized: Invalid token - duplicate jti', request=request
#         )
#     except SubjectDoesNotMatchIssuerException:
#         logger.warning('Invalid token - subject and issuer do not match')
#         error_response = backend.get_401_response(
#             'Unauthorized: Subject and Issuer do not match', request=request
#         )
#     except Exception:
#         logger.exception('An error occured while checking an asap token')
#         raise
# 
#     if error_response is not None and settings.ASAP_REQUIRED:
#         return error_response
# 
# Path: atlassian_jwt_auth/frameworks/django/backend.py
# class DjangoBackend(Backend):
#     def get_authorization_header(self, request=None):
#         if request is None:
#             raise ValueError('No request available')
# 
#         return request.META.get('HTTP_AUTHORIZATION', b'')
# 
#     def get_401_response(self, data=None, headers=None, request=None):
#         if headers is None:
#             headers = {}
# 
#         headers.update(self.default_headers_401)
# 
#         response = HttpResponse(content=data, status=401)
#         for k, v in headers.items():
#             response[k] = v
# 
#         return response
# 
#     def get_403_response(self, data=None, headers=None, request=None):
#         if headers is None:
#             headers = {}
# 
#         response = HttpResponseForbidden(data)
#         for k, v in headers.items():
#             response[k] = v
# 
#         return response
# 
#     def set_asap_claims_for_request(self, request, claims):
#         request.asap_claims = claims
# 
#     @property
#     def settings(self):
#         settings = {}
#         settings.update(self.default_settings)
# 
#         for k in settings.keys():
#             value = getattr(django_settings, k, None)
#             if value is None:
#                 continue
# 
#             settings[k] = value
# 
#         return self._process_settings(settings)


from ..common.asap import _process_asap_token
from .backend import DjangoBackend


def asap_middleware(get_response):
    """Middleware to enable ASAP for all requests"""
    backend = DjangoBackend()
    settings = backend.settings
    _verifier = backend.get_verifier(settings=settings)

    def middleware(request):
        error_response = _process_asap_token(request, backend, settings,
                                             verifier=_verifier)
        if error_response is not None:
            return error_response

        return get_response(request)

    return middleware


class OldStyleASAPMiddleware(object):
    """Middleware to enable ASAP for all requests (for legacy applications
    using MIDDLEWARE_CLASSES)"""

    def __init__(self):

# Path: atlassian_jwt_auth/frameworks/common/asap.py
# def _process_asap_token(request, backend, settings, verifier=None):
#     """ Verifies an ASAP token, validates the claims, and returns an error
#     response"""
#     logger = logging.getLogger('asap')
#     token = backend.get_asap_token(request)
#     error_response = None
#     if token is None and not settings.ASAP_REQUIRED and (
#             settings.ASAP_REQUIRED is not None):
#         return
#     try:
#         if token is None:
#             raise NoTokenProvidedError
#         if verifier is None:
#             verifier = backend.get_verifier(settings=settings)
#         asap_claims = verifier.verify_jwt(
#             token,
#             settings.ASAP_VALID_AUDIENCE,
#             leeway=settings.ASAP_VALID_LEEWAY,
#         )
# 
#         _verify_issuers(asap_claims, settings.ASAP_VALID_ISSUERS)
#         backend.set_asap_claims_for_request(request, asap_claims)
#     except NoTokenProvidedError:
#         logger.info('No token provided')
#         error_response = backend.get_401_response(
#             'Unauthorized', request=request
#         )
#     except PublicKeyRetrieverException as e:
#         if e.status_code not in (403, 404):
#             # Any error other than "not found" is a problem and should
#             # be dealt with elsewhere.
#             # Note that we treat 403 like 404 to account for the fact
#             # that a server configured to secure directory listings
#             # will return 403 for a missing file to avoid leaking
#             # information.
#             raise
#             logger.warning('Could not retrieve the matching public key')
#         error_response = backend.get_401_response(
#             'Unauthorized: Key not found', request=request
#         )
#     except InvalidIssuerError:
#         logger.warning('Invalid token - issuer')
#         error_response = backend.get_403_response(
#             'Forbidden: Invalid token issuer', request=request
#         )
#     except InvalidTokenError:
#         logger.warning('Invalid token')
#         error_response = backend.get_401_response(
#             'Unauthorized: Invalid token', request=request
#         )
#     except JtiUniquenessException:
#         logger.warning('Invalid token - duplicate jti')
#         error_response = backend.get_401_response(
#             'Unauthorized: Invalid token - duplicate jti', request=request
#         )
#     except SubjectDoesNotMatchIssuerException:
#         logger.warning('Invalid token - subject and issuer do not match')
#         error_response = backend.get_401_response(
#             'Unauthorized: Subject and Issuer do not match', request=request
#         )
#     except Exception:
#         logger.exception('An error occured while checking an asap token')
#         raise
# 
#     if error_response is not None and settings.ASAP_REQUIRED:
#         return error_response
# 
# Path: atlassian_jwt_auth/frameworks/django/backend.py
# class DjangoBackend(Backend):
#     def get_authorization_header(self, request=None):
#         if request is None:
#             raise ValueError('No request available')
# 
#         return request.META.get('HTTP_AUTHORIZATION', b'')
# 
#     def get_401_response(self, data=None, headers=None, request=None):
#         if headers is None:
#             headers = {}
# 
#         headers.update(self.default_headers_401)
# 
#         response = HttpResponse(content=data, status=401)
#         for k, v in headers.items():
#             response[k] = v
# 
#         return response
# 
#     def get_403_response(self, data=None, headers=None, request=None):
#         if headers is None:
#             headers = {}
# 
#         response = HttpResponseForbidden(data)
#         for k, v in headers.items():
#             response[k] = v
# 
#         return response
# 
#     def set_asap_claims_for_request(self, request, claims):
#         request.asap_claims = claims
# 
#     @property
#     def settings(self):
#         settings = {}
#         settings.update(self.default_settings)
# 
#         for k in settings.keys():
#             value = getattr(django_settings, k, None)
#             if value is None:
#                 continue
# 
#             settings[k] = value
# 
#         return self._process_settings(settings)


# Path: atlassian_jwt_auth/frameworks/django/middleware.py
from ..common.asap import _process_asap_token
from .backend import DjangoBackend



def asap_middleware(get_response):
    """Middleware to enable ASAP for all requests"""
    backend = DjangoBackend()
    settings = backend.settings
    _verifier = backend.get_verifier(settings=settings)

    def middleware(request):
        error_response = _process_asap_token(request, backend, settings,
                                             verifier=_verifier)
        if error_response is not None:
            return error_response

        return get_response(request)

    return middleware


class OldStyleASAPMiddleware(object):
    """Middleware to enable ASAP for all requests (for legacy applications
    using MIDDLEWARE_CLASSES)"""

    def __init__(self):

        self.backend = DjangoBackend()