2019-11-07 16:02:56 +00:00
|
|
|
"""saml sp views"""
|
2020-06-07 14:35:08 +00:00
|
|
|
from django.contrib.auth import logout
|
2020-06-24 20:27:14 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2019-11-07 17:02:59 +00:00
|
|
|
from django.http import Http404, HttpRequest, HttpResponse
|
2020-06-07 14:35:08 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
2019-11-07 16:02:56 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
2020-06-07 14:35:08 +00:00
|
|
|
from django.utils.http import urlencode
|
2020-07-08 12:27:32 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-11-07 16:02:56 +00:00
|
|
|
from django.views import View
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2020-06-23 19:49:27 +00:00
|
|
|
from signxml import InvalidSignature
|
2019-11-07 16:02:56 +00:00
|
|
|
|
2020-02-20 20:33:45 +00:00
|
|
|
from passbook.lib.views import bad_request_message
|
2020-07-11 17:57:27 +00:00
|
|
|
from passbook.providers.saml.utils.encoding import nice64
|
2020-02-20 20:33:45 +00:00
|
|
|
from passbook.sources.saml.exceptions import (
|
|
|
|
MissingSAMLResponse,
|
|
|
|
UnsupportedNameIDFormat,
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
2020-06-07 14:35:08 +00:00
|
|
|
from passbook.sources.saml.models import SAMLBindingTypes, SAMLSource
|
2020-07-10 23:02:55 +00:00
|
|
|
from passbook.sources.saml.processors.metadata import MetadataProcessor
|
|
|
|
from passbook.sources.saml.processors.request import RequestProcessor
|
|
|
|
from passbook.sources.saml.processors.response import ResponseProcessor
|
2019-11-07 16:02:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InitiateView(View):
|
|
|
|
"""Get the Form with SAML Request, which sends us to the IDP"""
|
|
|
|
|
2020-02-18 21:12:51 +00:00
|
|
|
def get(self, request: HttpRequest, source_slug: str) -> HttpResponse:
|
2019-11-07 16:02:56 +00:00
|
|
|
"""Replies with an XHTML SSO Request."""
|
2020-02-18 21:12:51 +00:00
|
|
|
source: SAMLSource = get_object_or_404(SAMLSource, slug=source_slug)
|
2019-11-07 16:35:25 +00:00
|
|
|
if not source.enabled:
|
|
|
|
raise Http404
|
2020-06-24 19:50:30 +00:00
|
|
|
relay_state = request.GET.get("next", "")
|
2020-07-11 23:46:46 +00:00
|
|
|
auth_n_req = RequestProcessor(source, request, relay_state)
|
2020-07-08 12:18:08 +00:00
|
|
|
# If the source is configured for Redirect bindings, we can just redirect there
|
2020-06-07 14:35:08 +00:00
|
|
|
if source.binding_type == SAMLBindingTypes.Redirect:
|
2020-07-11 23:46:46 +00:00
|
|
|
url_args = urlencode(auth_n_req.build_auth_n_detached())
|
2020-06-24 20:27:14 +00:00
|
|
|
return redirect(f"{source.sso_url}?{url_args}")
|
2020-07-08 12:18:08 +00:00
|
|
|
# As POST Binding we show a form
|
2020-07-11 17:57:27 +00:00
|
|
|
saml_request = nice64(auth_n_req.build_auth_n())
|
2020-06-07 14:35:08 +00:00
|
|
|
if source.binding_type == SAMLBindingTypes.POST:
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"saml/sp/login.html",
|
|
|
|
{
|
2020-06-24 20:27:14 +00:00
|
|
|
"request_url": source.sso_url,
|
2020-07-10 23:02:55 +00:00
|
|
|
"request": saml_request,
|
2020-06-24 11:12:34 +00:00
|
|
|
"relay_state": relay_state,
|
2020-06-07 14:35:08 +00:00
|
|
|
"source": source,
|
|
|
|
},
|
|
|
|
)
|
2020-07-08 12:18:08 +00:00
|
|
|
# Or an auto-submit form
|
|
|
|
if source.binding_type == SAMLBindingTypes.POST_AUTO:
|
|
|
|
return render(
|
|
|
|
request,
|
2020-07-08 12:27:32 +00:00
|
|
|
"generic/autosubmit_form.html",
|
2020-07-08 12:18:08 +00:00
|
|
|
{
|
2020-07-08 12:27:32 +00:00
|
|
|
"title": _("Redirecting to %(app)s..." % {"app": source.name}),
|
2020-07-10 23:02:55 +00:00
|
|
|
"attrs": {"SAMLRequest": saml_request, "RelayState": relay_state},
|
2020-07-08 12:18:08 +00:00
|
|
|
"url": source.sso_url,
|
|
|
|
},
|
|
|
|
)
|
2020-06-07 14:35:08 +00:00
|
|
|
raise Http404
|
2019-11-07 16:02:56 +00:00
|
|
|
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
@method_decorator(csrf_exempt, name="dispatch")
|
2019-11-07 16:02:56 +00:00
|
|
|
class ACSView(View):
|
|
|
|
"""AssertionConsumerService, consume assertion and log user in"""
|
|
|
|
|
2020-02-18 21:12:51 +00:00
|
|
|
def post(self, request: HttpRequest, source_slug: str) -> HttpResponse:
|
2019-11-07 16:02:56 +00:00
|
|
|
"""Handles a POSTed SSO Assertion and logs the user in."""
|
2020-02-18 21:12:51 +00:00
|
|
|
source: SAMLSource = get_object_or_404(SAMLSource, slug=source_slug)
|
2019-11-07 16:35:25 +00:00
|
|
|
if not source.enabled:
|
|
|
|
raise Http404
|
2020-07-10 23:02:55 +00:00
|
|
|
processor = ResponseProcessor(source)
|
2020-02-20 20:33:45 +00:00
|
|
|
try:
|
|
|
|
processor.parse(request)
|
|
|
|
except MissingSAMLResponse as exc:
|
|
|
|
return bad_request_message(request, str(exc))
|
2020-06-23 19:49:27 +00:00
|
|
|
except InvalidSignature as exc:
|
|
|
|
return bad_request_message(request, str(exc))
|
2020-02-20 20:33:45 +00:00
|
|
|
|
|
|
|
try:
|
2020-06-07 14:35:08 +00:00
|
|
|
return processor.prepare_flow(request)
|
2020-02-20 20:33:45 +00:00
|
|
|
except UnsupportedNameIDFormat as exc:
|
|
|
|
return bad_request_message(request, str(exc))
|
2019-11-07 16:02:56 +00:00
|
|
|
|
|
|
|
|
2020-06-24 20:27:14 +00:00
|
|
|
class SLOView(LoginRequiredMixin, View):
|
2019-11-07 16:02:56 +00:00
|
|
|
"""Single-Logout-View"""
|
|
|
|
|
2020-02-18 21:12:51 +00:00
|
|
|
def dispatch(self, request: HttpRequest, source_slug: str) -> HttpResponse:
|
2019-11-07 16:02:56 +00:00
|
|
|
"""Replies with an XHTML SSO Request."""
|
2020-06-24 20:27:14 +00:00
|
|
|
# TODO: Replace with flows
|
2020-02-18 21:12:51 +00:00
|
|
|
source: SAMLSource = get_object_or_404(SAMLSource, slug=source_slug)
|
2019-11-07 16:35:25 +00:00
|
|
|
if not source.enabled:
|
|
|
|
raise Http404
|
2019-11-07 16:02:56 +00:00
|
|
|
logout(request)
|
2019-12-31 11:51:16 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"saml/sp/sso_single_logout.html",
|
2020-06-24 20:46:20 +00:00
|
|
|
{"idp_logout_url": source.slo_url},
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
2019-11-07 16:02:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MetadataView(View):
|
|
|
|
"""Return XML Metadata for IDP"""
|
|
|
|
|
2020-02-18 21:12:51 +00:00
|
|
|
def dispatch(self, request: HttpRequest, source_slug: str) -> HttpResponse:
|
2019-11-07 16:02:56 +00:00
|
|
|
"""Replies with the XML Metadata SPSSODescriptor."""
|
2020-02-18 21:12:51 +00:00
|
|
|
source: SAMLSource = get_object_or_404(SAMLSource, slug=source_slug)
|
2020-07-10 23:02:55 +00:00
|
|
|
metadata = MetadataProcessor(source, request).build_entity_descriptor()
|
|
|
|
return HttpResponse(metadata, content_type="text/xml")
|