public function testGetAttributes() { $assertion = file_get_contents(TEST_ROOT . '/responses/response1.xml.base64'); $response = new OneLogin_Saml_Response($this->_settings, $assertion); $expectedAttributes = array('uid' => array('demo'), 'another_value' => array('value')); $this->assertEquals($expectedAttributes, $response->getAttributes()); // An assertion that has no attributes should return an empty array when asked for the attributes $assertion = file_get_contents(TEST_ROOT . '/responses/response2.xml.base64'); $response = new OneLogin_Saml_Response($this->_settings, $assertion); $this->assertEmpty($response->getAttributes()); }
/** * Updates the custom fields listed in settings->saml_settings['update'] in * our db records with the data from the xml in the saml assertion. * Every field listed in the ['update'] array is a key whose value is an attribute name. * If the value of the node does not equal the value in our * records, update our records to match the value from the assertion. * * @param User $user - user fetched from our db. * @return int - 0 = no action taken, 1 = user record saved, -1 = no update. */ protected function updateCustomFields($user) { $customFields = $this->getCustomFields('update'); if (empty($customFields)) { $GLOBALS['log']->debug("No custom fields! So returning 0."); return 0; } $GLOBALS['log']->debug("updateCF()... userid={$user->id}"); $attrs = $this->samlresponse->getAttributes(); $customFieldUpdated = false; foreach ($customFields as $field => $attrfield) { $GLOBALS['log']->debug("Top of fields loop with {$field}."); if (!property_exists($user, $field)) { $GLOBALS['log']->debug("{$field} is not a user field."); // custom field not listed in db query results! continue; } if (!$this->hasAttribute($attrfield)) { continue; } $customFieldValue = $user->{$field}; $xmlValue = $this->getAttribute($attrfield); $GLOBALS['log']->debug("{$field} SAML returned {$xmlValue}"); if ($customFieldValue != $xmlValue) { // need to update our user record. $customFieldUpdated = true; $user->{$field} = $xmlValue; $GLOBALS['log']->debug("db is out of date. setting {$field} to {$xmlValue}"); } } if ($customFieldUpdated) { $GLOBALS['log']->debug("updateCustomFields calling user->save() and returning 1"); $user->save(); return 1; } $GLOBALS['log']->debug("updateCustomFields found no fields to update. Returning -1"); return -1; }
/** * SAMPLE Code to demonstrate how to handle a SAML assertion response. * * The URL of this file will have been given during the SAML authorization. * After a successful authorization, the browser will be directed to this * link where it will send a certified response via $_POST. */ error_reporting(E_ALL); $settings = null; require 'settings.php'; $samlResponse = new OneLogin_Saml_Response($settings, $_POST['SAMLResponse']); try { if ($samlResponse->isValid()) { echo 'You are: ' . $samlResponse->getNameId() . '<br>'; $attributes = $samlResponse->getAttributes(); if (!empty($attributes)) { echo 'You have the following attributes:<br>'; echo '<table><thead><th>Name</th><th>Values</th></thead><tbody>'; foreach ($attributes as $attributeName => $attributeValues) { echo '<tr><td>' . htmlentities($attributeName) . '</td><td><ul>'; foreach ($attributeValues as $attributeValue) { echo '<li>' . htmlentities($attributeValue) . '</li>'; } echo '</ul></td></tr>'; } echo '</tbody></table><br><br>'; echo "The v.1 of the Onelogin's PHP SAML Tookit does not support SLO."; } } else { echo 'Invalid SAML response.';