Merge pull request #30357 from openedx/asheehan-edx/saml-api-related-improvements

fix: improvments to the saml config and data endpoints
This commit is contained in:
Alexander J Sheehan
2022-05-09 11:48:14 -04:00
committed by GitHub
2 changed files with 26 additions and 2 deletions

View File

@@ -61,6 +61,24 @@ class SAMLProviderConfigViewSet(PermissionRequiredMixin, SAMLProviderMixin, view
slug_list = [idp.provider_id for idp in enterprise_customer_idps]
return [config for config in SAMLProviderConfig.objects.current_set() if config.provider_id in slug_list]
def destroy(self, request, *args, **kwargs):
saml_provider_config = self.get_object()
config_id = saml_provider_config.id
provider_config_provider_id = saml_provider_config.provider_id
customer_uuid = self.requested_enterprise_uuid
try:
enterprise_customer = EnterpriseCustomer.objects.get(pk=customer_uuid)
except EnterpriseCustomer.DoesNotExist:
raise ValidationError(f'Enterprise customer not found at uuid: {customer_uuid}') # lint-amnesty, pylint: disable=raise-missing-from
enterprise_saml_provider = EnterpriseCustomerIdentityProvider.objects.filter(
enterprise_customer=enterprise_customer,
provider_id=provider_config_provider_id,
)
enterprise_saml_provider.delete()
saml_provider_config.delete()
return Response(data=config_id, status=status.HTTP_200_OK)
@property
def requested_enterprise_uuid(self):
"""

View File

@@ -2,6 +2,7 @@
Viewset for auth/saml/v0/samlproviderdata
"""
import logging
from requests.exceptions import SSLError, MissingSchema
from django.http import Http404
from django.shortcuts import get_object_or_404
@@ -94,7 +95,7 @@ class SAMLProviderDataViewSet(PermissionRequiredMixin, SAMLProviderDataMixin, vi
"""
return self.requested_enterprise_uuid
@action(detail=False, methods=['post'])
@action(detail=False, methods=['post', 'put'])
def sync_provider_data(self, request):
"""
Creates or updates a SAMProviderData record using info fetched from remote SAML metadata
@@ -110,7 +111,12 @@ class SAMLProviderDataViewSet(PermissionRequiredMixin, SAMLProviderDataMixin, vi
return Response('metadata_url is required!', status.HTTP_400_BAD_REQUEST)
# part 1: fetch information from remote metadata based on metadataUrl in samlproviderconfig
xml = fetch_metadata_xml(metadata_url)
try:
xml = fetch_metadata_xml(metadata_url)
except (SSLError, MissingSchema) as ex:
msg = f'Could not verify provider metadata url. Exc type: {type(ex).__name__}'
log.warning(msg)
return Response(msg, status.HTTP_406_NOT_ACCEPTABLE)
# part 2: create/update samlproviderdata
log.info("Processing IdP with entityID %s", entity_id)