From 699fb83dd48f56c212a72b8950eec33408555c2a Mon Sep 17 00:00:00 2001
From: "github-actions[bot]"
 <41898282+github-actions[bot]@users.noreply.github.com>
Date: Tue, 1 Aug 2023 10:39:46 +1000
Subject: [PATCH] Fix SSO check comparing id against name and extend log output
 (#5340) (#5377)

* add error log on SSO check failure

* sso_check_provider: fix by comparing against id

the name is the pretty printed version which not necessarily is the same
as the provider id it is compared against. This fails e.g. for the
microsoft allauth extension where the id is microsoft, but the name is
"Microsoft Graph".

Closes: #5330
(cherry picked from commit ee5416719f2df683e10a94840840389c088460de)

Co-authored-by: Hendrik v. Raven <hendrik@consetetur.de>
---
 InvenTree/part/templatetags/sso.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/InvenTree/part/templatetags/sso.py b/InvenTree/part/templatetags/sso.py
index 024f16052c..f018719e52 100644
--- a/InvenTree/part/templatetags/sso.py
+++ b/InvenTree/part/templatetags/sso.py
@@ -38,9 +38,13 @@ def sso_check_provider(provider):
     from allauth.socialaccount.models import SocialApp
 
     # First, check that the provider is enabled
-    apps = SocialApp.objects.filter(provider__iexact=provider.name)
+    apps = SocialApp.objects.filter(provider__iexact=provider.id)
 
     if not apps.exists():
+        logging.error(
+            "SSO SocialApp %s does not exist (known providers: %s)",
+            provider.id, [obj.provider for obj in SocialApp.objects.all()]
+        )
         return False
 
     # Next, check that the provider is correctly configured