sources/*: Allow creation of source connections via API

closes #1888

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-12-06 09:43:29 +01:00
parent e046000f36
commit b694816e7b
3 changed files with 72 additions and 26 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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