From e38eb1fd6e738aff76d863b5e23727935a2d2317 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Thu, 18 Jun 2020 18:12:48 +0200
Subject: [PATCH] remove idp.saml from plugin system (#44319)

---
 setup.py                            |  1 -
 src/authentic2/cors.py              |  4 +++-
 src/authentic2/idp/saml/__init__.py | 26 +++-----------------------
 src/authentic2/settings.py          |  2 +-
 src/authentic2/urls.py              | 13 +++++++++++++
 5 files changed, 20 insertions(+), 26 deletions(-)

diff --git a/setup.py b/setup.py
index 6d14d99d..c0662d0c 100755
--- a/setup.py
+++ b/setup.py
@@ -171,7 +171,6 @@ setup(name="authentic2",
           'authentic2.plugin': [
               'authentic2-auth-saml = authentic2_auth_saml:Plugin',
               'authentic2-auth-oidc = authentic2_auth_oidc:Plugin',
-              'authentic2-idp-saml2 = authentic2.idp.saml:Plugin',
               'authentic2-idp-cas = authentic2_idp_cas:Plugin',
               'authentic2-idp-oidc = authentic2_idp_oidc:Plugin',
               'authentic2-provisionning-ldap = authentic2_provisionning_ldap:Plugin',
diff --git a/src/authentic2/cors.py b/src/authentic2/cors.py
index fe38fff0..7a7d630c 100644
--- a/src/authentic2/cors.py
+++ b/src/authentic2/cors.py
@@ -14,8 +14,10 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import itertools
 from .decorators import SessionCache
 
+from django.apps import apps
 from django.conf import settings
 from django.utils.six.moves.urllib import parse as urlparse
 
@@ -56,7 +58,7 @@ def check_origin(request, origin):
     for whitelist_origin in app_settings.A2_CORS_WHITELIST:
         if whitelist_origin == origin:
             return True
-    for plugin in plugins.get_plugins():
+    for plugin in itertools.chain(plugins.get_plugins(), apps.get_app_configs()):
         if hasattr(plugin, 'check_origin'):
             if plugin.check_origin(request, origin):
                 return True
diff --git a/src/authentic2/idp/saml/__init__.py b/src/authentic2/idp/saml/__init__.py
index 65058aa3..cf160e5c 100644
--- a/src/authentic2/idp/saml/__init__.py
+++ b/src/authentic2/idp/saml/__init__.py
@@ -19,25 +19,9 @@ from django.core.checks import register, Warning, Tags
 from django.apps import AppConfig
 
 
-class Plugin(object):
-    def get_before_urls(self):
-        from . import app_settings
-        from django.conf.urls import url, include
-        from authentic2.decorators import (setting_enabled, required,
-                                           lasso_required)
-
-        return required(
-            (
-                setting_enabled('ENABLE', settings=app_settings),
-                lasso_required()
-            ),
-            [url(r'^idp/saml2/', include(__name__ + '.urls'))])
-
-    def get_apps(self):
-        return ['authentic2.idp.saml']
-
-    def get_idp_backends(self):
-        return ['authentic2.idp.saml.backend.SamlBackend']
+class SAML2IdPConfig(AppConfig):
+    name = 'authentic2.idp.saml'
+    label = 'authentic2_idp_saml'
 
     def check_origin(self, request, origin):
         from authentic2.cors import make_origin
@@ -48,10 +32,6 @@ class Plugin(object):
             if origin == provider_origin:
                 return True
 
-
-class SAML2IdPConfig(AppConfig):
-    name = 'authentic2.idp.saml'
-    label = 'authentic2_idp_saml'
 default_app_config = 'authentic2.idp.saml.SAML2IdPConfig'
 
 
diff --git a/src/authentic2/settings.py b/src/authentic2/settings.py
index a5d25380..c1b00de6 100644
--- a/src/authentic2/settings.py
+++ b/src/authentic2/settings.py
@@ -190,7 +190,7 @@ RBAC_ROLE_PARENTING_MODEL = 'a2_rbac.RoleParenting'
 
 # List of IdP backends, mainly used to show available services in the homepage
 # of user, and to handle SLO for each protocols
-IDP_BACKENDS = plugins.register_plugins_idp_backends(())
+IDP_BACKENDS = ('authentic2.idp.saml.backend.SamlBackend',) + plugins.register_plugins_idp_backends(())
 
 # Whether to autoload SAML 2.0 identity providers and services metadata
 # Only https URLS are accepted.
diff --git a/src/authentic2/urls.py b/src/authentic2/urls.py
index 6eefd462..2e084ef4 100644
--- a/src/authentic2/urls.py
+++ b/src/authentic2/urls.py
@@ -24,6 +24,10 @@ from django.views.generic.base import TemplateView
 from django.views.static import serve as media_serve
 
 from . import plugins, views
+from authentic2.decorators import setting_enabled, required, lasso_required
+import authentic2.idp.saml.app_settings
+
+
 
 admin.autodiscover()
 
@@ -143,3 +147,12 @@ if settings.DEBUG and 'debug_toolbar' in settings.INSTALLED_APPS:
     ] + urlpatterns
 
 urlpatterns = plugins.register_plugins_urls(urlpatterns)
+
+authentic2_idp_saml_urls = required(
+    (
+        setting_enabled('ENABLE', settings=authentic2.idp.saml.app_settings),
+        lasso_required()
+    ),
+    [url(r'^idp/saml2/', include('authentic2.idp.saml.urls'))]
+)
+urlpatterns = authentic2_idp_saml_urls + urlpatterns
-- 
2.20.1

