From d718ebc34ef9b6fd30c6d5fcf6bd296430e3c560 Mon Sep 17 00:00:00 2001
From: Patrick Radtke <patrick@cirrusidentity.com>
Date: Tue, 23 Feb 2016 13:11:32 -0800
Subject: [PATCH] Expose RegistrationInfo in parsed metadata

- Add test case
- Fix .gitignore since it ignored all metadata folders
---
 .gitignore                                    |  2 +-
 lib/SimpleSAML/Metadata/SAMLParser.php        | 15 +++++++
 .../SimpleSAML/Metadata/SAMLBuilderTest.php   |  2 +-
 .../SimpleSAML/Metadata/SAMLParserTest.php    | 39 +++++++++++++++++++
 4 files changed, 56 insertions(+), 2 deletions(-)
 create mode 100644 tests/lib/SimpleSAML/Metadata/SAMLParserTest.php

diff --git a/.gitignore b/.gitignore
index 71c73837a..0a9f2384f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,7 @@
 .gitignore
 log
 config
-metadata
+./metadata
 cert
 
 !config/.gitkeep
diff --git a/lib/SimpleSAML/Metadata/SAMLParser.php b/lib/SimpleSAML/Metadata/SAMLParser.php
index ad725e8b2..2786fff5a 100644
--- a/lib/SimpleSAML/Metadata/SAMLParser.php
+++ b/lib/SimpleSAML/Metadata/SAMLParser.php
@@ -124,6 +124,11 @@ class SimpleSAML_Metadata_SAMLParser
      */
     private $entityAttributes;
 
+    /**
+     * An associative array of attributes from the RegistrationInfo element.
+     * @var array
+     */
+    private $registrationInfo;
 
     /**
      * @var array
@@ -180,6 +185,7 @@ class SimpleSAML_Metadata_SAMLParser
         $this->scopes = $ext['scope'];
         $this->tags = $ext['tags'];
         $this->entityAttributes = $ext['EntityAttributes'];
+        $this->registrationInfo = $ext['RegistrationInfo'];
 
         // look over the RoleDescriptors
         foreach ($entityElement->RoleDescriptor as $child) {
@@ -486,6 +492,11 @@ class SimpleSAML_Metadata_SAMLParser
             $metadata['tags'] = $tags;
         }
 
+
+        if (!empty($this->registrationInfo)) {
+            $metadata['RegistrationInfo'] = $this->registrationInfo;
+        }
+
         if (!empty($this->entityAttributes)) {
             $metadata['EntityAttributes'] = $this->entityAttributes;
 
@@ -993,6 +1004,7 @@ class SimpleSAML_Metadata_SAMLParser
             'scope'            => array(),
             'tags'             => array(),
             'EntityAttributes' => array(),
+            'RegistrationInfo' => array(),
             'UIInfo'           => array(),
             'DiscoHints'       => array(),
         );
@@ -1006,6 +1018,9 @@ class SimpleSAML_Metadata_SAMLParser
 
             // Entity Attributes are only allowed at entity level extensions and not at RoleDescriptor level
             if ($element instanceof SAML2_XML_md_EntityDescriptor) {
+                if ($e instanceof SAML2_XML_mdrpi_RegistrationInfo) {
+                    $ret['RegistrationInfo']['registrationAuthority'] = $e->registrationAuthority;
+                }
                 if ($e instanceof SAML2_XML_mdattr_EntityAttributes && !empty($e->children)) {
                     foreach ($e->children as $attr) {
                         // only saml:Attribute are currently supported here. The specifications also allows
diff --git a/tests/lib/SimpleSAML/Metadata/SAMLBuilderTest.php b/tests/lib/SimpleSAML/Metadata/SAMLBuilderTest.php
index 48caa5eaf..8f0fdddb3 100644
--- a/tests/lib/SimpleSAML/Metadata/SAMLBuilderTest.php
+++ b/tests/lib/SimpleSAML/Metadata/SAMLBuilderTest.php
@@ -8,7 +8,7 @@ class SimpleSAML_Metadata_SAMLBuilderTest extends PHPUnit_Framework_TestCase
 {
 
     /**
-     * Test the requeste attributes are valued correctly.
+     * Test the requested attributes are valued correctly.
      */
     public function testAttributes()
     {
diff --git a/tests/lib/SimpleSAML/Metadata/SAMLParserTest.php b/tests/lib/SimpleSAML/Metadata/SAMLParserTest.php
new file mode 100644
index 000000000..95874317c
--- /dev/null
+++ b/tests/lib/SimpleSAML/Metadata/SAMLParserTest.php
@@ -0,0 +1,39 @@
+<?php
+namespace SimpleSAML\Metadata;
+
+/**
+ * Test SAML parsing
+ */
+class SAMLParserTest extends \PHPUnit_Framework_TestCase
+{
+
+    /**
+     * Test Registration Info is parsed
+     */
+    public function testRegistrationInfo()
+    {
+        $expected = array(
+            'registrationAuthority' => 'https://incommon.org',
+        );
+
+        $document = \SAML2_DOMDocumentFactory::fromString(
+            <<<XML
+<EntityDescriptor entityID="theEntityID"
+ xmlns="urn:oasis:names:tc:SAML:2.0:metadata" xmlns:mdrpi="urn:oasis:names:tc:SAML:metadata:rpi">
+  <Extensions>
+    <mdrpi:RegistrationInfo registrationAuthority="https://incommon.org"/>
+     </Extensions>
+  <SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
+  </SPSSODescriptor>
+</EntityDescriptor>
+XML
+        );
+
+
+        $entities = \SimpleSAML_Metadata_SAMLParser::parseDescriptorsElement($document->documentElement);
+        $this->assertArrayHasKey('theEntityID', $entities);
+        // RegistrationInfo is accessible in the SP or IDP metadata accessors
+        $this->assertEquals($expected, $entities['theEntityID']->getMetadata20SP()['RegistrationInfo']);
+
+    }
+}
-- 
GitLab