From b0acccdb34d52403e7daaee075a71f35d5469d21 Mon Sep 17 00:00:00 2001
From: Patrick Radtke <patrick@cirrusidentity.com>
Date: Tue, 19 Sep 2017 16:24:12 -0700
Subject: [PATCH] Show AuthData on authentication status page

It can be useful to see the AuthData when testing an authentication source.

Since the AuthData can be a hierarchy of data of various depths it is converted
into a pretty printed JSON structure.

Since the amount of AuthData can vary greatly, the data is displayed in side
a `<details>` element which requires clicking to view.
---
 dictionaries/status.definition.json | 8 +++++++-
 modules/core/www/authenticate.php   | 2 ++
 templates/status.php                | 7 +++++++
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/dictionaries/status.definition.json b/dictionaries/status.definition.json
index 57030aaab..d81f6e524 100644
--- a/dictionaries/status.definition.json
+++ b/dictionaries/status.definition.json
@@ -37,5 +37,11 @@
 	},
 	"logout": {
 		"en": "Logout"
-	}
+	},
+        "authData_header": {
+                "en": "AuthData"
+        },
+        "authData_summary": {
+                "en": "Click to view AuthData"
+        }
 }
diff --git a/modules/core/www/authenticate.php b/modules/core/www/authenticate.php
index 8e04e4a6c..bfb717c80 100644
--- a/modules/core/www/authenticate.php
+++ b/modules/core/www/authenticate.php
@@ -37,11 +37,13 @@ if (!$as->isAuthenticated()) {
 }
 
 $attributes = $as->getAttributes();
+$authData = $as->getAuthDataArray();
 
 $t = new SimpleSAML_XHTML_Template($config, 'status.php', 'attributes');
 
 $t->data['header'] = '{status:header_saml20_sp}';
 $t->data['attributes'] = $attributes;
+$t->data['authData'] = $authData;
 $t->data['nameid'] = !is_null($as->getAuthData('saml:sp:NameID')) ? $as->getAuthData('saml:sp:NameID') : false;
 $t->data['logouturl'] = \SimpleSAML\Utils\HTTP::getSelfURLNoQuery().'?as='.urlencode($asId).'&logout';
 $t->show();
diff --git a/templates/status.php b/templates/status.php
index 798b546ac..95333f9a2 100644
--- a/templates/status.php
+++ b/templates/status.php
@@ -60,6 +60,13 @@ if ($nameid !== false) {
     echo(present_attributes($this, $list, ''));
 }
 
+$authData = $this->data['authData'];
+if (isset($authData)) {
+    echo "<h2>".$this->t('{status:authData_header}')."</h2>";
+    echo '<details><summary>' . $this->t('{status:authData_summary}') . '</summary>'; 
+    echo('<pre>' . htmlspecialchars(json_encode($this->data['authData'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)) . '</pre>');
+    echo '</details>';
+}
 if (isset($this->data['logout'])) {
     echo('<h2>'.$this->t('{status:logout}').'</h2>');
     echo('<p>'.$this->data['logout'].'</p>');
-- 
GitLab