From 84c45470053dabb0376d2a275b49b99f5b4b0499 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 17 Aug 2021 12:56:38 +0200 Subject: [PATCH] sources/plex: add API for user connections Signed-off-by: Jens Langhammer --- authentik/api/v2/urls.py | 6 +- authentik/sources/plex/api/__init__.py | 0 .../sources/plex/{api.py => api/source.py} | 0 .../sources/plex/api/source_connection.py | 41 ++ authentik/sources/plex/models.py | 2 +- schema.yml | 695 +++++++++++++----- .../settings/SourceSettingsOAuth.ts | 4 +- 7 files changed, 541 insertions(+), 207 deletions(-) create mode 100644 authentik/sources/plex/api/__init__.py rename authentik/sources/plex/{api.py => api/source.py} (100%) create mode 100644 authentik/sources/plex/api/source_connection.py diff --git a/authentik/api/v2/urls.py b/authentik/api/v2/urls.py index 64ef4de66..43c8899a8 100644 --- a/authentik/api/v2/urls.py +++ b/authentik/api/v2/urls.py @@ -58,7 +58,8 @@ from authentik.providers.saml.api import SAMLPropertyMappingViewSet, SAMLProvide from authentik.sources.ldap.api import LDAPPropertyMappingViewSet, LDAPSourceViewSet from authentik.sources.oauth.api.source import OAuthSourceViewSet from authentik.sources.oauth.api.source_connection import UserOAuthSourceConnectionViewSet -from authentik.sources.plex.api import PlexSourceViewSet +from authentik.sources.plex.api.source import PlexSourceViewSet +from authentik.sources.plex.api.source_connection import PlexSourceConnectionViewSet from authentik.sources.saml.api import SAMLSourceViewSet from authentik.stages.authenticator_duo.api import ( AuthenticatorDuoStageViewSet, @@ -127,7 +128,8 @@ router.register("events/transports", NotificationTransportViewSet) router.register("events/rules", NotificationRuleViewSet) router.register("sources/all", SourceViewSet) -router.register("sources/oauth_user_connections", UserOAuthSourceConnectionViewSet) +router.register("sources/user_connections/oauth", UserOAuthSourceConnectionViewSet) +router.register("sources/user_connections/plex", PlexSourceConnectionViewSet) router.register("sources/ldap", LDAPSourceViewSet) router.register("sources/saml", SAMLSourceViewSet) router.register("sources/oauth", OAuthSourceViewSet) diff --git a/authentik/sources/plex/api/__init__.py b/authentik/sources/plex/api/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/authentik/sources/plex/api.py b/authentik/sources/plex/api/source.py similarity index 100% rename from authentik/sources/plex/api.py rename to authentik/sources/plex/api/source.py diff --git a/authentik/sources/plex/api/source_connection.py b/authentik/sources/plex/api/source_connection.py new file mode 100644 index 000000000..0e793c66d --- /dev/null +++ b/authentik/sources/plex/api/source_connection.py @@ -0,0 +1,41 @@ +"""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 authentik.api.authorization import OwnerFilter, OwnerPermissions +from authentik.core.api.sources import SourceSerializer +from authentik.core.api.used_by import UsedByMixin +from authentik.sources.plex.models import PlexSourceConnection + + +class PlexSourceConnectionSerializer(SourceSerializer): + """Plex Source connection Serializer""" + + class Meta: + model = PlexSourceConnection + fields = [ + "pk", + "user", + "source", + "identifier", + "plex_token", + ] + + +class PlexSourceConnectionViewSet( + mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.DestroyModelMixin, + UsedByMixin, + mixins.ListModelMixin, + GenericViewSet, +): + """Plex Source connection Serializer""" + + queryset = PlexSourceConnection.objects.all() + serializer_class = PlexSourceConnectionSerializer + filterset_fields = ["source__slug"] + permission_classes = [OwnerPermissions] + filter_backends = [OwnerFilter, DjangoFilterBackend, OrderingFilter, SearchFilter] diff --git a/authentik/sources/plex/models.py b/authentik/sources/plex/models.py index fe215b467..56592d94e 100644 --- a/authentik/sources/plex/models.py +++ b/authentik/sources/plex/models.py @@ -56,7 +56,7 @@ class PlexSource(Source): @property def serializer(self) -> BaseSerializer: - from authentik.sources.plex.api import PlexSourceSerializer + from authentik.sources.plex.api.source import PlexSourceSerializer return PlexSourceSerializer diff --git a/schema.yml b/schema.yml index e1a57ceae..fbe0b454c 100644 --- a/schema.yml +++ b/schema.yml @@ -13197,208 +13197,6 @@ paths: $ref: '#/components/schemas/ValidationError' '403': $ref: '#/components/schemas/GenericError' - /api/v2beta/sources/oauth_user_connections/: - get: - operationId: sources_oauth_user_connections_list - description: Source Viewset - parameters: - - name: ordering - required: false - in: query - description: Which field to use when ordering the results. - schema: - type: string - - name: page - required: false - in: query - description: A page number within the paginated result set. - schema: - type: integer - - name: page_size - required: false - in: query - description: Number of results to return per page. - schema: - type: integer - - name: search - required: false - in: query - description: A search term. - schema: - type: string - - in: query - name: source__slug - schema: - type: string - tags: - - sources - security: - - authentik: [] - - cookieAuth: [] - responses: - '200': - content: - application/json: - schema: - $ref: '#/components/schemas/PaginatedUserOAuthSourceConnectionList' - description: '' - '400': - $ref: '#/components/schemas/ValidationError' - '403': - $ref: '#/components/schemas/GenericError' - /api/v2beta/sources/oauth_user_connections/{id}/: - get: - operationId: sources_oauth_user_connections_retrieve - description: Source Viewset - parameters: - - in: path - name: id - schema: - type: integer - description: A unique integer value identifying this User OAuth Source Connection. - required: true - tags: - - sources - security: - - authentik: [] - - cookieAuth: [] - responses: - '200': - content: - application/json: - schema: - $ref: '#/components/schemas/UserOAuthSourceConnection' - description: '' - '400': - $ref: '#/components/schemas/ValidationError' - '403': - $ref: '#/components/schemas/GenericError' - put: - operationId: sources_oauth_user_connections_update - description: Source Viewset - parameters: - - in: path - name: id - schema: - type: integer - description: A unique integer value identifying this User OAuth Source Connection. - required: true - tags: - - sources - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/UserOAuthSourceConnectionRequest' - application/x-www-form-urlencoded: - schema: - $ref: '#/components/schemas/UserOAuthSourceConnectionRequest' - multipart/form-data: - schema: - $ref: '#/components/schemas/UserOAuthSourceConnectionRequest' - required: true - security: - - authentik: [] - - cookieAuth: [] - responses: - '200': - content: - application/json: - schema: - $ref: '#/components/schemas/UserOAuthSourceConnection' - description: '' - '400': - $ref: '#/components/schemas/ValidationError' - '403': - $ref: '#/components/schemas/GenericError' - patch: - operationId: sources_oauth_user_connections_partial_update - description: Source Viewset - parameters: - - in: path - name: id - schema: - type: integer - description: A unique integer value identifying this User OAuth Source Connection. - required: true - tags: - - sources - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/PatchedUserOAuthSourceConnectionRequest' - application/x-www-form-urlencoded: - schema: - $ref: '#/components/schemas/PatchedUserOAuthSourceConnectionRequest' - multipart/form-data: - schema: - $ref: '#/components/schemas/PatchedUserOAuthSourceConnectionRequest' - security: - - authentik: [] - - cookieAuth: [] - responses: - '200': - content: - application/json: - schema: - $ref: '#/components/schemas/UserOAuthSourceConnection' - description: '' - '400': - $ref: '#/components/schemas/ValidationError' - '403': - $ref: '#/components/schemas/GenericError' - delete: - operationId: sources_oauth_user_connections_destroy - description: Source Viewset - parameters: - - in: path - name: id - schema: - type: integer - description: A unique integer value identifying this User OAuth Source Connection. - required: true - tags: - - sources - security: - - authentik: [] - - cookieAuth: [] - responses: - '204': - description: No response body - '400': - $ref: '#/components/schemas/ValidationError' - '403': - $ref: '#/components/schemas/GenericError' - /api/v2beta/sources/oauth_user_connections/{id}/used_by/: - get: - operationId: sources_oauth_user_connections_used_by_list - description: Get a list of all objects that use this object - parameters: - - in: path - name: id - schema: - type: integer - description: A unique integer value identifying this User OAuth Source Connection. - required: true - tags: - - sources - security: - - authentik: [] - - cookieAuth: [] - responses: - '200': - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/UsedBy' - description: '' - '400': - $ref: '#/components/schemas/ValidationError' - '403': - $ref: '#/components/schemas/GenericError' /api/v2beta/sources/plex/: get: operationId: sources_plex_list @@ -14110,6 +13908,410 @@ paths: $ref: '#/components/schemas/ValidationError' '403': $ref: '#/components/schemas/GenericError' + /api/v2beta/sources/user_connections/oauth/: + get: + operationId: sources_user_connections_oauth_list + description: Source Viewset + parameters: + - name: ordering + required: false + in: query + description: Which field to use when ordering the results. + schema: + type: string + - name: page + required: false + in: query + description: A page number within the paginated result set. + schema: + type: integer + - name: page_size + required: false + in: query + description: Number of results to return per page. + schema: + type: integer + - name: search + required: false + in: query + description: A search term. + schema: + type: string + - in: query + name: source__slug + schema: + type: string + tags: + - sources + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/PaginatedUserOAuthSourceConnectionList' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + /api/v2beta/sources/user_connections/oauth/{id}/: + get: + operationId: sources_user_connections_oauth_retrieve + description: Source Viewset + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User OAuth Source Connection. + required: true + tags: + - sources + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/UserOAuthSourceConnection' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + put: + operationId: sources_user_connections_oauth_update + description: Source Viewset + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User OAuth Source Connection. + required: true + tags: + - sources + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/UserOAuthSourceConnectionRequest' + application/x-www-form-urlencoded: + schema: + $ref: '#/components/schemas/UserOAuthSourceConnectionRequest' + multipart/form-data: + schema: + $ref: '#/components/schemas/UserOAuthSourceConnectionRequest' + required: true + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/UserOAuthSourceConnection' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + patch: + operationId: sources_user_connections_oauth_partial_update + description: Source Viewset + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User OAuth Source Connection. + required: true + tags: + - sources + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/PatchedUserOAuthSourceConnectionRequest' + application/x-www-form-urlencoded: + schema: + $ref: '#/components/schemas/PatchedUserOAuthSourceConnectionRequest' + multipart/form-data: + schema: + $ref: '#/components/schemas/PatchedUserOAuthSourceConnectionRequest' + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/UserOAuthSourceConnection' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + delete: + operationId: sources_user_connections_oauth_destroy + description: Source Viewset + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User OAuth Source Connection. + required: true + tags: + - sources + security: + - authentik: [] + - cookieAuth: [] + responses: + '204': + description: No response body + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + /api/v2beta/sources/user_connections/oauth/{id}/used_by/: + get: + operationId: sources_user_connections_oauth_used_by_list + description: Get a list of all objects that use this object + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User OAuth Source Connection. + required: true + tags: + - sources + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/UsedBy' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + /api/v2beta/sources/user_connections/plex/: + get: + operationId: sources_user_connections_plex_list + description: Plex Source connection Serializer + parameters: + - name: ordering + required: false + in: query + description: Which field to use when ordering the results. + schema: + type: string + - name: page + required: false + in: query + description: A page number within the paginated result set. + schema: + type: integer + - name: page_size + required: false + in: query + description: Number of results to return per page. + schema: + type: integer + - name: search + required: false + in: query + description: A search term. + schema: + type: string + - in: query + name: source__slug + schema: + type: string + tags: + - sources + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/PaginatedPlexSourceConnectionList' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + /api/v2beta/sources/user_connections/plex/{id}/: + get: + operationId: sources_user_connections_plex_retrieve + description: Plex Source connection Serializer + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User Plex Source Connection. + required: true + tags: + - sources + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/PlexSourceConnection' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + put: + operationId: sources_user_connections_plex_update + description: Plex Source connection Serializer + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User Plex Source Connection. + required: true + tags: + - sources + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/PlexSourceConnectionRequest' + application/x-www-form-urlencoded: + schema: + $ref: '#/components/schemas/PlexSourceConnectionRequest' + multipart/form-data: + schema: + $ref: '#/components/schemas/PlexSourceConnectionRequest' + required: true + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/PlexSourceConnection' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + patch: + operationId: sources_user_connections_plex_partial_update + description: Plex Source connection Serializer + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User Plex Source Connection. + required: true + tags: + - sources + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/PatchedPlexSourceConnectionRequest' + application/x-www-form-urlencoded: + schema: + $ref: '#/components/schemas/PatchedPlexSourceConnectionRequest' + multipart/form-data: + schema: + $ref: '#/components/schemas/PatchedPlexSourceConnectionRequest' + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/PlexSourceConnection' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + delete: + operationId: sources_user_connections_plex_destroy + description: Plex Source connection Serializer + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User Plex Source Connection. + required: true + tags: + - sources + security: + - authentik: [] + - cookieAuth: [] + responses: + '204': + description: No response body + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' + /api/v2beta/sources/user_connections/plex/{id}/used_by/: + get: + operationId: sources_user_connections_plex_used_by_list + description: Get a list of all objects that use this object + parameters: + - in: path + name: id + schema: + type: integer + description: A unique integer value identifying this User Plex Source Connection. + required: true + tags: + - sources + security: + - authentik: [] + - cookieAuth: [] + responses: + '200': + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/UsedBy' + description: '' + '400': + $ref: '#/components/schemas/ValidationError' + '403': + $ref: '#/components/schemas/GenericError' /api/v2beta/stages/all/: get: operationId: stages_all_list @@ -24767,6 +24969,41 @@ components: required: - pagination - results + PaginatedPlexSourceConnectionList: + type: object + properties: + pagination: + type: object + properties: + next: + type: number + previous: + type: number + count: + type: number + current: + type: number + total_pages: + type: number + start_index: + type: number + end_index: + type: number + required: + - next + - previous + - count + - current + - total_pages + - start_index + - end_index + results: + type: array + items: + $ref: '#/components/schemas/PlexSourceConnection' + required: + - pagination + - results PaginatedPlexSourceList: type: object properties: @@ -27099,6 +27336,19 @@ components: minimum: -2147483648 description: How many attempts a user has before the flow is canceled. To lock the user out, use a reputation policy and a user_write stage. + PatchedPlexSourceConnectionRequest: + type: object + description: Plex Source connection Serializer + properties: + user: + type: integer + source: + type: string + format: uuid + identifier: + type: string + plex_token: + type: string PatchedPlexSourceRequest: type: object description: Plex Source Serializer @@ -27775,6 +28025,47 @@ components: - slug - verbose_name - verbose_name_plural + PlexSourceConnection: + type: object + description: Plex Source connection Serializer + properties: + pk: + type: integer + readOnly: true + title: ID + user: + type: integer + source: + type: string + format: uuid + identifier: + type: string + plex_token: + type: string + required: + - identifier + - pk + - plex_token + - source + - user + PlexSourceConnectionRequest: + type: object + description: Plex Source connection Serializer + properties: + user: + type: integer + source: + type: string + format: uuid + identifier: + type: string + plex_token: + type: string + required: + - identifier + - plex_token + - source + - user PlexSourceRequest: type: object description: Plex Source Serializer diff --git a/web/src/pages/user-settings/settings/SourceSettingsOAuth.ts b/web/src/pages/user-settings/settings/SourceSettingsOAuth.ts index b64b36922..83a86a6a6 100644 --- a/web/src/pages/user-settings/settings/SourceSettingsOAuth.ts +++ b/web/src/pages/user-settings/settings/SourceSettingsOAuth.ts @@ -21,7 +21,7 @@ export class SourceSettingsOAuth extends BaseUserSettings { renderInner(): TemplateResult { return html`${until( new SourcesApi(DEFAULT_CONFIG) - .sourcesOauthUserConnectionsList({ + .sourcesUserConnectionsOauthList({ sourceSlug: this.objectId, }) .then((connection) => { @@ -32,7 +32,7 @@ export class SourceSettingsOAuth extends BaseUserSettings { @click=${() => { return new SourcesApi( DEFAULT_CONFIG, - ).sourcesOauthUserConnectionsDestroy({ + ).sourcesUserConnectionsOauthDestroy({ id: connection.results[0].pk || 0, }); }}