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(source="application.slug", allow_null=True) 22 assigned_application_name = ReadOnlyField(source="application.name", allow_null=True) 23 assigned_backchannel_application_slug = ReadOnlyField( 24 source="backchannel_application.slug", allow_null=True 25 ) 26 assigned_backchannel_application_name = ReadOnlyField( 27 source="backchannel_application.name", allow_null=True 28 ) 29 30 component = SerializerMethodField() 31 32 def get_component(self, obj: Provider) -> str: # pragma: no cover 33 """Get object component so that we know how to edit the object""" 34 if obj.__class__ == Provider: 35 return "" 36 return obj.component 37 38 class Meta: 39 model = Provider 40 fields = [ 41 "pk", 42 "name", 43 "authentication_flow", 44 "authorization_flow", 45 "invalidation_flow", 46 "property_mappings", 47 "component", 48 "assigned_application_slug", 49 "assigned_application_name", 50 "assigned_backchannel_application_slug", 51 "assigned_backchannel_application_name", 52 "verbose_name", 53 "verbose_name_plural", 54 "meta_model_name", 55 ] 56 extra_kwargs = { 57 "authorization_flow": {"required": True, "allow_null": False}, 58 "invalidation_flow": {"required": True, "allow_null": False}, 59 } 60 61 62class ProviderFilter(FilterSet): 63 """Filter for providers""" 64 65 application__isnull = BooleanFilter(method="filter_application__isnull") 66 backchannel = BooleanFilter( 67 method="filter_backchannel", 68 label=_( 69 "When not set all providers are returned. When set to true, only backchannel " 70 "providers are returned. When set to false, backchannel providers are excluded" 71 ), 72 ) 73 74 def filter_application__isnull(self, queryset: QuerySet, name, value): 75 """Only return providers that are neither assigned to application, 76 both as provider or application provider""" 77 return queryset.filter( 78 Q(backchannel_application__isnull=value, is_backchannel=True) 79 | Q(application__isnull=value) 80 ) 81 82 def filter_backchannel(self, queryset: QuerySet, name, value): 83 """By default all providers are returned. When set to true, only backchannel providers are 84 returned. When set to false, backchannel providers are excluded""" 85 return queryset.filter(is_backchannel=value) 86 87 88class ProviderViewSet( 89 TypesMixin, 90 mixins.RetrieveModelMixin, 91 mixins.DestroyModelMixin, 92 UsedByMixin, 93 mixins.ListModelMixin, 94 GenericViewSet, 95): 96 """Provider Viewset""" 97 98 queryset = Provider.objects.none() 99 serializer_class = ProviderSerializer 100 filterset_class = ProviderFilter 101 search_fields = [ 102 "name", 103 "application__name", 104 ] 105 106 def get_queryset(self): # pragma: no cover 107 return Provider.objects.select_subclasses()
19class ProviderSerializer(ModelSerializer, MetaNameSerializer): 20 """Provider Serializer""" 21 22 assigned_application_slug = ReadOnlyField(source="application.slug", allow_null=True) 23 assigned_application_name = ReadOnlyField(source="application.name", allow_null=True) 24 assigned_backchannel_application_slug = ReadOnlyField( 25 source="backchannel_application.slug", allow_null=True 26 ) 27 assigned_backchannel_application_name = ReadOnlyField( 28 source="backchannel_application.name", allow_null=True 29 ) 30 31 component = SerializerMethodField() 32 33 def get_component(self, obj: Provider) -> str: # pragma: no cover 34 """Get object component so that we know how to edit the object""" 35 if obj.__class__ == Provider: 36 return "" 37 return obj.component 38 39 class Meta: 40 model = Provider 41 fields = [ 42 "pk", 43 "name", 44 "authentication_flow", 45 "authorization_flow", 46 "invalidation_flow", 47 "property_mappings", 48 "component", 49 "assigned_application_slug", 50 "assigned_application_name", 51 "assigned_backchannel_application_slug", 52 "assigned_backchannel_application_name", 53 "verbose_name", 54 "verbose_name_plural", 55 "meta_model_name", 56 ] 57 extra_kwargs = { 58 "authorization_flow": {"required": True, "allow_null": False}, 59 "invalidation_flow": {"required": True, "allow_null": False}, 60 }
Provider Serializer
39 class Meta: 40 model = Provider 41 fields = [ 42 "pk", 43 "name", 44 "authentication_flow", 45 "authorization_flow", 46 "invalidation_flow", 47 "property_mappings", 48 "component", 49 "assigned_application_slug", 50 "assigned_application_name", 51 "assigned_backchannel_application_slug", 52 "assigned_backchannel_application_name", 53 "verbose_name", 54 "verbose_name_plural", 55 "meta_model_name", 56 ] 57 extra_kwargs = { 58 "authorization_flow": {"required": True, "allow_null": False}, 59 "invalidation_flow": {"required": True, "allow_null": False}, 60 }
63class ProviderFilter(FilterSet): 64 """Filter for providers""" 65 66 application__isnull = BooleanFilter(method="filter_application__isnull") 67 backchannel = BooleanFilter( 68 method="filter_backchannel", 69 label=_( 70 "When not set all providers are returned. When set to true, only backchannel " 71 "providers are returned. When set to false, backchannel providers are excluded" 72 ), 73 ) 74 75 def filter_application__isnull(self, queryset: QuerySet, name, value): 76 """Only return providers that are neither assigned to application, 77 both as provider or application provider""" 78 return queryset.filter( 79 Q(backchannel_application__isnull=value, is_backchannel=True) 80 | Q(application__isnull=value) 81 ) 82 83 def filter_backchannel(self, queryset: QuerySet, name, value): 84 """By default all providers are returned. When set to true, only backchannel providers are 85 returned. When set to false, backchannel providers are excluded""" 86 return queryset.filter(is_backchannel=value)
Filter for providers
75 def filter_application__isnull(self, queryset: QuerySet, name, value): 76 """Only return providers that are neither assigned to application, 77 both as provider or application provider""" 78 return queryset.filter( 79 Q(backchannel_application__isnull=value, is_backchannel=True) 80 | Q(application__isnull=value) 81 )
Only return providers that are neither assigned to application, both as provider or application provider
83 def filter_backchannel(self, queryset: QuerySet, name, value): 84 """By default all providers are returned. When set to true, only backchannel providers are 85 returned. When set to false, backchannel providers are excluded""" 86 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
89class ProviderViewSet( 90 TypesMixin, 91 mixins.RetrieveModelMixin, 92 mixins.DestroyModelMixin, 93 UsedByMixin, 94 mixins.ListModelMixin, 95 GenericViewSet, 96): 97 """Provider Viewset""" 98 99 queryset = Provider.objects.none() 100 serializer_class = ProviderSerializer 101 filterset_class = ProviderFilter 102 search_fields = [ 103 "name", 104 "application__name", 105 ] 106 107 def get_queryset(self): # pragma: no cover 108 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)