authentik.sources.oauth.api.source
OAuth Source Serializer
1"""OAuth Source Serializer""" 2 3from typing import Any 4 5from django.urls.base import reverse_lazy 6from django_filters.filters import BooleanFilter 7from django_filters.filterset import FilterSet 8from drf_spectacular.types import OpenApiTypes 9from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_field 10from requests import RequestException 11from rest_framework.decorators import action 12from rest_framework.fields import BooleanField, CharField, ChoiceField, SerializerMethodField 13from rest_framework.request import Request 14from rest_framework.response import Response 15from rest_framework.serializers import ValidationError 16from rest_framework.viewsets import ModelViewSet 17 18from authentik.core.api.sources import SourceSerializer 19from authentik.core.api.used_by import UsedByMixin 20from authentik.core.api.utils import PassiveSerializer 21from authentik.lib.utils.http import get_http_session 22from authentik.sources.oauth.models import OAuthSource, PKCEMethod 23from authentik.sources.oauth.types.registry import SourceType, registry 24 25 26class SourceTypeSerializer(PassiveSerializer): 27 """Serializer for SourceType""" 28 29 name = CharField(required=True) 30 verbose_name = CharField(required=True) 31 urls_customizable = BooleanField() 32 request_token_url = CharField(read_only=True, allow_null=True) 33 authorization_url = CharField(read_only=True, allow_null=True) 34 access_token_url = CharField(read_only=True, allow_null=True) 35 profile_url = CharField(read_only=True, allow_null=True) 36 oidc_well_known_url = CharField(read_only=True, allow_null=True) 37 oidc_jwks_url = CharField(read_only=True, allow_null=True) 38 39 40class OAuthSourceSerializer(SourceSerializer): 41 """OAuth Source Serializer""" 42 43 provider_type = ChoiceField(choices=registry.get_name_tuple()) 44 callback_url = SerializerMethodField() 45 type = SerializerMethodField() 46 47 def get_callback_url(self, instance: OAuthSource) -> str: 48 """Get OAuth Callback URL""" 49 relative_url = reverse_lazy( 50 "authentik_sources_oauth:oauth-client-callback", 51 kwargs={"source_slug": instance.slug}, 52 ) 53 if "request" not in self.context: 54 return relative_url 55 return self.context["request"].build_absolute_uri(relative_url) 56 57 @extend_schema_field(SourceTypeSerializer) 58 def get_type(self, instance: OAuthSource) -> SourceTypeSerializer: 59 """Get source's type configuration""" 60 return SourceTypeSerializer(instance.source_type).data 61 62 def _get_value(self, attrs: dict[str, Any], key: str, default: Any = None) -> Any: 63 """Get a value from the request (attrs), falling back to the instance, then default""" 64 if key in attrs: 65 return attrs[key] 66 if self.instance: 67 return getattr(self.instance, key) 68 return default 69 70 def validate(self, attrs: dict) -> dict: 71 session = get_http_session() 72 provider_type_name = attrs.get( 73 "provider_type", 74 self.instance.provider_type if self.instance else None, 75 ) 76 source_type = registry.find_type(provider_type_name) 77 78 well_known = ( 79 self._get_value(attrs, "oidc_well_known_url") or source_type.oidc_well_known_url 80 ) 81 inferred_oidc_jwks_url = None 82 enabled = attrs.get("enabled", self.instance.enabled if self.instance else True) 83 84 if enabled and well_known and well_known != "": 85 try: 86 well_known_config = session.get(well_known) 87 well_known_config.raise_for_status() 88 except RequestException as exc: 89 text = exc.response.text if exc.response is not None else str(exc) 90 raise ValidationError({"oidc_well_known_url": text}) from None 91 config = well_known_config.json() 92 if "issuer" not in config: 93 raise ValidationError({"oidc_well_known_url": "Invalid well-known configuration"}) 94 field_map = { 95 # authentik field to oidc field 96 "authorization_url": "authorization_endpoint", 97 "access_token_url": "token_endpoint", 98 "profile_url": "userinfo_endpoint", 99 } 100 for ak_key, oidc_key in field_map.items(): 101 # Don't overwrite user-set values 102 if ak_key in attrs and attrs[ak_key]: 103 continue 104 attrs[ak_key] = config.get(oidc_key, "") 105 # code_challenge_methods_supported is a list per RFC 8414, not a 106 # single method. Pick one (prefer S256, the RFC-recommended method) 107 # rather than letting the list round-trip into the pkce TextField 108 # and later str() into the authorize URL as "['plain', 'S256']". 109 if not attrs.get("pkce"): 110 supported_methods = config.get("code_challenge_methods_supported") or [] 111 attrs["pkce"] = PKCEMethod.NONE 112 if isinstance(supported_methods, list): 113 if PKCEMethod.S256 in supported_methods: 114 attrs["pkce"] = PKCEMethod.S256 115 elif PKCEMethod.PLAIN in supported_methods: 116 attrs["pkce"] = PKCEMethod.PLAIN 117 inferred_oidc_jwks_url = config.get("jwks_uri", "") 118 119 # Prefer user-entered URL to inferred URL to default URL 120 jwks_url = attrs.get("oidc_jwks_url") or inferred_oidc_jwks_url or source_type.oidc_jwks_url 121 if enabled and jwks_url and jwks_url != "": 122 attrs["oidc_jwks_url"] = jwks_url 123 try: 124 jwks_config = session.get(jwks_url) 125 jwks_config.raise_for_status() 126 except RequestException as exc: 127 text = exc.response.text if exc.response is not None else str(exc) 128 raise ValidationError({"oidc_jwks_url": text}) from None 129 config = jwks_config.json() 130 attrs["oidc_jwks"] = config 131 132 for url in [ 133 "authorization_url", 134 "access_token_url", 135 "profile_url", 136 ]: 137 if getattr(source_type, url, None) is None: 138 if url not in attrs: 139 raise ValidationError( 140 f"{url} is required for provider {source_type.verbose_name}" 141 ) 142 consumer_secret = self._get_value(attrs, "consumer_secret") 143 pkce = self._get_value(attrs, "pkce", PKCEMethod.NONE) 144 if source_type.requires_client_secret and not consumer_secret: 145 raise ValidationError({"consumer_secret": "Consumer secret is required."}) 146 if not consumer_secret and pkce == PKCEMethod.NONE: 147 raise ValidationError({"pkce": "PKCE is required when no consumer secret is used."}) 148 return attrs 149 150 class Meta: 151 model = OAuthSource 152 fields = SourceSerializer.Meta.fields + [ 153 "group_matching_mode", 154 "provider_type", 155 "request_token_url", 156 "authorization_url", 157 "access_token_url", 158 "profile_url", 159 "pkce", 160 "consumer_key", 161 "consumer_secret", 162 "callback_url", 163 "additional_scopes", 164 "type", 165 "oidc_well_known_url", 166 "oidc_jwks_url", 167 "oidc_jwks", 168 "authorization_code_auth_method", 169 ] 170 extra_kwargs = { 171 "consumer_secret": {"write_only": True, "allow_blank": True, "required": False}, 172 "request_token_url": {"allow_blank": True}, 173 "authorization_url": {"allow_blank": True}, 174 "access_token_url": {"allow_blank": True}, 175 "profile_url": {"allow_blank": True}, 176 } 177 178 179class OAuthSourceFilter(FilterSet): 180 """OAuth Source filter set""" 181 182 has_jwks = BooleanFilter(label="Only return sources with JWKS data", method="filter_has_jwks") 183 184 def filter_has_jwks(self, queryset, name, value): # pragma: no cover 185 """Only return sources with JWKS data""" 186 return queryset.exclude(oidc_jwks__iexact="{}") 187 188 class Meta: 189 model = OAuthSource 190 fields = [ 191 "pbm_uuid", 192 "name", 193 "slug", 194 "enabled", 195 "authentication_flow", 196 "enrollment_flow", 197 "policy_engine_mode", 198 "user_matching_mode", 199 "group_matching_mode", 200 "provider_type", 201 "request_token_url", 202 "authorization_url", 203 "access_token_url", 204 "profile_url", 205 "consumer_key", 206 "additional_scopes", 207 ] 208 209 210class OAuthSourceViewSet(UsedByMixin, ModelViewSet): 211 """Source Viewset""" 212 213 queryset = OAuthSource.objects.all() 214 serializer_class = OAuthSourceSerializer 215 lookup_field = "slug" 216 filterset_class = OAuthSourceFilter 217 search_fields = ["name", "slug"] 218 ordering = ["name"] 219 220 @extend_schema( 221 responses={200: SourceTypeSerializer(many=True)}, 222 parameters=[ 223 OpenApiParameter( 224 name="name", 225 location=OpenApiParameter.QUERY, 226 type=OpenApiTypes.STR, 227 ) 228 ], 229 ) 230 @action(detail=False, pagination_class=None, filter_backends=[]) 231 def source_types(self, request: Request) -> Response: 232 """Get all creatable source types. If ?name is set, only returns the type for <name>. 233 If <name> isn't found, returns the default type.""" 234 data = [] 235 if "name" in request.query_params: 236 source_type = registry.find_type(request.query_params.get("name")) 237 if source_type.__class__ != SourceType: 238 data.append(SourceTypeSerializer(source_type).data) 239 else: 240 for source_type in registry.get(): 241 data.append(SourceTypeSerializer(source_type).data) 242 return Response(data)
27class SourceTypeSerializer(PassiveSerializer): 28 """Serializer for SourceType""" 29 30 name = CharField(required=True) 31 verbose_name = CharField(required=True) 32 urls_customizable = BooleanField() 33 request_token_url = CharField(read_only=True, allow_null=True) 34 authorization_url = CharField(read_only=True, allow_null=True) 35 access_token_url = CharField(read_only=True, allow_null=True) 36 profile_url = CharField(read_only=True, allow_null=True) 37 oidc_well_known_url = CharField(read_only=True, allow_null=True) 38 oidc_jwks_url = CharField(read_only=True, allow_null=True)
Serializer for SourceType
Inherited Members
41class OAuthSourceSerializer(SourceSerializer): 42 """OAuth Source Serializer""" 43 44 provider_type = ChoiceField(choices=registry.get_name_tuple()) 45 callback_url = SerializerMethodField() 46 type = SerializerMethodField() 47 48 def get_callback_url(self, instance: OAuthSource) -> str: 49 """Get OAuth Callback URL""" 50 relative_url = reverse_lazy( 51 "authentik_sources_oauth:oauth-client-callback", 52 kwargs={"source_slug": instance.slug}, 53 ) 54 if "request" not in self.context: 55 return relative_url 56 return self.context["request"].build_absolute_uri(relative_url) 57 58 @extend_schema_field(SourceTypeSerializer) 59 def get_type(self, instance: OAuthSource) -> SourceTypeSerializer: 60 """Get source's type configuration""" 61 return SourceTypeSerializer(instance.source_type).data 62 63 def _get_value(self, attrs: dict[str, Any], key: str, default: Any = None) -> Any: 64 """Get a value from the request (attrs), falling back to the instance, then default""" 65 if key in attrs: 66 return attrs[key] 67 if self.instance: 68 return getattr(self.instance, key) 69 return default 70 71 def validate(self, attrs: dict) -> dict: 72 session = get_http_session() 73 provider_type_name = attrs.get( 74 "provider_type", 75 self.instance.provider_type if self.instance else None, 76 ) 77 source_type = registry.find_type(provider_type_name) 78 79 well_known = ( 80 self._get_value(attrs, "oidc_well_known_url") or source_type.oidc_well_known_url 81 ) 82 inferred_oidc_jwks_url = None 83 enabled = attrs.get("enabled", self.instance.enabled if self.instance else True) 84 85 if enabled and well_known and well_known != "": 86 try: 87 well_known_config = session.get(well_known) 88 well_known_config.raise_for_status() 89 except RequestException as exc: 90 text = exc.response.text if exc.response is not None else str(exc) 91 raise ValidationError({"oidc_well_known_url": text}) from None 92 config = well_known_config.json() 93 if "issuer" not in config: 94 raise ValidationError({"oidc_well_known_url": "Invalid well-known configuration"}) 95 field_map = { 96 # authentik field to oidc field 97 "authorization_url": "authorization_endpoint", 98 "access_token_url": "token_endpoint", 99 "profile_url": "userinfo_endpoint", 100 } 101 for ak_key, oidc_key in field_map.items(): 102 # Don't overwrite user-set values 103 if ak_key in attrs and attrs[ak_key]: 104 continue 105 attrs[ak_key] = config.get(oidc_key, "") 106 # code_challenge_methods_supported is a list per RFC 8414, not a 107 # single method. Pick one (prefer S256, the RFC-recommended method) 108 # rather than letting the list round-trip into the pkce TextField 109 # and later str() into the authorize URL as "['plain', 'S256']". 110 if not attrs.get("pkce"): 111 supported_methods = config.get("code_challenge_methods_supported") or [] 112 attrs["pkce"] = PKCEMethod.NONE 113 if isinstance(supported_methods, list): 114 if PKCEMethod.S256 in supported_methods: 115 attrs["pkce"] = PKCEMethod.S256 116 elif PKCEMethod.PLAIN in supported_methods: 117 attrs["pkce"] = PKCEMethod.PLAIN 118 inferred_oidc_jwks_url = config.get("jwks_uri", "") 119 120 # Prefer user-entered URL to inferred URL to default URL 121 jwks_url = attrs.get("oidc_jwks_url") or inferred_oidc_jwks_url or source_type.oidc_jwks_url 122 if enabled and jwks_url and jwks_url != "": 123 attrs["oidc_jwks_url"] = jwks_url 124 try: 125 jwks_config = session.get(jwks_url) 126 jwks_config.raise_for_status() 127 except RequestException as exc: 128 text = exc.response.text if exc.response is not None else str(exc) 129 raise ValidationError({"oidc_jwks_url": text}) from None 130 config = jwks_config.json() 131 attrs["oidc_jwks"] = config 132 133 for url in [ 134 "authorization_url", 135 "access_token_url", 136 "profile_url", 137 ]: 138 if getattr(source_type, url, None) is None: 139 if url not in attrs: 140 raise ValidationError( 141 f"{url} is required for provider {source_type.verbose_name}" 142 ) 143 consumer_secret = self._get_value(attrs, "consumer_secret") 144 pkce = self._get_value(attrs, "pkce", PKCEMethod.NONE) 145 if source_type.requires_client_secret and not consumer_secret: 146 raise ValidationError({"consumer_secret": "Consumer secret is required."}) 147 if not consumer_secret and pkce == PKCEMethod.NONE: 148 raise ValidationError({"pkce": "PKCE is required when no consumer secret is used."}) 149 return attrs 150 151 class Meta: 152 model = OAuthSource 153 fields = SourceSerializer.Meta.fields + [ 154 "group_matching_mode", 155 "provider_type", 156 "request_token_url", 157 "authorization_url", 158 "access_token_url", 159 "profile_url", 160 "pkce", 161 "consumer_key", 162 "consumer_secret", 163 "callback_url", 164 "additional_scopes", 165 "type", 166 "oidc_well_known_url", 167 "oidc_jwks_url", 168 "oidc_jwks", 169 "authorization_code_auth_method", 170 ] 171 extra_kwargs = { 172 "consumer_secret": {"write_only": True, "allow_blank": True, "required": False}, 173 "request_token_url": {"allow_blank": True}, 174 "authorization_url": {"allow_blank": True}, 175 "access_token_url": {"allow_blank": True}, 176 "profile_url": {"allow_blank": True}, 177 }
OAuth Source Serializer
48 def get_callback_url(self, instance: OAuthSource) -> str: 49 """Get OAuth Callback URL""" 50 relative_url = reverse_lazy( 51 "authentik_sources_oauth:oauth-client-callback", 52 kwargs={"source_slug": instance.slug}, 53 ) 54 if "request" not in self.context: 55 return relative_url 56 return self.context["request"].build_absolute_uri(relative_url)
Get OAuth Callback URL
@extend_schema_field(SourceTypeSerializer)
def
get_type( self, instance: authentik.sources.oauth.models.OAuthSource) -> SourceTypeSerializer:
58 @extend_schema_field(SourceTypeSerializer) 59 def get_type(self, instance: OAuthSource) -> SourceTypeSerializer: 60 """Get source's type configuration""" 61 return SourceTypeSerializer(instance.source_type).data
Get source's type configuration
def
validate(self, attrs: dict) -> dict:
71 def validate(self, attrs: dict) -> dict: 72 session = get_http_session() 73 provider_type_name = attrs.get( 74 "provider_type", 75 self.instance.provider_type if self.instance else None, 76 ) 77 source_type = registry.find_type(provider_type_name) 78 79 well_known = ( 80 self._get_value(attrs, "oidc_well_known_url") or source_type.oidc_well_known_url 81 ) 82 inferred_oidc_jwks_url = None 83 enabled = attrs.get("enabled", self.instance.enabled if self.instance else True) 84 85 if enabled and well_known and well_known != "": 86 try: 87 well_known_config = session.get(well_known) 88 well_known_config.raise_for_status() 89 except RequestException as exc: 90 text = exc.response.text if exc.response is not None else str(exc) 91 raise ValidationError({"oidc_well_known_url": text}) from None 92 config = well_known_config.json() 93 if "issuer" not in config: 94 raise ValidationError({"oidc_well_known_url": "Invalid well-known configuration"}) 95 field_map = { 96 # authentik field to oidc field 97 "authorization_url": "authorization_endpoint", 98 "access_token_url": "token_endpoint", 99 "profile_url": "userinfo_endpoint", 100 } 101 for ak_key, oidc_key in field_map.items(): 102 # Don't overwrite user-set values 103 if ak_key in attrs and attrs[ak_key]: 104 continue 105 attrs[ak_key] = config.get(oidc_key, "") 106 # code_challenge_methods_supported is a list per RFC 8414, not a 107 # single method. Pick one (prefer S256, the RFC-recommended method) 108 # rather than letting the list round-trip into the pkce TextField 109 # and later str() into the authorize URL as "['plain', 'S256']". 110 if not attrs.get("pkce"): 111 supported_methods = config.get("code_challenge_methods_supported") or [] 112 attrs["pkce"] = PKCEMethod.NONE 113 if isinstance(supported_methods, list): 114 if PKCEMethod.S256 in supported_methods: 115 attrs["pkce"] = PKCEMethod.S256 116 elif PKCEMethod.PLAIN in supported_methods: 117 attrs["pkce"] = PKCEMethod.PLAIN 118 inferred_oidc_jwks_url = config.get("jwks_uri", "") 119 120 # Prefer user-entered URL to inferred URL to default URL 121 jwks_url = attrs.get("oidc_jwks_url") or inferred_oidc_jwks_url or source_type.oidc_jwks_url 122 if enabled and jwks_url and jwks_url != "": 123 attrs["oidc_jwks_url"] = jwks_url 124 try: 125 jwks_config = session.get(jwks_url) 126 jwks_config.raise_for_status() 127 except RequestException as exc: 128 text = exc.response.text if exc.response is not None else str(exc) 129 raise ValidationError({"oidc_jwks_url": text}) from None 130 config = jwks_config.json() 131 attrs["oidc_jwks"] = config 132 133 for url in [ 134 "authorization_url", 135 "access_token_url", 136 "profile_url", 137 ]: 138 if getattr(source_type, url, None) is None: 139 if url not in attrs: 140 raise ValidationError( 141 f"{url} is required for provider {source_type.verbose_name}" 142 ) 143 consumer_secret = self._get_value(attrs, "consumer_secret") 144 pkce = self._get_value(attrs, "pkce", PKCEMethod.NONE) 145 if source_type.requires_client_secret and not consumer_secret: 146 raise ValidationError({"consumer_secret": "Consumer secret is required."}) 147 if not consumer_secret and pkce == PKCEMethod.NONE: 148 raise ValidationError({"pkce": "PKCE is required when no consumer secret is used."}) 149 return attrs
Inherited Members
class
OAuthSourceSerializer.Meta:
151 class Meta: 152 model = OAuthSource 153 fields = SourceSerializer.Meta.fields + [ 154 "group_matching_mode", 155 "provider_type", 156 "request_token_url", 157 "authorization_url", 158 "access_token_url", 159 "profile_url", 160 "pkce", 161 "consumer_key", 162 "consumer_secret", 163 "callback_url", 164 "additional_scopes", 165 "type", 166 "oidc_well_known_url", 167 "oidc_jwks_url", 168 "oidc_jwks", 169 "authorization_code_auth_method", 170 ] 171 extra_kwargs = { 172 "consumer_secret": {"write_only": True, "allow_blank": True, "required": False}, 173 "request_token_url": {"allow_blank": True}, 174 "authorization_url": {"allow_blank": True}, 175 "access_token_url": {"allow_blank": True}, 176 "profile_url": {"allow_blank": True}, 177 }
model =
<class 'authentik.sources.oauth.models.OAuthSource'>
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', 'provider_type', 'request_token_url', 'authorization_url', 'access_token_url', 'profile_url', 'pkce', 'consumer_key', 'consumer_secret', 'callback_url', 'additional_scopes', 'type', 'oidc_well_known_url', 'oidc_jwks_url', 'oidc_jwks', 'authorization_code_auth_method']
class
OAuthSourceFilter(django_filters.filterset.FilterSet):
180class OAuthSourceFilter(FilterSet): 181 """OAuth Source filter set""" 182 183 has_jwks = BooleanFilter(label="Only return sources with JWKS data", method="filter_has_jwks") 184 185 def filter_has_jwks(self, queryset, name, value): # pragma: no cover 186 """Only return sources with JWKS data""" 187 return queryset.exclude(oidc_jwks__iexact="{}") 188 189 class Meta: 190 model = OAuthSource 191 fields = [ 192 "pbm_uuid", 193 "name", 194 "slug", 195 "enabled", 196 "authentication_flow", 197 "enrollment_flow", 198 "policy_engine_mode", 199 "user_matching_mode", 200 "group_matching_mode", 201 "provider_type", 202 "request_token_url", 203 "authorization_url", 204 "access_token_url", 205 "profile_url", 206 "consumer_key", 207 "additional_scopes", 208 ]
OAuth Source filter set
def
filter_has_jwks(self, queryset, name, value):
185 def filter_has_jwks(self, queryset, name, value): # pragma: no cover 186 """Only return sources with JWKS data""" 187 return queryset.exclude(oidc_jwks__iexact="{}")
Only return sources with JWKS data
base_filters =
OrderedDict({'pbm_uuid': <django_filters.filters.UUIDFilter object>, 'name': <django_filters.filters.CharFilter object>, 'slug': <django_filters.filters.CharFilter object>, 'enabled': <django_filters.filters.BooleanFilter object>, 'authentication_flow': <django_filters.filters.ModelChoiceFilter object>, 'enrollment_flow': <django_filters.filters.ModelChoiceFilter object>, 'policy_engine_mode': <django_filters.filters.ChoiceFilter object>, 'user_matching_mode': <django_filters.filters.ChoiceFilter object>, 'group_matching_mode': <django_filters.filters.ChoiceFilter object>, 'provider_type': <django_filters.filters.CharFilter object>, 'request_token_url': <django_filters.filters.CharFilter object>, 'authorization_url': <django_filters.filters.CharFilter object>, 'access_token_url': <django_filters.filters.CharFilter object>, 'profile_url': <django_filters.filters.CharFilter object>, 'consumer_key': <django_filters.filters.CharFilter object>, 'additional_scopes': <django_filters.filters.CharFilter object>, 'has_jwks': <django_filters.filters.BooleanFilter object>})
class
OAuthSourceFilter.Meta:
189 class Meta: 190 model = OAuthSource 191 fields = [ 192 "pbm_uuid", 193 "name", 194 "slug", 195 "enabled", 196 "authentication_flow", 197 "enrollment_flow", 198 "policy_engine_mode", 199 "user_matching_mode", 200 "group_matching_mode", 201 "provider_type", 202 "request_token_url", 203 "authorization_url", 204 "access_token_url", 205 "profile_url", 206 "consumer_key", 207 "additional_scopes", 208 ]
model =
<class 'authentik.sources.oauth.models.OAuthSource'>
class
OAuthSourceViewSet(authentik.core.api.used_by.UsedByMixin, rest_framework.viewsets.ModelViewSet):
211class OAuthSourceViewSet(UsedByMixin, ModelViewSet): 212 """Source Viewset""" 213 214 queryset = OAuthSource.objects.all() 215 serializer_class = OAuthSourceSerializer 216 lookup_field = "slug" 217 filterset_class = OAuthSourceFilter 218 search_fields = ["name", "slug"] 219 ordering = ["name"] 220 221 @extend_schema( 222 responses={200: SourceTypeSerializer(many=True)}, 223 parameters=[ 224 OpenApiParameter( 225 name="name", 226 location=OpenApiParameter.QUERY, 227 type=OpenApiTypes.STR, 228 ) 229 ], 230 ) 231 @action(detail=False, pagination_class=None, filter_backends=[]) 232 def source_types(self, request: Request) -> Response: 233 """Get all creatable source types. If ?name is set, only returns the type for <name>. 234 If <name> isn't found, returns the default type.""" 235 data = [] 236 if "name" in request.query_params: 237 source_type = registry.find_type(request.query_params.get("name")) 238 if source_type.__class__ != SourceType: 239 data.append(SourceTypeSerializer(source_type).data) 240 else: 241 for source_type in registry.get(): 242 data.append(SourceTypeSerializer(source_type).data) 243 return Response(data)
Source Viewset
serializer_class =
<class 'OAuthSourceSerializer'>
filterset_class =
<class 'OAuthSourceFilter'>
@extend_schema(responses={200: SourceTypeSerializer(many=True)}, parameters=[OpenApiParameter(name='name', location=OpenApiParameter.QUERY, type=OpenApiTypes.STR)])
@action(detail=False, pagination_class=None, filter_backends=[])
def
source_types( self, request: rest_framework.request.Request) -> rest_framework.response.Response:
221 @extend_schema( 222 responses={200: SourceTypeSerializer(many=True)}, 223 parameters=[ 224 OpenApiParameter( 225 name="name", 226 location=OpenApiParameter.QUERY, 227 type=OpenApiTypes.STR, 228 ) 229 ], 230 ) 231 @action(detail=False, pagination_class=None, filter_backends=[]) 232 def source_types(self, request: Request) -> Response: 233 """Get all creatable source types. If ?name is set, only returns the type for <name>. 234 If <name> isn't found, returns the default type.""" 235 data = [] 236 if "name" in request.query_params: 237 source_type = registry.find_type(request.query_params.get("name")) 238 if source_type.__class__ != SourceType: 239 data.append(SourceTypeSerializer(source_type).data) 240 else: 241 for source_type in registry.get(): 242 data.append(SourceTypeSerializer(source_type).data) 243 return Response(data)
Get all creatable source types. If ?name is set, only returns the type for