From fca4f00b98107988214775ebe3049100757790c8 Mon Sep 17 00:00:00 2001
From: Valentin Deniaud <vdeniaud@entrouvert.com>
Date: Thu, 27 Oct 2022 13:44:43 +0200
Subject: [PATCH] wip

---
 .../authenticators/authenticator_add_form.html | 10 ++++++++++
 .../authenticators/authenticator_detail.html   |  3 +++
 .../authenticators/templatetags/__init__.py    |  0
 .../templatetags/authenticator_extras.py       | 10 ++++++++++
 src/authentic2_auth_oidc/models.py             |  1 +
 .../idp_configuration_info.html                | 10 ++++++++++
 src/authentic2_auth_saml/models.py             |  1 +
 .../idp_configuration_info.html                | 18 ++++++++++++++++++
 8 files changed, 53 insertions(+)
 create mode 100644 src/authentic2/apps/authenticators/templatetags/__init__.py
 create mode 100644 src/authentic2/apps/authenticators/templatetags/authenticator_extras.py
 create mode 100644 src/authentic2_auth_oidc/templates/authentic2_auth_oidc/idp_configuration_info.html
 create mode 100644 src/authentic2_auth_saml/templates/authentic2_auth_saml/idp_configuration_info.html

diff --git a/src/authentic2/apps/authenticators/templates/authentic2/authenticators/authenticator_add_form.html b/src/authentic2/apps/authenticators/templates/authentic2/authenticators/authenticator_add_form.html
index f6aa9043a..71d91d220 100644
--- a/src/authentic2/apps/authenticators/templates/authentic2/authenticators/authenticator_add_form.html
+++ b/src/authentic2/apps/authenticators/templates/authentic2/authenticators/authenticator_add_form.html
@@ -10,6 +10,13 @@
   <form method="post" enctype="multipart/form-data">
     {% csrf_token %}
     {{ form|with_template }}
+
+    {% for authenticator in form.authenticators.values %}
+      {% if authenticator.manager_idp_info_template_name %}
+        {% include authenticator.manager_idp_info_template_name %}
+      {% endif %}
+    {% endfor %}
+
     <div class="buttons">
       <button>{% trans "Add" %}</button>
       <a class="cancel" href="{% url 'a2-manager-authenticators' %}">{% trans 'Cancel' %}</a>
@@ -19,8 +26,11 @@
         $('div#authenticator span.required').remove();
         name_field = $('div#id_name_p');
         $('select#id_authenticator').change(function() {
+          $('div.idp-info').hide();
+          $('div.idp-info.' + this.value).show();
           if(this.value == 'saml' || this.value == 'oidc') {
             name_field.show();
+            $('div#' + this.value + '-idp-info').show();
             $('input#id_name').prop('required', true);
           } else {
             name_field.hide();
diff --git a/src/authentic2/apps/authenticators/templates/authentic2/authenticators/authenticator_detail.html b/src/authentic2/apps/authenticators/templates/authentic2/authenticators/authenticator_detail.html
index a368a10bc..181dc1fc8 100644
--- a/src/authentic2/apps/authenticators/templates/authentic2/authenticators/authenticator_detail.html
+++ b/src/authentic2/apps/authenticators/templates/authentic2/authenticators/authenticator_detail.html
@@ -45,6 +45,9 @@
 
       <div class="pk-tabs--container">
         <div aria-labelledby="tab-description" id="panel-description" role="tabpanel" tabindex="0">
+          {% if object.manager_idp_info_template_name %}
+            {% include object.manager_idp_info_template_name %}
+          {% endif %}
           <ul>
             {% for line in object.get_full_description %}
               <li>{{ line }}</li>
diff --git a/src/authentic2/apps/authenticators/templatetags/__init__.py b/src/authentic2/apps/authenticators/templatetags/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/src/authentic2/apps/authenticators/templatetags/authenticator_extras.py b/src/authentic2/apps/authenticators/templatetags/authenticator_extras.py
new file mode 100644
index 000000000..240eb275d
--- /dev/null
+++ b/src/authentic2/apps/authenticators/templatetags/authenticator_extras.py
@@ -0,0 +1,10 @@
+from django import template
+
+from authentic2.utils.misc import make_url
+
+register = template.Library()
+
+
+@register.simple_tag(takes_context=True)
+def absolute_url(context, view_name, *args, **kwargs):
+    return make_url(view_name, request=context['request'], absolute=True)
diff --git a/src/authentic2_auth_oidc/models.py b/src/authentic2_auth_oidc/models.py
index 37a23987d..c048ce5af 100644
--- a/src/authentic2_auth_oidc/models.py
+++ b/src/authentic2_auth_oidc/models.py
@@ -124,6 +124,7 @@ class OIDCProvider(BaseAuthenticator):
 
     type = 'oidc'
     how = ['oidc']
+    manager_idp_info_template_name = 'authentic2_auth_oidc/idp_configuration_info.html'
     description_fields = ['show_condition', 'issuer', 'scopes', 'strategy', 'created', 'modified']
 
     class Meta:
diff --git a/src/authentic2_auth_oidc/templates/authentic2_auth_oidc/idp_configuration_info.html b/src/authentic2_auth_oidc/templates/authentic2_auth_oidc/idp_configuration_info.html
new file mode 100644
index 000000000..efd767b21
--- /dev/null
+++ b/src/authentic2_auth_oidc/templates/authentic2_auth_oidc/idp_configuration_info.html
@@ -0,0 +1,10 @@
+{% load i18n authenticator_extras %}
+
+<div class="infonotice oidc idp-info">
+  <strong>{% trans "Configuration information for your identity provider" %}</strong>
+  <ul>
+    {% filter urlize %}
+      <li>{% trans "Redirect URI (redirect_uri):" %} {% absolute_url 'oidc-login-callback' %}</li>
+      <li>{% trans "Redirect URI after logout (post_logout_redirect_uri):" %} {% absolute_url 'auth_logout' %}</li>
+    {% endfilter %}
+  </div>
diff --git a/src/authentic2_auth_saml/models.py b/src/authentic2_auth_saml/models.py
index 3aabf1e45..a39e3ecf8 100644
--- a/src/authentic2_auth_saml/models.py
+++ b/src/authentic2_auth_saml/models.py
@@ -162,6 +162,7 @@ class SAMLAuthenticator(BaseAuthenticator):
     type = 'saml'
     how = ['saml']
     manager_view_template_name = 'authentic2_auth_saml/authenticator_detail.html'
+    manager_idp_info_template_name = 'authentic2_auth_saml/idp_configuration_info.html'
     description_fields = ['show_condition', 'metadata_url', 'metadata_path', 'metadata', 'provision']
 
     class Meta:
diff --git a/src/authentic2_auth_saml/templates/authentic2_auth_saml/idp_configuration_info.html b/src/authentic2_auth_saml/templates/authentic2_auth_saml/idp_configuration_info.html
new file mode 100644
index 000000000..5fa1d4caf
--- /dev/null
+++ b/src/authentic2_auth_saml/templates/authentic2_auth_saml/idp_configuration_info.html
@@ -0,0 +1,18 @@
+{% load i18n authenticator_extras %}
+
+<div class="infonotice saml idp-info">
+  <strong>{% trans "Configuration information for your identity provider" %}</strong>
+  <ul>
+    {% filter urlize %}
+      <li>{% trans "Metadata URL:" %} {% absolute_url 'mellon_metadata' %}</li>
+    {% endfilter %}
+    <li>{% trans "Expected attributes:" %}
+      <ul>
+        <li>{% trans "Username (username), mandatory" %}</li>
+        <li>{% trans "Email (email), mandatory" %}</li>
+        <li>{% trans "First name (first_name)" %}</li>
+        <li>{% trans "Last name (last_name)" %}</li>
+      </ul>
+    </li>
+    <ul>
+    </div>
-- 
2.35.1

