2018-11-16 08:10:35 +00:00
|
|
|
"""passbook SAML IDP Views"""
|
2020-02-14 14:34:24 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.core.exceptions import PermissionDenied
|
2018-11-16 08:10:35 +00:00
|
|
|
from django.core.validators import URLValidator
|
2020-02-18 09:57:30 +00:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2018-12-16 16:09:26 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render, reverse
|
2019-02-27 11:36:18 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
2020-06-07 14:35:08 +00:00
|
|
|
from django.utils.http import urlencode
|
2018-12-16 16:09:26 +00:00
|
|
|
from django.views import View
|
2019-02-27 11:36:18 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2018-12-26 20:55:14 +00:00
|
|
|
from signxml.util import strip_pem_header
|
2019-10-01 08:24:10 +00:00
|
|
|
from structlog import get_logger
|
2018-12-26 20:55:14 +00:00
|
|
|
|
2019-12-05 15:14:08 +00:00
|
|
|
from passbook.audit.models import Event, EventAction
|
2020-02-20 16:04:20 +00:00
|
|
|
from passbook.core.models import Application, Provider
|
2020-06-07 14:35:08 +00:00
|
|
|
from passbook.flows.models import in_memory_stage
|
|
|
|
from passbook.flows.planner import (
|
|
|
|
PLAN_CONTEXT_APPLICATION,
|
|
|
|
PLAN_CONTEXT_SSO,
|
|
|
|
FlowPlanner,
|
|
|
|
)
|
|
|
|
from passbook.flows.stage import StageView
|
|
|
|
from passbook.flows.views import SESSION_KEY_PLAN
|
2018-11-16 10:41:14 +00:00
|
|
|
from passbook.lib.utils.template import render_to_string
|
2020-06-07 14:35:08 +00:00
|
|
|
from passbook.lib.utils.urls import redirect_with_qs
|
2020-02-18 09:57:30 +00:00
|
|
|
from passbook.lib.views import bad_request_message
|
2020-07-01 21:18:10 +00:00
|
|
|
from passbook.policies.mixins import PolicyAccessMixin
|
2020-02-24 16:34:52 +00:00
|
|
|
from passbook.providers.saml.exceptions import CannotHandleAssertion
|
2020-06-07 14:35:08 +00:00
|
|
|
from passbook.providers.saml.models import SAMLBindings, SAMLProvider
|
2020-02-18 09:57:30 +00:00
|
|
|
from passbook.providers.saml.processors.types import SAMLResponseParams
|
2020-06-20 20:30:45 +00:00
|
|
|
from passbook.stages.consent.stage import PLAN_CONTEXT_CONSENT_TEMPLATE
|
2018-11-16 08:10:35 +00:00
|
|
|
|
2019-10-04 08:08:53 +00:00
|
|
|
LOGGER = get_logger()
|
2019-12-31 11:51:16 +00:00
|
|
|
URL_VALIDATOR = URLValidator(schemes=("http", "https"))
|
2020-02-24 16:34:52 +00:00
|
|
|
SESSION_KEY_SAML_REQUEST = "SAMLRequest"
|
|
|
|
SESSION_KEY_SAML_RESPONSE = "SAMLResponse"
|
|
|
|
SESSION_KEY_RELAY_STATE = "RelayState"
|
|
|
|
SESSION_KEY_PARAMS = "SAMLParams"
|
2018-11-16 08:10:35 +00:00
|
|
|
|
|
|
|
|
2020-07-01 21:18:10 +00:00
|
|
|
class SAMLSSOView(LoginRequiredMixin, PolicyAccessMixin, View):
|
|
|
|
""""SAML SSO Base View, which plans a flow and injects our final stage.
|
|
|
|
Calls get/post handler."""
|
2018-12-26 20:55:14 +00:00
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
application: Application
|
|
|
|
provider: SAMLProvider
|
2018-12-26 20:55:14 +00:00
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
def dispatch(
|
|
|
|
self, request: HttpRequest, *args, application_slug: str, **kwargs
|
|
|
|
) -> HttpResponse:
|
|
|
|
self.application = get_object_or_404(Application, slug=application_slug)
|
|
|
|
self.provider: SAMLProvider = get_object_or_404(
|
|
|
|
SAMLProvider, pk=self.application.provider_id
|
2020-02-24 16:34:52 +00:00
|
|
|
)
|
2020-07-01 22:23:52 +00:00
|
|
|
if not self.user_has_access(self.application).passing:
|
2020-06-07 14:35:08 +00:00
|
|
|
raise PermissionDenied()
|
|
|
|
# Call the method handler, which checks the SAML Request
|
|
|
|
method_response = super().dispatch(request, *args, application_slug, **kwargs)
|
|
|
|
if method_response:
|
|
|
|
return method_response
|
|
|
|
# Regardless, we start the planner and return to it
|
|
|
|
planner = FlowPlanner(self.provider.authorization_flow)
|
|
|
|
planner.allow_empty_flows = True
|
|
|
|
plan = planner.plan(
|
|
|
|
self.request,
|
2020-06-20 20:30:45 +00:00
|
|
|
{
|
|
|
|
PLAN_CONTEXT_SSO: True,
|
|
|
|
PLAN_CONTEXT_APPLICATION: self.application,
|
|
|
|
PLAN_CONTEXT_CONSENT_TEMPLATE: "providers/saml/consent.html",
|
|
|
|
},
|
2020-06-07 14:35:08 +00:00
|
|
|
)
|
2020-06-20 17:40:25 +00:00
|
|
|
plan.append(in_memory_stage(SAMLFlowFinalView))
|
2020-06-07 14:35:08 +00:00
|
|
|
self.request.session[SESSION_KEY_PLAN] = plan
|
|
|
|
return redirect_with_qs(
|
|
|
|
"passbook_flows:flow-executor-shell",
|
|
|
|
self.request.GET,
|
|
|
|
flow_slug=self.provider.authorization_flow.slug,
|
|
|
|
)
|
2019-03-10 01:07:48 +00:00
|
|
|
|
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
class SAMLSSOBindingRedirectView(SAMLSSOView):
|
|
|
|
"""SAML Handler for SSO/Redirect bindings, which are sent via GET"""
|
2018-11-16 08:10:35 +00:00
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
# pylint: disable=unused-argument
|
2020-06-15 09:56:20 +00:00
|
|
|
def get( # lgtm [py/similar-function]
|
2020-06-07 14:35:08 +00:00
|
|
|
self, request: HttpRequest, application_slug: str
|
|
|
|
) -> Optional[HttpResponse]:
|
|
|
|
"""Handle REDIRECT bindings"""
|
2020-02-14 14:34:24 +00:00
|
|
|
# Store these values now, because Django's login cycle won't preserve them.
|
2020-06-07 14:35:08 +00:00
|
|
|
if SESSION_KEY_SAML_REQUEST not in request.GET:
|
|
|
|
LOGGER.info("handle_saml_request: SAML payload missing")
|
2020-02-25 10:38:26 +00:00
|
|
|
return bad_request_message(
|
|
|
|
self.request, "The SAML request payload is missing."
|
|
|
|
)
|
2018-11-16 08:10:35 +00:00
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
self.request.session[SESSION_KEY_SAML_REQUEST] = request.GET[
|
|
|
|
SESSION_KEY_SAML_REQUEST
|
|
|
|
]
|
|
|
|
self.request.session[SESSION_KEY_RELAY_STATE] = request.GET.get(
|
2020-02-24 16:34:52 +00:00
|
|
|
SESSION_KEY_RELAY_STATE, ""
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2020-02-25 10:38:26 +00:00
|
|
|
self.provider.processor.can_handle(self.request)
|
2020-02-24 16:34:52 +00:00
|
|
|
params = self.provider.processor.generate_response()
|
2020-02-25 10:38:26 +00:00
|
|
|
self.request.session[SESSION_KEY_PARAMS] = params
|
2020-02-24 16:34:52 +00:00
|
|
|
except CannotHandleAssertion as exc:
|
2020-02-24 18:14:43 +00:00
|
|
|
LOGGER.info(exc)
|
2020-06-07 14:35:08 +00:00
|
|
|
return bad_request_message(self.request, str(exc))
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(csrf_exempt, name="dispatch")
|
|
|
|
class SAMLSSOBindingPOSTView(SAMLSSOView):
|
|
|
|
"""SAML Handler for SSO/POST bindings"""
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def post(
|
|
|
|
self, request: HttpRequest, application_slug: str
|
|
|
|
) -> Optional[HttpResponse]:
|
|
|
|
"""Handle POST bindings"""
|
|
|
|
# Store these values now, because Django's login cycle won't preserve them.
|
|
|
|
if SESSION_KEY_SAML_REQUEST not in request.POST:
|
|
|
|
LOGGER.info("handle_saml_request: SAML payload missing")
|
2020-02-24 16:34:52 +00:00
|
|
|
return bad_request_message(
|
2020-06-07 14:35:08 +00:00
|
|
|
self.request, "The SAML request payload is missing."
|
2020-02-24 16:34:52 +00:00
|
|
|
)
|
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
self.request.session[SESSION_KEY_SAML_REQUEST] = request.POST[
|
|
|
|
SESSION_KEY_SAML_REQUEST
|
|
|
|
]
|
|
|
|
self.request.session[SESSION_KEY_RELAY_STATE] = request.POST.get(
|
|
|
|
SESSION_KEY_RELAY_STATE, ""
|
2020-02-24 16:34:52 +00:00
|
|
|
)
|
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
try:
|
|
|
|
self.provider.processor.can_handle(self.request)
|
|
|
|
params = self.provider.processor.generate_response()
|
|
|
|
self.request.session[SESSION_KEY_PARAMS] = params
|
|
|
|
except CannotHandleAssertion as exc:
|
|
|
|
LOGGER.info(exc)
|
|
|
|
return bad_request_message(self.request, str(exc))
|
|
|
|
return None
|
2020-02-25 10:38:26 +00:00
|
|
|
|
2020-02-24 16:34:52 +00:00
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
class SAMLSSOBindingInitView(SAMLSSOView):
|
|
|
|
"""SAML Handler for for IdP Initiated login flows"""
|
2020-02-24 16:34:52 +00:00
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def get(
|
|
|
|
self, request: HttpRequest, application_slug: str
|
|
|
|
) -> Optional[HttpResponse]:
|
|
|
|
"""Create saml params from scratch"""
|
|
|
|
LOGGER.debug(
|
|
|
|
"handle_saml_no_request: No SAML Request, using IdP-initiated flow."
|
|
|
|
)
|
2020-02-24 16:34:52 +00:00
|
|
|
self.provider.processor.is_idp_initiated = True
|
2020-06-07 14:35:08 +00:00
|
|
|
self.provider.processor.init_deep_link(self.request)
|
2020-02-24 16:34:52 +00:00
|
|
|
params = self.provider.processor.generate_response()
|
2020-06-07 14:35:08 +00:00
|
|
|
self.request.session[SESSION_KEY_PARAMS] = params
|
2018-11-16 08:10:35 +00:00
|
|
|
|
2020-02-18 09:57:30 +00:00
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
# This View doesn't have a URL on purpose, as its called by the FlowExecutor
|
|
|
|
class SAMLFlowFinalView(StageView):
|
|
|
|
"""View used by FlowExecutor after all stages have passed. Logs the authorization,
|
|
|
|
and redirects to the SP (if REDIRECT is configured) or shows and auto-submit for
|
|
|
|
(if POST is configured)."""
|
2019-02-27 11:36:18 +00:00
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
|
|
|
application: Application = self.executor.plan.context[PLAN_CONTEXT_APPLICATION]
|
2020-06-20 19:49:48 +00:00
|
|
|
provider: SAMLProvider = get_object_or_404(
|
|
|
|
SAMLProvider, pk=application.provider_id
|
|
|
|
)
|
2020-02-24 16:34:52 +00:00
|
|
|
# Log Application Authorization
|
|
|
|
Event.new(
|
|
|
|
EventAction.AUTHORIZE_APPLICATION,
|
2020-06-07 14:35:08 +00:00
|
|
|
authorized_application=application,
|
|
|
|
flow=self.executor.plan.flow_pk,
|
2020-02-24 16:34:52 +00:00
|
|
|
).from_http(self.request)
|
|
|
|
self.request.session.pop(SESSION_KEY_SAML_REQUEST, None)
|
|
|
|
self.request.session.pop(SESSION_KEY_SAML_RESPONSE, None)
|
|
|
|
self.request.session.pop(SESSION_KEY_RELAY_STATE, None)
|
2020-06-07 14:35:08 +00:00
|
|
|
if SESSION_KEY_PARAMS not in self.request.session:
|
|
|
|
return self.executor.stage_invalid()
|
2020-02-24 16:34:52 +00:00
|
|
|
response: SAMLResponseParams = self.request.session.pop(SESSION_KEY_PARAMS)
|
2020-06-07 14:35:08 +00:00
|
|
|
|
|
|
|
if provider.sp_binding == SAMLBindings.POST:
|
|
|
|
return render(
|
|
|
|
self.request,
|
2020-06-20 19:49:16 +00:00
|
|
|
"providers/saml/autosubmit_form.html",
|
2020-06-07 14:35:08 +00:00
|
|
|
{
|
|
|
|
"url": response.acs_url,
|
|
|
|
"application": application,
|
|
|
|
"attrs": {
|
|
|
|
"ACSUrl": response.acs_url,
|
|
|
|
SESSION_KEY_SAML_RESPONSE: response.saml_response,
|
|
|
|
SESSION_KEY_RELAY_STATE: response.relay_state,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if provider.sp_binding == SAMLBindings.REDIRECT:
|
|
|
|
querystring = urlencode(
|
|
|
|
{
|
2020-02-24 16:34:52 +00:00
|
|
|
SESSION_KEY_SAML_RESPONSE: response.saml_response,
|
|
|
|
SESSION_KEY_RELAY_STATE: response.relay_state,
|
2020-06-07 14:35:08 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return redirect(f"{response.acs_url}?{querystring}")
|
|
|
|
return bad_request_message(request, "Invalid sp_binding specified")
|
2018-12-16 16:09:26 +00:00
|
|
|
|
2018-12-26 20:55:14 +00:00
|
|
|
|
2020-06-20 19:51:52 +00:00
|
|
|
class DescriptorDownloadView(View):
|
2018-11-16 10:41:14 +00:00
|
|
|
"""Replies with the XML Metadata IDSSODescriptor."""
|
2018-12-16 16:09:26 +00:00
|
|
|
|
2020-02-18 09:57:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_metadata(request: HttpRequest, provider: SAMLProvider) -> str:
|
|
|
|
"""Return rendered XML Metadata"""
|
|
|
|
entity_id = provider.issuer
|
2020-06-07 14:35:08 +00:00
|
|
|
saml_sso_binding_post = request.build_absolute_uri(
|
2019-12-31 11:51:16 +00:00
|
|
|
reverse(
|
2020-06-07 14:35:08 +00:00
|
|
|
"passbook_providers_saml:sso-post",
|
|
|
|
kwargs={"application_slug": provider.application.slug},
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
|
|
|
)
|
2020-06-07 14:35:08 +00:00
|
|
|
saml_sso_binding_redirect = request.build_absolute_uri(
|
2019-12-31 11:51:16 +00:00
|
|
|
reverse(
|
2020-06-07 14:35:08 +00:00
|
|
|
"passbook_providers_saml:sso-redirect",
|
|
|
|
kwargs={"application_slug": provider.application.slug},
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
|
|
|
)
|
2020-02-20 16:37:09 +00:00
|
|
|
subject_format = provider.processor.subject_format
|
2018-12-16 16:09:26 +00:00
|
|
|
ctx = {
|
2020-06-07 14:35:08 +00:00
|
|
|
"saml_sso_binding_post": saml_sso_binding_post,
|
|
|
|
"saml_sso_binding_redirect": saml_sso_binding_redirect,
|
2019-12-31 11:51:16 +00:00
|
|
|
"entity_id": entity_id,
|
2020-02-20 16:37:09 +00:00
|
|
|
"subject_format": subject_format,
|
2018-12-16 16:09:26 +00:00
|
|
|
}
|
2020-04-10 19:54:23 +00:00
|
|
|
if provider.signing_kp:
|
|
|
|
ctx["cert_public_key"] = strip_pem_header(
|
|
|
|
provider.signing_kp.certificate_data.replace("\r", "")
|
|
|
|
).replace("\n", "")
|
2020-06-20 19:49:16 +00:00
|
|
|
return render_to_string("providers/saml/xml/metadata.xml", ctx)
|
2020-02-18 09:57:43 +00:00
|
|
|
|
2020-06-07 14:35:08 +00:00
|
|
|
def get(self, request: HttpRequest, application_slug: str) -> HttpResponse:
|
2020-02-18 09:57:43 +00:00
|
|
|
"""Replies with the XML Metadata IDSSODescriptor."""
|
2020-06-20 19:51:52 +00:00
|
|
|
application = get_object_or_404(Application, slug=application_slug)
|
|
|
|
provider: SAMLProvider = get_object_or_404(
|
|
|
|
SAMLProvider, pk=application.provider_id
|
2020-06-07 14:35:08 +00:00
|
|
|
)
|
2020-02-20 16:04:20 +00:00
|
|
|
try:
|
2020-06-20 19:51:52 +00:00
|
|
|
metadata = DescriptorDownloadView.get_metadata(request, provider)
|
2020-02-20 16:04:20 +00:00
|
|
|
except Provider.application.RelatedObjectDoesNotExist: # pylint: disable=no-member
|
|
|
|
return bad_request_message(
|
|
|
|
request, "Provider is not assigned to an application."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
response = HttpResponse(metadata, content_type="application/xml")
|
2020-02-24 16:34:52 +00:00
|
|
|
response[
|
|
|
|
"Content-Disposition"
|
2020-06-20 19:51:52 +00:00
|
|
|
] = f'attachment; filename="{provider.name}_passbook_meta.xml"'
|
2020-02-20 16:04:20 +00:00
|
|
|
return response
|