From 45f1d95bf99110cd1dfc68f58215ecbad9309b89 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 9 Feb 2021 16:47:49 +0100 Subject: [PATCH] sources/oauth: add callback URL to api --- authentik/sources/oauth/api.py | 16 +++++ authentik/sources/oauth/models.py | 8 --- swagger.yaml | 6 +- web/src/api/sources/OAuth.ts | 22 +++++++ web/src/pages/sources/OAuthSourceViewPage.ts | 69 ++++++++++---------- web/src/pages/sources/SourceViewPage.ts | 2 +- 6 files changed, 79 insertions(+), 44 deletions(-) create mode 100644 web/src/api/sources/OAuth.ts diff --git a/authentik/sources/oauth/api.py b/authentik/sources/oauth/api.py index 161e63905..0534088eb 100644 --- a/authentik/sources/oauth/api.py +++ b/authentik/sources/oauth/api.py @@ -1,4 +1,6 @@ """OAuth Source Serializer""" +from django.urls.base import reverse_lazy +from rest_framework.fields import SerializerMethodField from rest_framework.viewsets import ModelViewSet from authentik.core.api.sources import SourceSerializer @@ -8,6 +10,18 @@ from authentik.sources.oauth.models import OAuthSource class OAuthSourceSerializer(SourceSerializer): """OAuth Source Serializer""" + callback_url = SerializerMethodField() + + def get_callback_url(self, instance: OAuthSource) -> str: + """Get OAuth Callback URL""" + relative_url = reverse_lazy( + "authentik_sources_oauth:oauth-client-callback", + kwargs={"source_slug": instance.slug}, + ) + if "request" not in self.context: + return relative_url + return self.context["request"].build_absolute_uri(relative_url) + class Meta: model = OAuthSource fields = SourceSerializer.Meta.fields + [ @@ -18,7 +32,9 @@ class OAuthSourceSerializer(SourceSerializer): "profile_url", "consumer_key", "consumer_secret", + "callback_url", ] + extra_kwargs = {"consumer_secret": {"write_only": True}} class OAuthSourceViewSet(ModelViewSet): diff --git a/authentik/sources/oauth/models.py b/authentik/sources/oauth/models.py index c944ff03a..f01728889 100644 --- a/authentik/sources/oauth/models.py +++ b/authentik/sources/oauth/models.py @@ -64,14 +64,6 @@ class OAuthSource(Source): name=self.name, ) - @property - def ui_additional_info(self) -> str: - url = reverse_lazy( - "authentik_sources_oauth:oauth-client-callback", - kwargs={"source_slug": self.slug}, - ) - return f"Callback URL:
{url}
" - @property def ui_user_settings(self) -> Optional[str]: view_name = "authentik_sources_oauth:oauth-client-user" diff --git a/swagger.yaml b/swagger.yaml index 362127189..d2438d795 100755 --- a/swagger.yaml +++ b/swagger.yaml @@ -4981,7 +4981,7 @@ paths: /sources/ldap/{slug}/sync_status/: get: operationId: sources_ldap_sync_status - description: LDAP Source Viewset + description: Get source's sync status parameters: [] responses: '200': @@ -9631,6 +9631,10 @@ definitions: title: Consumer secret type: string minLength: 1 + callback_url: + title: Callback url + type: string + readOnly: true SAMLSource: description: SAMLSource Serializer required: diff --git a/web/src/api/sources/OAuth.ts b/web/src/api/sources/OAuth.ts new file mode 100644 index 000000000..216d9c202 --- /dev/null +++ b/web/src/api/sources/OAuth.ts @@ -0,0 +1,22 @@ +import { DefaultClient } from "../Client"; +import { Source } from "../Sources"; + +export class OAuthSource extends Source { + provider_type: string; + request_token_url: string; + authorization_url: string; + access_token_url: string; + profile_url: string; + consumer_key: string; + callback_url: string; + + constructor() { + super(); + throw Error(); + } + + static get(slug: string): Promise { + return DefaultClient.fetch(["sources", "oauth", slug]); + } + +} diff --git a/web/src/pages/sources/OAuthSourceViewPage.ts b/web/src/pages/sources/OAuthSourceViewPage.ts index 61ec59423..b60ef4a89 100644 --- a/web/src/pages/sources/OAuthSourceViewPage.ts +++ b/web/src/pages/sources/OAuthSourceViewPage.ts @@ -7,13 +7,13 @@ import "../../elements/buttons/SpinnerButton"; import "../../elements/CodeMirror"; import "../../elements/Tabs"; import { Page } from "../../elements/Page"; -import { LDAPSource } from "../../api/sources/LDAP"; +import { OAuthSource } from "../../api/sources/OAuth"; import { Source } from "../../api/Sources"; @customElement("ak-source-oauth-view") export class OAuthSourceViewPage extends Page { pageTitle(): string { - return gettext(`LDAP Source ${this.source?.name}`); + return gettext(`OAuth Source ${this.source?.name || ""}`); } pageDescription(): string | undefined { return; @@ -24,16 +24,16 @@ export class OAuthSourceViewPage extends Page { @property() set args(value: { [key: string]: string }) { - this.sourceID = value.id; + this.sourceSlug = value.slug; } @property({ type: String }) - set sourceID(value: string) { - LDAPSource.get(value).then((s) => this.source = s); + set sourceSlug(value: string) { + OAuthSource.get(value).then((s) => this.source = s); } @property({ attribute: false }) - source?: LDAPSource; + source?: OAuthSource; static get styles(): CSSResult[] { return COMMON_STYLES; @@ -43,7 +43,7 @@ export class OAuthSourceViewPage extends Page { super(); this.addEventListener("ak-refresh", () => { if (!this.source?.pk) return; - this.sourceID = this.source?.pk; + this.sourceSlug = this.source?.slug; }); } @@ -68,22 +68,42 @@ export class OAuthSourceViewPage extends Page {
- ${gettext("Server URI")} + ${gettext("Provider Type")}
-
${this.source.server_uri}
+
${this.source.provider_type}
- ${gettext("Base DN")} + ${gettext("Callback URL")}
-
-
    -
  • ${this.source.base_dn}
  • -
-
+ ${this.source.callback_url} +
+
+
+
+ ${gettext("Access Key")} +
+
+
${this.source.consumer_key}
+
+
+
+
+ ${gettext("Authorization URL")} +
+
+
${this.source.authorization_url}
+
+
+
+
+ ${gettext("Token URL")} +
+
+
${this.source.access_token_url}
@@ -97,28 +117,9 @@ export class OAuthSourceViewPage extends Page { -
-
- ${gettext("Sync status")} -
-
- -
-
-
-
-
-
- ${gettext("These policies control which users can authorize using these policies.")} -
-
- - -
-
`; } } diff --git a/web/src/pages/sources/SourceViewPage.ts b/web/src/pages/sources/SourceViewPage.ts index 7439c7bfd..7abbb731d 100644 --- a/web/src/pages/sources/SourceViewPage.ts +++ b/web/src/pages/sources/SourceViewPage.ts @@ -43,7 +43,7 @@ export class SourceViewPage extends LitElement { switch (this.source?.object_type) { case "ldap": return html``; - case "oauth2": + case "oauth": return html``; // case "proxy": // return html``;