providers/saml: add metadata download link to api

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-06-10 14:02:19 +02:00
parent 4766d6ff3d
commit d878d2140e
3 changed files with 23 additions and 4 deletions

View File

@ -4,11 +4,17 @@ from xml.etree.ElementTree import ParseError # nosec
from defusedxml.ElementTree import fromstring from defusedxml.ElementTree import fromstring
from django.http.response import HttpResponse from django.http.response import HttpResponse
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from drf_spectacular.types import OpenApiTypes from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.fields import CharField, FileField, ReadOnlyField from rest_framework.fields import (
CharField,
FileField,
ReadOnlyField,
SerializerMethodField,
)
from rest_framework.parsers import MultiPartParser from rest_framework.parsers import MultiPartParser
from rest_framework.permissions import AllowAny from rest_framework.permissions import AllowAny
from rest_framework.relations import SlugRelatedField from rest_framework.relations import SlugRelatedField
@ -37,6 +43,15 @@ LOGGER = get_logger()
class SAMLProviderSerializer(ProviderSerializer): class SAMLProviderSerializer(ProviderSerializer):
"""SAMLProvider Serializer""" """SAMLProvider Serializer"""
metadata_download_url = SerializerMethodField()
def get_metadata_download_url(self, instance: SAMLProvider) -> str:
"""Get metadata download URL"""
return (
reverse("authentik_api:samlprovider-metadata", kwargs={"pk": instance.pk})
+ "?download"
)
class Meta: class Meta:
model = SAMLProvider model = SAMLProvider
@ -54,6 +69,7 @@ class SAMLProviderSerializer(ProviderSerializer):
"signing_kp", "signing_kp",
"verification_kp", "verification_kp",
"sp_binding", "sp_binding",
"metadata_download_url",
] ]

View File

@ -26576,12 +26576,16 @@ components:
title: Service Provider Binding title: Service Provider Binding
description: This determines how authentik sends the response back to the description: This determines how authentik sends the response back to the
Service Provider. Service Provider.
metadata_download_url:
type: string
readOnly: true
required: required:
- acs_url - acs_url
- assigned_application_name - assigned_application_name
- assigned_application_slug - assigned_application_slug
- authorization_flow - authorization_flow
- component - component
- metadata_download_url
- name - name
- pk - pk
- verbose_name - verbose_name

View File

@ -157,14 +157,13 @@ export class SAMLProviderViewPage extends LitElement {
</div> </div>
<div class="pf-c-card__footer"> <div class="pf-c-card__footer">
<a class="pf-c-button pf-m-primary" target="_blank" <a class="pf-c-button pf-m-primary" target="_blank"
href="/api/v2beta/providers/saml/${this.provider.pk}/metadata/?download"> href=${this.provider.metadataDownloadUrl}>
${t`Download`} ${t`Download`}
</a> </a>
<ak-action-button <ak-action-button
class="pf-m-secondary" class="pf-m-secondary"
.apiRequest=${() => { .apiRequest=${() => {
const path = `/api/v2beta/providers/saml/${this.provider?.pk}/metadata/?download`; const fullUrl = window.location.origin + this.provider.metadataDownloadUrl;
const fullUrl = window.location.origin + path;
return navigator.clipboard.writeText(fullUrl); return navigator.clipboard.writeText(fullUrl);
}}> }}>
${t`Copy download URL`} ${t`Copy download URL`}