From a367d8515fa20e7db1db6a20d56819348531f1c3 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 10 Feb 2021 20:12:02 +0100 Subject: [PATCH] core: add source endpoint --- authentik/core/api/sources.py | 27 +++++++++++++++++++++++++- swagger.yaml | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/authentik/core/api/sources.py b/authentik/core/api/sources.py index 40815228c..e2c927434 100644 --- a/authentik/core/api/sources.py +++ b/authentik/core/api/sources.py @@ -1,8 +1,16 @@ """Source API Views""" +from authentik.lib.templatetags.authentik_utils import verbose_name +from authentik.lib.utils.reflection import all_subclasses +from drf_yasg2.utils import swagger_auto_schema +from rest_framework.decorators import action +from rest_framework.request import Request +from rest_framework.response import Response from rest_framework.serializers import ModelSerializer, SerializerMethodField from rest_framework.viewsets import ReadOnlyModelViewSet +from django.shortcuts import reverse +from django.utils.translation import gettext_lazy as _ -from authentik.core.api.utils import MetaNameSerializer +from authentik.core.api.utils import MetaNameSerializer, TypeCreateSerializer from authentik.core.models import Source @@ -40,3 +48,20 @@ class SourceViewSet(ReadOnlyModelViewSet): def get_queryset(self): return Source.objects.select_subclasses() + + @swagger_auto_schema(responses={200: TypeCreateSerializer(many=True)}) + @action(detail=False) + # pylint: disable=unused-argument + def types(self, request: Request) -> Response: + """Get all creatable source types""" + data = [] + for subclass in all_subclasses(self.queryset.model): + data.append( + { + "name": verbose_name(subclass), + "description": subclass.__doc__, + "link": reverse("authentik_admin:source-create") + + f"?type={subclass.__name__}", + } + ) + return Response(TypeCreateSerializer(data, many=True).data) diff --git a/swagger.yaml b/swagger.yaml index 3f98d340e..9674c2a07 100755 --- a/swagger.yaml +++ b/swagger.yaml @@ -4909,6 +4909,42 @@ paths: tags: - sources parameters: [] + /sources/all/types/: + get: + operationId: sources_all_types + description: Get all creatable source types + parameters: + - name: ordering + in: query + description: Which field to use when ordering the results. + required: false + type: string + - name: search + in: query + description: A search term. + required: false + type: string + - name: page + in: query + description: A page number within the paginated result set. + required: false + type: integer + - name: page_size + in: query + description: Number of results to return per page. + required: false + type: integer + responses: + '200': + description: '' + schema: + description: '' + type: array + items: + $ref: '#/definitions/TypeCreate' + tags: + - sources + parameters: [] /sources/all/{slug}/: get: operationId: sources_all_read