Closed alinjie closed 5 years ago
All my middlewares, in case that is relevant:
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_auth_adfs.middleware.LoginRequiredMiddleware',
'django.middleware.security.SecurityMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
]
The LoginRequiredMiddleware is meant for vanilla Django pages. Not for Django Rest Framework.
DRF uses a different system for authenticating.
If you want to mix regular Django views with DRF, then you should exclude the API path from the LoginRequiredMiddleware config. See https://django-auth-adfs.readthedocs.io/en/latest/settings_ref.html#login-exempt-urls
I see! I'll create my own custom middleware then.
Thanks for the quick reply! :)
Related to this issue, I've encountered another problem.
I've made the following middleware to ensure that the request is authenticated:
def require_auth(get_response):
# One-time configuration and initialization.
def middleware(request):
# Code to be executed for each request before
# the view (and later middleware) are called.
if not request.user.is_authenticated and request.method != "OPTIONS":
if not request.path in settings.EXCLUDED_PAHTS:
return HttpResponse("Unauthorized", 401)
response = get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
return middleware
The middlewares are applied in the following order (my custom middleware last):
MIDDLEWARE = [
"elasticapm.contrib.django.middleware.TracingMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.middleware.security.SecurityMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"compliancedash.custom_middleware.require_auth.require_auth",
]
When inspecting the request, the user.is_authenticated
variable is set to false, even though the access token is included in the headers. It seems to me like the authentication process happens after my custom middleware is applied.
When inspecting the request, without the middleware applied, in a DRF view, the user.is_authenticated
variable is true and the authenticated user is resolved correctly.
Am I missing something? Is there any way to "manually" authenticate the user, perhaps?
My bad. I added this, which solves my issue:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
Hi,
I've set up Azure AD authentication by following the guide provided in the docs. It seems to be working fine, as claims are resolved etc when requesting a DRF endpoint with the access token in the header.
When I apply the
django_auth_adfs.middleware.LoginRequiredMiddleware
middleware, every request is redirected with a status code of302
, indicating that the request is not authenticated even though the access token is in the header and the request should be authenticated.Configuration
settings.py
Debug console output (without middleware applied)
Console Output (With middleware applied):
Both requests was made in the same session. The only difference is that the middleware was commented out
Do you have any ideas as to what is causing this? Any help would be appriciated!