From b694816e7bc61ce8fa55ae6f5a90bcdbc70a3fa4 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 6 Dec 2021 09:43:29 +0100 Subject: [PATCH] sources/*: Allow creation of source connections via API closes #1888 Signed-off-by: Jens Langhammer --- .../sources/oauth/api/source_connection.py | 25 +++------ .../sources/plex/api/source_connection.py | 17 +++--- schema.yml | 56 +++++++++++++++++++ 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/authentik/sources/oauth/api/source_connection.py b/authentik/sources/oauth/api/source_connection.py index 61275db35..9ef4c11ec 100644 --- a/authentik/sources/oauth/api/source_connection.py +++ b/authentik/sources/oauth/api/source_connection.py @@ -1,8 +1,7 @@ """OAuth Source Serializer""" from django_filters.rest_framework import DjangoFilterBackend -from rest_framework import mixins from rest_framework.filters import OrderingFilter, SearchFilter -from rest_framework.viewsets import GenericViewSet +from rest_framework.viewsets import ModelViewSet from authentik.api.authorization import OwnerFilter, OwnerPermissions from authentik.core.api.sources import SourceSerializer @@ -15,25 +14,14 @@ class UserOAuthSourceConnectionSerializer(SourceSerializer): class Meta: model = UserOAuthSourceConnection - fields = [ - "pk", - "user", - "source", - "identifier", - ] + fields = ["pk", "user", "source", "identifier", "access_token"] extra_kwargs = { "user": {"read_only": True}, + "access_token": {"write_only": True}, } -class UserOAuthSourceConnectionViewSet( - mixins.RetrieveModelMixin, - mixins.UpdateModelMixin, - mixins.DestroyModelMixin, - UsedByMixin, - mixins.ListModelMixin, - GenericViewSet, -): +class UserOAuthSourceConnectionViewSet(UsedByMixin, ModelViewSet): """Source Viewset""" queryset = UserOAuthSourceConnection.objects.all() @@ -42,3 +30,8 @@ class UserOAuthSourceConnectionViewSet( permission_classes = [OwnerPermissions] filter_backends = [OwnerFilter, DjangoFilterBackend, OrderingFilter, SearchFilter] ordering = ["source__slug"] + + def perform_create(self, serializer: UserOAuthSourceConnectionSerializer): + if not self.request.user.is_superuser: + return serializer.save() + return serializer.save(user=self.request.user) diff --git a/authentik/sources/plex/api/source_connection.py b/authentik/sources/plex/api/source_connection.py index de158cbc8..936afb7f7 100644 --- a/authentik/sources/plex/api/source_connection.py +++ b/authentik/sources/plex/api/source_connection.py @@ -1,8 +1,7 @@ """Plex Source connection Serializer""" from django_filters.rest_framework import DjangoFilterBackend -from rest_framework import mixins from rest_framework.filters import OrderingFilter, SearchFilter -from rest_framework.viewsets import GenericViewSet +from rest_framework.viewsets import ModelViewSet from authentik.api.authorization import OwnerFilter, OwnerPermissions from authentik.core.api.sources import SourceSerializer @@ -27,14 +26,7 @@ class PlexSourceConnectionSerializer(SourceSerializer): } -class PlexSourceConnectionViewSet( - mixins.RetrieveModelMixin, - mixins.UpdateModelMixin, - mixins.DestroyModelMixin, - UsedByMixin, - mixins.ListModelMixin, - GenericViewSet, -): +class PlexSourceConnectionViewSet(UsedByMixin, ModelViewSet): """Plex Source connection Serializer""" queryset = PlexSourceConnection.objects.all() @@ -43,3 +35,8 @@ class PlexSourceConnectionViewSet( permission_classes = [OwnerPermissions] filter_backends = [OwnerFilter, DjangoFilterBackend, OrderingFilter, SearchFilter] ordering = ["pk"] + + def perform_create(self, serializer: PlexSourceConnectionSerializer): + if not self.request.user.is_superuser: + return serializer.save() + return serializer.save(user=self.request.user) diff --git a/schema.yml b/schema.yml index d241ac93d..b95863b12 100644 --- a/schema.yml +++ b/schema.yml @@ -13570,6 +13570,30 @@ paths: $ref: '#/components/schemas/ValidationError' '403': $ref: '#/components/schemas/GenericError' + post: + operationId: sources_user_connections_oauth_create + description: Source Viewset + tags: + - sources + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/UserOAuthSourceConnectionRequest' + required: true + security: + - authentik: [] + responses: + '201': + content: + application/json: + schema: + $ref: '#/components/schemas/UserOAuthSourceConnection' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' /sources/user_connections/oauth/{id}/: get: operationId: sources_user_connections_oauth_retrieve @@ -13754,6 +13778,30 @@ paths: $ref: '#/components/schemas/ValidationError' '403': $ref: '#/components/schemas/GenericError' + post: + operationId: sources_user_connections_plex_create + description: Plex Source connection Serializer + tags: + - sources + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/PlexSourceConnectionRequest' + required: true + security: + - authentik: [] + responses: + '201': + content: + application/json: + schema: + $ref: '#/components/schemas/PlexSourceConnection' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' /sources/user_connections/plex/{id}/: get: operationId: sources_user_connections_plex_retrieve @@ -28143,6 +28191,10 @@ components: type: string minLength: 1 maxLength: 255 + access_token: + type: string + writeOnly: true + nullable: true PatchedUserRequest: type: object description: User Serializer @@ -30889,6 +30941,10 @@ components: type: string minLength: 1 maxLength: 255 + access_token: + type: string + writeOnly: true + nullable: true required: - identifier - source