authentik.sources.saml.api.source
SAMLSource API Views
1"""SAMLSource API Views""" 2 3from django.urls import reverse 4from django.utils.translation import gettext_lazy as _ 5from drf_spectacular.utils import extend_schema 6from rest_framework.decorators import action 7from rest_framework.fields import SerializerMethodField 8from rest_framework.request import Request 9from rest_framework.response import Response 10from rest_framework.serializers import ValidationError 11from rest_framework.viewsets import ModelViewSet 12 13from authentik.core.api.sources import SourceSerializer 14from authentik.core.api.used_by import UsedByMixin 15from authentik.crypto.validators import XML_SIGNING_KEY_TYPES, KeyTypeValidator 16from authentik.providers.saml.api.providers import SAMLMetadataSerializer 17from authentik.sources.saml.models import SAMLSource 18from authentik.sources.saml.processors.metadata import MetadataProcessor 19 20 21class SAMLSourceSerializer(SourceSerializer): 22 """SAMLSource Serializer""" 23 24 url_issuer = SerializerMethodField() 25 26 def get_url_issuer(self, instance: SAMLSource) -> str: 27 """Get the resolved Issuer, falling back to the metadata URL when unset""" 28 if "request" not in self._context: 29 return instance.issuer_override or "" 30 return instance.get_issuer(self._context["request"]._request) 31 32 def validate(self, attrs: dict): 33 if attrs.get("verification_kp"): 34 if not attrs.get("signed_assertion") and not attrs.get("signed_response"): 35 raise ValidationError( 36 _( 37 "With a Verification Certificate selected, at least one of" 38 " 'Verify Assertion Signature' or 'Verify Response Signature' " 39 "must be selected." 40 ) 41 ) 42 return super().validate(attrs) 43 44 class Meta: 45 model = SAMLSource 46 fields = SourceSerializer.Meta.fields + [ 47 "group_matching_mode", 48 "pre_authentication_flow", 49 "issuer_override", 50 "url_issuer", 51 "sso_url", 52 "slo_url", 53 "allow_idp_initiated", 54 "force_authn", 55 "name_id_policy", 56 "binding_type", 57 "verification_kp", 58 "signing_kp", 59 "digest_algorithm", 60 "signature_algorithm", 61 "temporary_user_delete_after", 62 "encryption_kp", 63 "signed_assertion", 64 "signed_response", 65 ] 66 extra_kwargs = { 67 "signing_kp": {"validators": [KeyTypeValidator(*XML_SIGNING_KEY_TYPES)]}, 68 "verification_kp": {"validators": [KeyTypeValidator(*XML_SIGNING_KEY_TYPES)]}, 69 "encryption_kp": {"validators": [KeyTypeValidator(*XML_SIGNING_KEY_TYPES)]}, 70 } 71 72 73class SAMLSourceViewSet(UsedByMixin, ModelViewSet): 74 """SAMLSource Viewset""" 75 76 queryset = SAMLSource.objects.all() 77 serializer_class = SAMLSourceSerializer 78 lookup_field = "slug" 79 filterset_fields = [ 80 "pbm_uuid", 81 "name", 82 "slug", 83 "enabled", 84 "authentication_flow", 85 "enrollment_flow", 86 "managed", 87 "policy_engine_mode", 88 "user_matching_mode", 89 "pre_authentication_flow", 90 "issuer_override", 91 "sso_url", 92 "slo_url", 93 "allow_idp_initiated", 94 "force_authn", 95 "name_id_policy", 96 "binding_type", 97 "verification_kp", 98 "signing_kp", 99 "digest_algorithm", 100 "signature_algorithm", 101 "temporary_user_delete_after", 102 "signed_assertion", 103 "signed_response", 104 ] 105 search_fields = ["name", "slug"] 106 ordering = ["name"] 107 108 @extend_schema(responses={200: SAMLMetadataSerializer(many=False)}) 109 @action(methods=["GET"], detail=True) 110 def metadata(self, request: Request, slug: str) -> Response: 111 """Return metadata as XML string""" 112 source = self.get_object() 113 metadata = MetadataProcessor(source, request).build_entity_descriptor() 114 return Response( 115 { 116 "metadata": metadata, 117 "download_url": reverse( 118 "authentik_sources_saml:metadata", 119 kwargs={ 120 "source_slug": source.slug, 121 }, 122 ), 123 } 124 )
22class SAMLSourceSerializer(SourceSerializer): 23 """SAMLSource Serializer""" 24 25 url_issuer = SerializerMethodField() 26 27 def get_url_issuer(self, instance: SAMLSource) -> str: 28 """Get the resolved Issuer, falling back to the metadata URL when unset""" 29 if "request" not in self._context: 30 return instance.issuer_override or "" 31 return instance.get_issuer(self._context["request"]._request) 32 33 def validate(self, attrs: dict): 34 if attrs.get("verification_kp"): 35 if not attrs.get("signed_assertion") and not attrs.get("signed_response"): 36 raise ValidationError( 37 _( 38 "With a Verification Certificate selected, at least one of" 39 " 'Verify Assertion Signature' or 'Verify Response Signature' " 40 "must be selected." 41 ) 42 ) 43 return super().validate(attrs) 44 45 class Meta: 46 model = SAMLSource 47 fields = SourceSerializer.Meta.fields + [ 48 "group_matching_mode", 49 "pre_authentication_flow", 50 "issuer_override", 51 "url_issuer", 52 "sso_url", 53 "slo_url", 54 "allow_idp_initiated", 55 "force_authn", 56 "name_id_policy", 57 "binding_type", 58 "verification_kp", 59 "signing_kp", 60 "digest_algorithm", 61 "signature_algorithm", 62 "temporary_user_delete_after", 63 "encryption_kp", 64 "signed_assertion", 65 "signed_response", 66 ] 67 extra_kwargs = { 68 "signing_kp": {"validators": [KeyTypeValidator(*XML_SIGNING_KEY_TYPES)]}, 69 "verification_kp": {"validators": [KeyTypeValidator(*XML_SIGNING_KEY_TYPES)]}, 70 "encryption_kp": {"validators": [KeyTypeValidator(*XML_SIGNING_KEY_TYPES)]}, 71 }
SAMLSource Serializer
27 def get_url_issuer(self, instance: SAMLSource) -> str: 28 """Get the resolved Issuer, falling back to the metadata URL when unset""" 29 if "request" not in self._context: 30 return instance.issuer_override or "" 31 return instance.get_issuer(self._context["request"]._request)
Get the resolved Issuer, falling back to the metadata URL when unset
def
validate(self, attrs: dict):
33 def validate(self, attrs: dict): 34 if attrs.get("verification_kp"): 35 if not attrs.get("signed_assertion") and not attrs.get("signed_response"): 36 raise ValidationError( 37 _( 38 "With a Verification Certificate selected, at least one of" 39 " 'Verify Assertion Signature' or 'Verify Response Signature' " 40 "must be selected." 41 ) 42 ) 43 return super().validate(attrs)
Inherited Members
class
SAMLSourceSerializer.Meta:
45 class Meta: 46 model = SAMLSource 47 fields = SourceSerializer.Meta.fields + [ 48 "group_matching_mode", 49 "pre_authentication_flow", 50 "issuer_override", 51 "url_issuer", 52 "sso_url", 53 "slo_url", 54 "allow_idp_initiated", 55 "force_authn", 56 "name_id_policy", 57 "binding_type", 58 "verification_kp", 59 "signing_kp", 60 "digest_algorithm", 61 "signature_algorithm", 62 "temporary_user_delete_after", 63 "encryption_kp", 64 "signed_assertion", 65 "signed_response", 66 ] 67 extra_kwargs = { 68 "signing_kp": {"validators": [KeyTypeValidator(*XML_SIGNING_KEY_TYPES)]}, 69 "verification_kp": {"validators": [KeyTypeValidator(*XML_SIGNING_KEY_TYPES)]}, 70 "encryption_kp": {"validators": [KeyTypeValidator(*XML_SIGNING_KEY_TYPES)]}, 71 }
model =
<class 'authentik.sources.saml.models.SAMLSource'>
fields =
['pk', 'name', 'slug', 'enabled', 'promoted', 'authentication_flow', 'enrollment_flow', 'user_property_mappings', 'group_property_mappings', 'component', 'verbose_name', 'verbose_name_plural', 'meta_model_name', 'policy_engine_mode', 'user_matching_mode', 'managed', 'user_path_template', 'icon', 'icon_url', 'icon_themed_urls', 'group_matching_mode', 'pre_authentication_flow', 'issuer_override', 'url_issuer', 'sso_url', 'slo_url', 'allow_idp_initiated', 'force_authn', 'name_id_policy', 'binding_type', 'verification_kp', 'signing_kp', 'digest_algorithm', 'signature_algorithm', 'temporary_user_delete_after', 'encryption_kp', 'signed_assertion', 'signed_response']
extra_kwargs =
{'signing_kp': {'validators': [<KeyTypeValidator(allowed=[KeyType.RSA, KeyType.EC, KeyType.DSA])>]}, 'verification_kp': {'validators': [<KeyTypeValidator(allowed=[KeyType.RSA, KeyType.EC, KeyType.DSA])>]}, 'encryption_kp': {'validators': [<KeyTypeValidator(allowed=[KeyType.RSA, KeyType.EC, KeyType.DSA])>]}}
class
SAMLSourceViewSet(authentik.core.api.used_by.UsedByMixin, rest_framework.viewsets.ModelViewSet):
74class SAMLSourceViewSet(UsedByMixin, ModelViewSet): 75 """SAMLSource Viewset""" 76 77 queryset = SAMLSource.objects.all() 78 serializer_class = SAMLSourceSerializer 79 lookup_field = "slug" 80 filterset_fields = [ 81 "pbm_uuid", 82 "name", 83 "slug", 84 "enabled", 85 "authentication_flow", 86 "enrollment_flow", 87 "managed", 88 "policy_engine_mode", 89 "user_matching_mode", 90 "pre_authentication_flow", 91 "issuer_override", 92 "sso_url", 93 "slo_url", 94 "allow_idp_initiated", 95 "force_authn", 96 "name_id_policy", 97 "binding_type", 98 "verification_kp", 99 "signing_kp", 100 "digest_algorithm", 101 "signature_algorithm", 102 "temporary_user_delete_after", 103 "signed_assertion", 104 "signed_response", 105 ] 106 search_fields = ["name", "slug"] 107 ordering = ["name"] 108 109 @extend_schema(responses={200: SAMLMetadataSerializer(many=False)}) 110 @action(methods=["GET"], detail=True) 111 def metadata(self, request: Request, slug: str) -> Response: 112 """Return metadata as XML string""" 113 source = self.get_object() 114 metadata = MetadataProcessor(source, request).build_entity_descriptor() 115 return Response( 116 { 117 "metadata": metadata, 118 "download_url": reverse( 119 "authentik_sources_saml:metadata", 120 kwargs={ 121 "source_slug": source.slug, 122 }, 123 ), 124 } 125 )
SAMLSource Viewset
serializer_class =
<class 'SAMLSourceSerializer'>
filterset_fields =
['pbm_uuid', 'name', 'slug', 'enabled', 'authentication_flow', 'enrollment_flow', 'managed', 'policy_engine_mode', 'user_matching_mode', 'pre_authentication_flow', 'issuer_override', 'sso_url', 'slo_url', 'allow_idp_initiated', 'force_authn', 'name_id_policy', 'binding_type', 'verification_kp', 'signing_kp', 'digest_algorithm', 'signature_algorithm', 'temporary_user_delete_after', 'signed_assertion', 'signed_response']
@extend_schema(responses={200: SAMLMetadataSerializer(many=False)})
@action(methods=['GET'], detail=True)
def
metadata( self, request: rest_framework.request.Request, slug: str) -> rest_framework.response.Response:
109 @extend_schema(responses={200: SAMLMetadataSerializer(many=False)}) 110 @action(methods=["GET"], detail=True) 111 def metadata(self, request: Request, slug: str) -> Response: 112 """Return metadata as XML string""" 113 source = self.get_object() 114 metadata = MetadataProcessor(source, request).build_entity_descriptor() 115 return Response( 116 { 117 "metadata": metadata, 118 "download_url": reverse( 119 "authentik_sources_saml:metadata", 120 kwargs={ 121 "source_slug": source.slug, 122 }, 123 ), 124 } 125 )
Return metadata as XML string