From 476fc1a8073309ae7f16f73ebcb7e107f40dd07c Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Thu, 11 Aug 2011 07:45:23 +0000
Subject: [PATCH] Add support for custom names on authsources in multiauth.

Thanks to Lorenzo Gil Sanchez for implementing this!

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2890 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/multiauth/docs/multiauth.txt          | 41 ++++++++++++++++++-
 .../multiauth/lib/Auth/Source/MultiAuth.php   | 36 +++++++++++++++-
 modules/multiauth/templates/selectsource.php  |  5 ++-
 3 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/modules/multiauth/docs/multiauth.txt b/modules/multiauth/docs/multiauth.txt
index 4c0f52be2..347b2c242 100644
--- a/modules/multiauth/docs/multiauth.txt
+++ b/modules/multiauth/docs/multiauth.txt
@@ -29,7 +29,21 @@ authentication source:
          * The available authentication sources.
          * They must be defined in this authsources.php file.
          */
-        'sources' => array('example-saml', 'example-admin'),
+        'sources' => array(
+            'example-saml' => array(
+                'text' => array(
+                    'en' => 'Log in using a SAML SP',
+                    'es' => 'Entrar usando un SP SAML',
+                ),
+                'css-class' => 'SAML',
+            ),
+            'example-admin' => array(
+                'text' => array(
+                    'en' => 'Log in using the admin password',
+                    'es' => 'Entrar usando la contraseña de administrador',
+                ),
+            ),
+        ),
     ),
 
     'example-saml' => array(
@@ -52,7 +66,30 @@ authentication sources defined in the `config/authsources.php`
 file. The order in this array does not matter since the user
 is the one that decides which one to use.
 
-I is possible to add the parameter `source` to the calling URL, 
+The keys of the sources array are the identifiers of authentication
+sources defined in the authsources.php configuration file. The
+values are arrays with information used to create the user
+interface that will let the user select the authentication source
+he wants. Older versions of the multiauth module did not have
+this structure and just have the authsources identifiers as the
+values of the sources array. It has been improved in a backwards
+compatibility fashion so both cases should work.
+
+So each source in the sources array has a key and a value. As
+mentioned above the key is the authsource identifier and the value
+is another array with two optional keys: 'text' and 'css-class'.
+The text element is another array with localized strings for one
+or more languages. These texts will be shown in the selectsource.php
+view. Note that you should at least enter the text in the default
+language as specified in your config.php file. The css-class
+element is a string with the css class that will be applied to
+the <li> element in the selectsource.php view. By default the
+authtype of the authsource is used as the css class with colons
+replaced by dashes. So in the previous example, the css class used
+in the 'example-admin' authentication source would be
+'core-AdminPassword'.
+
+It is possible to add the parameter `source` to the calling URL, 
 when accessing a service, to allow the user to preselect the
 authsource to be used. This can be handy if you support different
 authentication types for differen types of users and you want the 
diff --git a/modules/multiauth/lib/Auth/Source/MultiAuth.php b/modules/multiauth/lib/Auth/Source/MultiAuth.php
index d54ee15c8..d61356449 100644
--- a/modules/multiauth/lib/Auth/Source/MultiAuth.php
+++ b/modules/multiauth/lib/Auth/Source/MultiAuth.php
@@ -53,7 +53,41 @@ class sspmod_multiauth_Auth_Source_MultiAuth extends SimpleSAML_Auth_Source {
 			throw new Exception('The required "sources" config option was not found');
 		}
 
-		$this->sources = $config['sources'];
+		$globalConfiguration = SimpleSAML_Configuration::getInstance();
+		$defaultLanguage = $globalConfiguration->getString('language.default', 'en');
+		$authsources = SimpleSAML_Configuration::getConfig('authsources.php');
+		$this->sources = array();
+		foreach($config['sources'] as $source => $info) {
+
+			if (is_int($source)) { // Backwards compatibility 
+				$source = $info;
+				$info = array();
+			}
+
+			if (array_key_exists('text', $info)) {
+				$text = $info['text'];
+			} else {
+				$text = array($defaultLanguage => $source);
+			}
+
+			if (array_key_exists('css-class', $info)) {
+				$css_class = $info['css-class'];
+			} else {
+				/* Use the authtype as the css class */
+				$authconfig = $authsources->getArray($source, NULL);
+				if (!array_key_exists(0, $authconfig) || !is_string($authconfig[0])) {
+					$css_class = "";
+				} else {
+					$css_class = str_replace(":", "-", $authconfig[0]);
+				}
+			}
+
+			$this->sources[] = array(
+				'source' => $source,
+				'text' => $text,
+				'css_class' => $css_class,
+			);
+		}
 	}
 
 	/**
diff --git a/modules/multiauth/templates/selectsource.php b/modules/multiauth/templates/selectsource.php
index 5d5290cd1..135a43a2f 100644
--- a/modules/multiauth/templates/selectsource.php
+++ b/modules/multiauth/templates/selectsource.php
@@ -11,9 +11,10 @@ $this->includeAtTemplateBase('includes/header.php');
 <ul>
 <?php
 foreach($this->data['sources'] as $source) {
-	echo '<li><a href="?source=' . htmlspecialchars($source) .
+	echo '<li class="' . htmlspecialchars($source['css_class']) . ' authsource">' .
+		'<a href="?source=' . htmlspecialchars($source['source']) .
 		'&AuthState=' . htmlspecialchars($this->data['authstate']) . '">' .
-		htmlspecialchars($source) . '</a></li>';
+		'<span>' . htmlspecialchars($this->t($source['text'])) . '</span></a></li>';
 }
 ?>
 </ul>
-- 
GitLab