From b097284017dc9acca61a24e66cbd3c2b68d4a2c5 Mon Sep 17 00:00:00 2001
From: Serghei Mihai <smihai@entrouvert.com>
Date: Wed, 30 Mar 2022 10:48:50 +0200
Subject: [PATCH 2/2] display service logo and name on login and registration
 pages

---
 src/authentic2/context_processors.py          | 11 ++++-
 .../templates/authentic2/login.html           |  3 ++
 .../authentic2/service_info_fragment.html     | 38 ++++++++++++++
 .../registration/registration_form.html       |  2 +
 tests/idp_oidc/test_misc.py                   | 49 +++++++++++++++++++
 5 files changed, 102 insertions(+), 1 deletion(-)
 create mode 100644 src/authentic2/templates/authentic2/service_info_fragment.html

diff --git a/src/authentic2/context_processors.py b/src/authentic2/context_processors.py
index a92f12464..b0393323b 100644
--- a/src/authentic2/context_processors.py
+++ b/src/authentic2/context_processors.py
@@ -66,7 +66,16 @@ def a2_processor(request):
         variables['USER_SWITCHED'] = constants.SWITCH_USER_SESSION_KEY in request.session
         if 'service_pk' in request.session:
             try:
-                variables['service'] = Service.objects.get(pk=request.session['service_pk'])
+                service = Service.objects.get(pk=request.session['service_pk'])
+                variables['service'] = service
+                variables['service_colour'] = service.colour
+                if service.logo:
+                    variables['service_logo_url'] = service.logo.url
+                if service.ou:
+                    variables['service_ou_colour'] = service.ou.colour
+                    if service.ou.logo:
+                        variables['service_ou_logo_url'] = service.ou.logo.url
+
             except Service.DoesNotExist:
                 pass
     return variables
diff --git a/src/authentic2/templates/authentic2/login.html b/src/authentic2/templates/authentic2/login.html
index 1033333f4..f6000e61a 100644
--- a/src/authentic2/templates/authentic2/login.html
+++ b/src/authentic2/templates/authentic2/login.html
@@ -15,6 +15,9 @@
 {% endblock %}
 
 {% block content %}
+
+{% include "authentic2/service_info_fragment.html" %}
+
   <div id="a2-login-forms">
     {% for id, login_block in blocks.items %}
         <span id="css-tab{{ forloop.counter }}"></span>
diff --git a/src/authentic2/templates/authentic2/service_info_fragment.html b/src/authentic2/templates/authentic2/service_info_fragment.html
new file mode 100644
index 000000000..f338bd8c2
--- /dev/null
+++ b/src/authentic2/templates/authentic2/service_info_fragment.html
@@ -0,0 +1,38 @@
+{% load i18n %}
+{% if service %}
+{% firstof service_colour service_ou_colour as colour %}
+{% firstof service.home_url service.ou.home_url as home_url %}
+{% firstof service.name service.ou.name as name %}
+
+{% if home_url %}
+{% block service-colour %}
+{% if colour %}
+<style>
+a.service--link {
+    color: {{ colour }};
+}
+</style>
+{% endif %}
+{% endblock %}
+
+<div id="service-message">
+  {% firstof service_logo_url service_ou_logo_url as logo_url %}
+  {% block service-logo %}
+  {% if logo_url %}
+  <picture>
+    <a href="{{ home_url }}"><img src="{{ logo_url }}" alt="{{ name }}" class="service--logo" /></a>
+  </picture>
+  {% endif %}
+  {% endblock %}
+
+  <div>
+    <a href="{{ home_url }}" class="service--link">
+      {% block service-name-pre %}{% endblock %}
+      {% block service-name %}{{ name }}{% endblock %}
+      {% block service-name-post %}{% endblock %}
+    </a>
+  </div>
+</div>
+{% endif %}
+
+{% endif %}
diff --git a/src/authentic2/templates/registration/registration_form.html b/src/authentic2/templates/registration/registration_form.html
index 72e74198e..29b0151a6 100644
--- a/src/authentic2/templates/registration/registration_form.html
+++ b/src/authentic2/templates/registration/registration_form.html
@@ -9,6 +9,8 @@
 
 <h2>{{ view.title }}</h2>
 
+{% include "authentic2/service_info_fragment.html" %}
+
 {% for id, block in frontends.items %}
   <div class="registration_frontend">
     <h2>{{ block.name }}</h2>
diff --git a/tests/idp_oidc/test_misc.py b/tests/idp_oidc/test_misc.py
index 264cd8b28..e10251dfd 100644
--- a/tests/idp_oidc/test_misc.py
+++ b/tests/idp_oidc/test_misc.py
@@ -24,6 +24,7 @@ import pytest
 from django.contrib.auth import get_user_model
 from django.core.exceptions import ValidationError
 from django.core.files import File
+from django.core.files.uploadedfile import SimpleUploadedFile
 from django.http import QueryDict
 from django.test.utils import override_settings
 from django.urls import reverse
@@ -129,6 +130,54 @@ def test_admin(other_attributes, app, superuser, oidc_settings):
     assert OIDCClient.objects.count() == 1
 
 
+def test_login_from_client_with_home_url(oidc_client, app):
+    redirect_uri = oidc_client.redirect_uris.split()[0]
+    params = {
+        'client_id': oidc_client.client_id,
+        'scope': 'openid profile email',
+        'redirect_uri': redirect_uri,
+        'state': 'xxx',
+        'nonce': 'yyy',
+        'login_hint': 'backoffice john@example.com',
+        'response_type': 'code',
+    }
+    authorize_url = make_url('oidc-authorize', params=params)
+    response = app.get(authorize_url).follow()
+    assert not response.pyquery.find('#service-message')
+
+    ou = oidc_client.ou
+    ou.home_url = 'https://ou.example.net'
+    ou.colour = '#8c00ec'
+    with open('tests/200x200.jpg', 'rb') as fd:
+        ou.logo = SimpleUploadedFile(name='200x200.jpg', content=fd.read())
+        ou.save()
+    response = app.get(authorize_url).follow()
+    assert response.pyquery.find('#service-message')
+    assert response.pyquery.find('a.service--link')
+    link = response.pyquery.find('a.service--link')[0]
+    assert link.attrib['href'] == 'https://ou.example.net'
+    print(response.text)
+    assert 'color: #8c00ec' in response.text
+    assert response.pyquery.find('img.service--logo')[0].attrib['src'] == '/media/services/logos/200x200.jpg'
+
+    oidc_client.home_url = 'https://service.example.net'
+    oidc_client.colour = '#ec008c'
+    with open('tests/201x201.jpg', 'rb') as fd:
+        oidc_client.logo = SimpleUploadedFile(name='201x201.jpg', content=fd.read())
+    oidc_client.save()
+
+    response = app.get(authorize_url).follow()
+    link = response.pyquery.find('a.service--link')[0]
+    assert link.attrib['href'] == 'https://service.example.net'
+    assert 'color: #ec008c' in response.text
+    assert response.pyquery.find('img.service--logo')[0].attrib['src'] == '/media/services/logos/201x201.jpg'
+
+    # check registration page
+    response = response.click('Register!')
+    assert link.attrib['href'] == 'https://service.example.net'
+    assert response.pyquery.find('img.service--logo')[0].attrib['src'] == '/media/services/logos/201x201.jpg'
+
+
 @pytest.mark.parametrize('oidc_client', OIDC_CLIENT_PARAMS, indirect=True)
 @pytest.mark.parametrize('do_not_ask_again', [(True,), (False,)])
 @pytest.mark.parametrize('login_first', [(True,), (False,)])
-- 
2.35.1

