sources/*: simplify source api

This commit is contained in:
Jens Langhammer 2021-02-08 10:15:59 +01:00
parent cd5631ec76
commit 3ced67b151
12 changed files with 85 additions and 57 deletions

View File

@ -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",
]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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("/")}/`;

View File

@ -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<Policy> {
return DefaultClient.fetch<Policy>(["policies", "all", pk]);

View File

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

View File

@ -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<Source> {
return DefaultClient.fetch<Source>(["sources", "all", slug]);
@ -19,4 +21,8 @@ export class Source {
static list(filter?: QueryArguments): Promise<PBResponse<Source>> {
return DefaultClient.fetch<PBResponse<Source>>(["sources", "all"], filter);
}
static adminUrl(rest: string): string {
return `/administration/sources/${rest}`;
}
}