From 3ced67b151008a5cfa4cb61025405d2120812414 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 8 Feb 2021 10:15:59 +0100 Subject: [PATCH] sources/*: simplify source api --- authentik/admin/forms/source.py | 19 ------------------ authentik/core/api/sources.py | 25 +++++++++++++----------- authentik/sources/ldap/api.py | 6 +++--- authentik/sources/oauth/api.py | 8 +++----- authentik/sources/oauth/forms.py | 8 ++++++-- authentik/sources/saml/api.py | 8 +++----- authentik/sources/saml/forms.py | 8 ++++++-- swagger.yaml | 33 ++++++++++++++++++++++++++++---- web/src/api/Client.ts | 7 +++++++ web/src/api/Policies.ts | 6 ++++-- web/src/api/Providers.ts | 4 ++-- web/src/api/Sources.ts | 10 ++++++++-- 12 files changed, 85 insertions(+), 57 deletions(-) delete mode 100644 authentik/admin/forms/source.py diff --git a/authentik/admin/forms/source.py b/authentik/admin/forms/source.py deleted file mode 100644 index 0a9b50065..000000000 --- a/authentik/admin/forms/source.py +++ /dev/null @@ -1,19 +0,0 @@ -"""authentik core source form fields""" - -SOURCE_FORM_FIELDS = [ - "name", - "slug", - "enabled", - "authentication_flow", - "enrollment_flow", -] -SOURCE_SERIALIZER_FIELDS = [ - "pk", - "name", - "slug", - "enabled", - "authentication_flow", - "enrollment_flow", - "verbose_name", - "verbose_name_plural", -] diff --git a/authentik/core/api/sources.py b/authentik/core/api/sources.py index 8e41a7816..3ab5f10a9 100644 --- a/authentik/core/api/sources.py +++ b/authentik/core/api/sources.py @@ -2,7 +2,6 @@ from rest_framework.serializers import ModelSerializer, SerializerMethodField from rest_framework.viewsets import ReadOnlyModelViewSet -from authentik.admin.forms.source import SOURCE_SERIALIZER_FIELDS from authentik.core.api.utils import MetaNameSerializer from authentik.core.models import Source @@ -10,22 +9,26 @@ from authentik.core.models import Source class SourceSerializer(ModelSerializer, MetaNameSerializer): """Source Serializer""" - __type__ = SerializerMethodField(method_name="get_type") + object_type = SerializerMethodField() - def get_type(self, obj): + def get_object_type(self, obj): """Get object type so that we know which API Endpoint to use to get the full object""" - return obj._meta.object_name.lower().replace("source", "") - - def to_representation(self, instance: Source): - # pyright: reportGeneralTypeIssues=false - if instance.__class__ == Source: - return super().to_representation(instance) - return instance.serializer(instance=instance).data + return obj._meta.object_name.lower().replace("provider", "") class Meta: model = Source - fields = SOURCE_SERIALIZER_FIELDS + ["__type__"] + fields = SOURCE_SERIALIZER_FIELDS = [ + "pk", + "name", + "slug", + "enabled", + "authentication_flow", + "enrollment_flow", + "object_type", + "verbose_name", + "verbose_name_plural", + ] class SourceViewSet(ReadOnlyModelViewSet): diff --git a/authentik/sources/ldap/api.py b/authentik/sources/ldap/api.py index bd95686e3..c22de70e2 100644 --- a/authentik/sources/ldap/api.py +++ b/authentik/sources/ldap/api.py @@ -2,17 +2,17 @@ from rest_framework.serializers import ModelSerializer from rest_framework.viewsets import ModelViewSet -from authentik.admin.forms.source import SOURCE_SERIALIZER_FIELDS +from authentik.core.api.sources import SourceSerializer from authentik.core.api.utils import MetaNameSerializer from authentik.sources.ldap.models import LDAPPropertyMapping, LDAPSource -class LDAPSourceSerializer(ModelSerializer, MetaNameSerializer): +class LDAPSourceSerializer(SourceSerializer): """LDAP Source Serializer""" class Meta: model = LDAPSource - fields = SOURCE_SERIALIZER_FIELDS + [ + fields = SourceSerializer.Meta.fields + [ "server_uri", "bind_cn", "bind_password", diff --git a/authentik/sources/oauth/api.py b/authentik/sources/oauth/api.py index e9718480a..c58149005 100644 --- a/authentik/sources/oauth/api.py +++ b/authentik/sources/oauth/api.py @@ -1,18 +1,16 @@ """OAuth Source Serializer""" -from rest_framework.serializers import ModelSerializer from rest_framework.viewsets import ModelViewSet -from authentik.admin.forms.source import SOURCE_SERIALIZER_FIELDS -from authentik.core.api.utils import MetaNameSerializer +from authentik.core.api.sources import SourceSerializer from authentik.sources.oauth.models import OAuthSource -class OAuthSourceSerializer(ModelSerializer, MetaNameSerializer): +class OAuthSourceSerializer(SourceSerializer): """OAuth Source Serializer""" class Meta: model = OAuthSource - fields = SOURCE_SERIALIZER_FIELDS + [ + fields = SourceSerializer.Meta.fields + [ "provider_type", "request_token_url", "authorization_url", diff --git a/authentik/sources/oauth/forms.py b/authentik/sources/oauth/forms.py index 7018c096d..69ee0e41d 100644 --- a/authentik/sources/oauth/forms.py +++ b/authentik/sources/oauth/forms.py @@ -2,7 +2,6 @@ from django import forms -from authentik.admin.forms.source import SOURCE_FORM_FIELDS from authentik.flows.models import Flow, FlowDesignation from authentik.sources.oauth.models import OAuthSource from authentik.sources.oauth.types.manager import MANAGER @@ -27,7 +26,12 @@ class OAuthSourceForm(forms.ModelForm): class Meta: model = OAuthSource - fields = SOURCE_FORM_FIELDS + [ + fields = [ + "name", + "slug", + "enabled", + "authentication_flow", + "enrollment_flow", "provider_type", "request_token_url", "authorization_url", diff --git a/authentik/sources/saml/api.py b/authentik/sources/saml/api.py index 408ad3b1d..37da99206 100644 --- a/authentik/sources/saml/api.py +++ b/authentik/sources/saml/api.py @@ -1,19 +1,17 @@ """SAMLSource API Views""" -from rest_framework.serializers import ModelSerializer from rest_framework.viewsets import ModelViewSet -from authentik.admin.forms.source import SOURCE_FORM_FIELDS -from authentik.core.api.utils import MetaNameSerializer +from authentik.core.api.sources import SourceSerializer from authentik.sources.saml.models import SAMLSource -class SAMLSourceSerializer(ModelSerializer, MetaNameSerializer): +class SAMLSourceSerializer(SourceSerializer): """SAMLSource Serializer""" class Meta: model = SAMLSource - fields = SOURCE_FORM_FIELDS + [ + fields = SourceSerializer.Meta.fields + [ "issuer", "sso_url", "slo_url", diff --git a/authentik/sources/saml/forms.py b/authentik/sources/saml/forms.py index bd2fdcf9f..b33ae455c 100644 --- a/authentik/sources/saml/forms.py +++ b/authentik/sources/saml/forms.py @@ -2,7 +2,6 @@ from django import forms -from authentik.admin.forms.source import SOURCE_FORM_FIELDS from authentik.crypto.models import CertificateKeyPair from authentik.flows.models import Flow, FlowDesignation from authentik.sources.saml.models import SAMLSource @@ -28,7 +27,12 @@ class SAMLSourceForm(forms.ModelForm): class Meta: model = SAMLSource - fields = SOURCE_FORM_FIELDS + [ + fields = [ + "name", + "slug", + "enabled", + "authentication_flow", + "enrollment_flow", "issuer", "sso_url", "slo_url", diff --git a/swagger.yaml b/swagger.yaml index ac497df95..799164642 100755 --- a/swagger.yaml +++ b/swagger.yaml @@ -9157,6 +9157,10 @@ definitions: type: string format: uuid x-nullable: true + object_type: + title: Object type + type: string + readOnly: true verbose_name: title: Verbose name type: string @@ -9165,10 +9169,6 @@ definitions: title: Verbose name plural type: string readOnly: true - __type__: - title: 'type ' - type: string - readOnly: true LDAPSource: description: LDAP Source Serializer required: @@ -9213,6 +9213,10 @@ definitions: type: string format: uuid x-nullable: true + object_type: + title: Object type + type: string + readOnly: true verbose_name: title: Verbose name type: string @@ -9344,6 +9348,10 @@ definitions: type: string format: uuid x-nullable: true + object_type: + title: Object type + type: string + readOnly: true verbose_name: title: Verbose name type: string @@ -9397,6 +9405,11 @@ definitions: - sso_url type: object properties: + pk: + title: Pbm uuid + type: string + format: uuid + readOnly: true name: title: Name description: Source's display Name. @@ -9425,6 +9438,18 @@ definitions: type: string format: uuid x-nullable: true + object_type: + title: Object type + type: string + readOnly: true + verbose_name: + title: Verbose name + type: string + readOnly: true + verbose_name_plural: + title: Verbose name plural + type: string + readOnly: true issuer: title: Issuer description: Also known as Entity ID. Defaults the Metadata URL. diff --git a/web/src/api/Client.ts b/web/src/api/Client.ts index 3556b0c88..4632fa17d 100644 --- a/web/src/api/Client.ts +++ b/web/src/api/Client.ts @@ -7,6 +7,13 @@ export interface QueryArguments { [key: string]: number | string | boolean | null; } +export interface BaseInheritanceModel { + + verbose_name: string; + verbose_name_plural: string; + +} + export class Client { makeUrl(url: string[], query?: QueryArguments): string { let builtUrl = `/api/${VERSION}/${url.join("/")}/`; diff --git a/web/src/api/Policies.ts b/web/src/api/Policies.ts index 1c3a26738..3273ddb77 100644 --- a/web/src/api/Policies.ts +++ b/web/src/api/Policies.ts @@ -1,12 +1,14 @@ -import { DefaultClient, PBResponse, QueryArguments } from "./Client"; +import { DefaultClient, BaseInheritanceModel, PBResponse, QueryArguments } from "./Client"; -export class Policy { +export class Policy implements BaseInheritanceModel { pk: string; name: string; constructor() { throw Error(); } + verbose_name: string; + verbose_name_plural: string; static get(pk: string): Promise { return DefaultClient.fetch(["policies", "all", pk]); diff --git a/web/src/api/Providers.ts b/web/src/api/Providers.ts index 3811db82d..7444aeb0c 100644 --- a/web/src/api/Providers.ts +++ b/web/src/api/Providers.ts @@ -1,6 +1,6 @@ -import { DefaultClient, PBResponse, QueryArguments } from "./Client"; +import { BaseInheritanceModel, DefaultClient, PBResponse, QueryArguments } from "./Client"; -export class Provider { +export class Provider implements BaseInheritanceModel { pk: number; name: string; authorization_flow: string; diff --git a/web/src/api/Sources.ts b/web/src/api/Sources.ts index 346ecf3c6..c2d184dcc 100644 --- a/web/src/api/Sources.ts +++ b/web/src/api/Sources.ts @@ -1,6 +1,6 @@ -import { DefaultClient, PBResponse, QueryArguments } from "./Client"; +import { BaseInheritanceModel, DefaultClient, PBResponse, QueryArguments } from "./Client"; -export class Source { +export class Source implements BaseInheritanceModel { pk: string; name: string; slug: string; @@ -11,6 +11,8 @@ export class Source { constructor() { throw Error(); } + verbose_name: string; + verbose_name_plural: string; static get(slug: string): Promise { return DefaultClient.fetch(["sources", "all", slug]); @@ -19,4 +21,8 @@ export class Source { static list(filter?: QueryArguments): Promise> { return DefaultClient.fetch>(["sources", "all"], filter); } + + static adminUrl(rest: string): string { + return `/administration/sources/${rest}`; + } }