authentik.core.api.providers
Provider API Views
1"""Provider API Views""" 2 3from django.db.models import QuerySet 4from django.db.models.query import Q 5from django.utils.translation import gettext_lazy as _ 6from django_filters.filters import BooleanFilter 7from django_filters.filterset import FilterSet 8from rest_framework import mixins 9from rest_framework.fields import ReadOnlyField, SerializerMethodField 10from rest_framework.viewsets import GenericViewSet 11 12from authentik.core.api.object_types import TypesMixin 13from authentik.core.api.used_by import UsedByMixin 14from authentik.core.api.utils import MetaNameSerializer, ModelSerializer 15from authentik.core.models import Provider 16 17 18class ProviderSerializer(ModelSerializer, MetaNameSerializer): 19 """Provider Serializer""" 20 21 assigned_application_slug = ReadOnlyField( 22 source="application.slug", allow_null=True, required=False 23 ) 24 assigned_application_name = ReadOnlyField( 25 source="application.name", allow_null=True, required=False 26 ) 27 assigned_backchannel_application_slug = ReadOnlyField( 28 source="backchannel_application.slug", allow_null=True, required=False 29 ) 30 assigned_backchannel_application_name = ReadOnlyField( 31 source="backchannel_application.name", allow_null=True, required=False 32 ) 33 34 component = SerializerMethodField() 35 36 def get_component(self, obj: Provider) -> str: # pragma: no cover 37 """Get object component so that we know how to edit the object""" 38 if obj.__class__ == Provider: 39 return "" 40 return obj.component 41 42 class Meta: 43 model = Provider 44 fields = [ 45 "pk", 46 "name", 47 "authentication_flow", 48 "authorization_flow", 49 "invalidation_flow", 50 "property_mappings", 51 "component", 52 "assigned_application_slug", 53 "assigned_application_name", 54 "assigned_backchannel_application_slug", 55 "assigned_backchannel_application_name", 56 "verbose_name", 57 "verbose_name_plural", 58 "meta_model_name", 59 ] 60 # This serializer is only a general read serializer for listing/reading 61 # providers without their specific type, 62 # setting whether authorization_flow/invalidation_flow are required 63 # is up to the child serializer 64 extra_kwargs = {} 65 extra_write_kwargs = { 66 "authorization_flow": {"required": True, "allow_null": False}, 67 "invalidation_flow": {"required": True, "allow_null": False}, 68 } 69 70 71class ProviderFilter(FilterSet): 72 """Filter for providers""" 73 74 application__isnull = BooleanFilter(method="filter_application__isnull") 75 backchannel = BooleanFilter( 76 method="filter_backchannel", 77 label=_( 78 "When not set all providers are returned. When set to true, only backchannel " 79 "providers are returned. When set to false, backchannel providers are excluded" 80 ), 81 ) 82 83 def filter_application__isnull(self, queryset: QuerySet, name, value): 84 """Only return providers that are neither assigned to application, 85 both as provider or application provider""" 86 return queryset.filter( 87 Q(backchannel_application__isnull=value, is_backchannel=True) 88 | Q(application__isnull=value) 89 ) 90 91 def filter_backchannel(self, queryset: QuerySet, name, value): 92 """By default all providers are returned. When set to true, only backchannel providers are 93 returned. When set to false, backchannel providers are excluded""" 94 return queryset.filter(is_backchannel=value) 95 96 97class ProviderViewSet( 98 TypesMixin, 99 mixins.RetrieveModelMixin, 100 mixins.DestroyModelMixin, 101 UsedByMixin, 102 mixins.ListModelMixin, 103 GenericViewSet, 104): 105 """Provider Viewset""" 106 107 queryset = Provider.objects.none() 108 serializer_class = ProviderSerializer 109 filterset_class = ProviderFilter 110 search_fields = [ 111 "name", 112 "application__name", 113 ] 114 115 def get_queryset(self): # pragma: no cover 116 return Provider.objects.select_subclasses()
19class ProviderSerializer(ModelSerializer, MetaNameSerializer): 20 """Provider Serializer""" 21 22 assigned_application_slug = ReadOnlyField( 23 source="application.slug", allow_null=True, required=False 24 ) 25 assigned_application_name = ReadOnlyField( 26 source="application.name", allow_null=True, required=False 27 ) 28 assigned_backchannel_application_slug = ReadOnlyField( 29 source="backchannel_application.slug", allow_null=True, required=False 30 ) 31 assigned_backchannel_application_name = ReadOnlyField( 32 source="backchannel_application.name", allow_null=True, required=False 33 ) 34 35 component = SerializerMethodField() 36 37 def get_component(self, obj: Provider) -> str: # pragma: no cover 38 """Get object component so that we know how to edit the object""" 39 if obj.__class__ == Provider: 40 return "" 41 return obj.component 42 43 class Meta: 44 model = Provider 45 fields = [ 46 "pk", 47 "name", 48 "authentication_flow", 49 "authorization_flow", 50 "invalidation_flow", 51 "property_mappings", 52 "component", 53 "assigned_application_slug", 54 "assigned_application_name", 55 "assigned_backchannel_application_slug", 56 "assigned_backchannel_application_name", 57 "verbose_name", 58 "verbose_name_plural", 59 "meta_model_name", 60 ] 61 # This serializer is only a general read serializer for listing/reading 62 # providers without their specific type, 63 # setting whether authorization_flow/invalidation_flow are required 64 # is up to the child serializer 65 extra_kwargs = {} 66 extra_write_kwargs = { 67 "authorization_flow": {"required": True, "allow_null": False}, 68 "invalidation_flow": {"required": True, "allow_null": False}, 69 }
Provider Serializer
37 def get_component(self, obj: Provider) -> str: # pragma: no cover 38 """Get object component so that we know how to edit the object""" 39 if obj.__class__ == Provider: 40 return "" 41 return obj.component
Get object component so that we know how to edit the object
Inherited Members
43 class Meta: 44 model = Provider 45 fields = [ 46 "pk", 47 "name", 48 "authentication_flow", 49 "authorization_flow", 50 "invalidation_flow", 51 "property_mappings", 52 "component", 53 "assigned_application_slug", 54 "assigned_application_name", 55 "assigned_backchannel_application_slug", 56 "assigned_backchannel_application_name", 57 "verbose_name", 58 "verbose_name_plural", 59 "meta_model_name", 60 ] 61 # This serializer is only a general read serializer for listing/reading 62 # providers without their specific type, 63 # setting whether authorization_flow/invalidation_flow are required 64 # is up to the child serializer 65 extra_kwargs = {} 66 extra_write_kwargs = { 67 "authorization_flow": {"required": True, "allow_null": False}, 68 "invalidation_flow": {"required": True, "allow_null": False}, 69 }
72class ProviderFilter(FilterSet): 73 """Filter for providers""" 74 75 application__isnull = BooleanFilter(method="filter_application__isnull") 76 backchannel = BooleanFilter( 77 method="filter_backchannel", 78 label=_( 79 "When not set all providers are returned. When set to true, only backchannel " 80 "providers are returned. When set to false, backchannel providers are excluded" 81 ), 82 ) 83 84 def filter_application__isnull(self, queryset: QuerySet, name, value): 85 """Only return providers that are neither assigned to application, 86 both as provider or application provider""" 87 return queryset.filter( 88 Q(backchannel_application__isnull=value, is_backchannel=True) 89 | Q(application__isnull=value) 90 ) 91 92 def filter_backchannel(self, queryset: QuerySet, name, value): 93 """By default all providers are returned. When set to true, only backchannel providers are 94 returned. When set to false, backchannel providers are excluded""" 95 return queryset.filter(is_backchannel=value)
Filter for providers
84 def filter_application__isnull(self, queryset: QuerySet, name, value): 85 """Only return providers that are neither assigned to application, 86 both as provider or application provider""" 87 return queryset.filter( 88 Q(backchannel_application__isnull=value, is_backchannel=True) 89 | Q(application__isnull=value) 90 )
Only return providers that are neither assigned to application, both as provider or application provider
92 def filter_backchannel(self, queryset: QuerySet, name, value): 93 """By default all providers are returned. When set to true, only backchannel providers are 94 returned. When set to false, backchannel providers are excluded""" 95 return queryset.filter(is_backchannel=value)
By default all providers are returned. When set to true, only backchannel providers are returned. When set to false, backchannel providers are excluded
98class ProviderViewSet( 99 TypesMixin, 100 mixins.RetrieveModelMixin, 101 mixins.DestroyModelMixin, 102 UsedByMixin, 103 mixins.ListModelMixin, 104 GenericViewSet, 105): 106 """Provider Viewset""" 107 108 queryset = Provider.objects.none() 109 serializer_class = ProviderSerializer 110 filterset_class = ProviderFilter 111 search_fields = [ 112 "name", 113 "application__name", 114 ] 115 116 def get_queryset(self): # pragma: no cover 117 return Provider.objects.select_subclasses()
Provider Viewset
Get the list of items for this view.
This must be an iterable, and may be a queryset.
Defaults to using self.queryset.
This method should always be used rather than accessing self.queryset
directly, as self.queryset gets evaluated only once, and those results
are cached for all subsequent requests.
You may want to override this if you need to provide different querysets depending on the incoming request.
(Eg. return a list of items that is specific to the user)