Repository: goauthentik/authentik

Issue:
Cannot create SAML provider from metadata
**Describe the bug**

It seems like it's not possible to create a SAML provider using metadata from the provider. Specifically, the issue seems to relate to the "authorization flow" selection - when any flow is chosen, an error like "Object with slug=a9f1f70d-6788-421d-826a-4dbe523e3e4e does not exist." appears below the dropdown. It should be noted that this does NOT occur when creating a regular SAML provider without metadata.

**To Reproduce**
Steps to reproduce the behavior:
1. Open Resources > Providers
2. Click Create > SAML Provider from Metadata
3. Upload a metadata file and give the provider a name
4. Click Create
5. Error appears.

**Expected behavior**

I would expect that the SAML provider is created successfully using the flow I select.

**Screenshots**

![screenshot 1](https://share.unixfy.net/direct/dp92m5yq.png)

**Logs**

This event appears consistently when I click "Create":
```
{"event": "Bad Request: /api/v2beta/providers/saml/import_metadata/", "level": "warning", "logger": "django.request", "timestamp": 1622491653.373808}
{"event": "/api/v2beta/providers/saml/import_metadata/", "host": "1.2.3.4", "level": "info", "logger": "authentik.asgi", "method": "POST", "pid": 17, "request_id": "ee57653dbdcb4000bc37bdd292e35c44", "runtime": 96, "scheme": "https", "size": 0.047, "status": 400, "timestamp": "2021-05-31T20:07:33.374429"}
```

**Version and Deployment (please complete the following information):**
 - authentik version: 2021.5.4
 - Deployment: docker-compose




Discussion:
@BeryJu I am having this exact issue with 2023.8.1

Could you reopen this? The description and all steps are identical to the original.

Resolving patch:
diff --git a/authentik/providers/saml/api/providers.py b/authentik/providers/saml/api/providers.py
--- a/authentik/providers/saml/api/providers.py
+++ b/authentik/providers/saml/api/providers.py
@@ -13,10 +13,9 @@
 from rest_framework.fields import CharField, FileField, SerializerMethodField
 from rest_framework.parsers import MultiPartParser
 from rest_framework.permissions import AllowAny
-from rest_framework.relations import SlugRelatedField
 from rest_framework.request import Request
 from rest_framework.response import Response
-from rest_framework.serializers import ValidationError
+from rest_framework.serializers import PrimaryKeyRelatedField, ValidationError
 from rest_framework.viewsets import ModelViewSet
 from structlog.stdlib import get_logger
 
@@ -168,10 +167,8 @@ class SAMLProviderImportSerializer(PassiveSerializer):
     """Import saml provider from XML Metadata"""
 
     name = CharField(required=True)
-    # Using SlugField because https://github.com/OpenAPITools/openapi-generator/issues/3278
-    authorization_flow = SlugRelatedField(
+    authorization_flow = PrimaryKeyRelatedField(
         queryset=Flow.objects.filter(designation=FlowDesignation.AUTHORIZATION),
-        slug_field="slug",
     )
     file = FileField()
 
diff --git a/authentik/root/settings.py b/authentik/root/settings.py
--- a/authentik/root/settings.py
+++ b/authentik/root/settings.py
@@ -402,7 +402,6 @@
     structlog.stdlib.add_logger_name,
     structlog.processors.TimeStamper(),
     structlog.processors.StackInfoRenderer(),
-    structlog.processors.format_exc_info,
 ]
 
 LOGGING = {
@@ -412,7 +411,7 @@
         "json": {
             "()": structlog.stdlib.ProcessorFormatter,
             "processor": structlog.processors.JSONRenderer(sort_keys=True),
-            "foreign_pre_chain": LOG_PRE_CHAIN,
+            "foreign_pre_chain": LOG_PRE_CHAIN + [structlog.processors.dict_tracebacks],
         },
         "console": {
             "()": structlog.stdlib.ProcessorFormatter,


Test patch:
diff --git a/authentik/providers/saml/tests/test_api.py b/authentik/providers/saml/tests/test_api.py
--- a/authentik/providers/saml/tests/test_api.py
+++ b/authentik/providers/saml/tests/test_api.py
@@ -89,7 +89,7 @@ def test_import_success(self):
                 {
                     "file": metadata,
                     "name": generate_id(),
-                    "authorization_flow": create_test_flow(FlowDesignation.AUTHORIZATION).slug,
+                    "authorization_flow": create_test_flow(FlowDesignation.AUTHORIZATION).pk,
                 },
                 format="multipart",
             )
@@ -106,7 +106,7 @@ def test_import_failed(self):
                 {
                     "file": metadata,
                     "name": generate_id(),
-                    "authorization_flow": create_test_flow().slug,
+                    "authorization_flow": create_test_flow().pk,
                 },
                 format="multipart",
             )