SimpleSAML\Utils\XML::checkSAMLMessage PHP Method

checkSAMLMessage() public static method

This function performs some sanity checks on XML documents, and optionally validates them against their schema if the 'validatexml' debugging option is enabled. A warning will be printed to the log if validation fails.
Author: Olav Morken, UNINETT AS ([email protected])
Author: Jaime Perez, UNINETT AS ([email protected])
public static checkSAMLMessage ( string $message, string $type )
$message string The SAML document we want to check.
$type string The type of document. Can be one of: - 'saml20' - 'saml11' - 'saml-meta'
    public static function checkSAMLMessage($message, $type)
    {
        $allowed_types = array('saml20', 'saml11', 'saml-meta');
        if (!(is_string($message) && in_array($type, $allowed_types))) {
            throw new \InvalidArgumentException('Invalid input parameters.');
        }
        // a SAML message should not contain a doctype-declaration
        if (strpos($message, '<!DOCTYPE') !== false) {
            throw new \SimpleSAML_Error_Exception('XML contained a doctype declaration.');
        }
        // see if debugging is enabled for XML validation
        $debug = \SimpleSAML_Configuration::getInstance()->getArrayize('debug', array('validatexml' => false));
        $enabled = \SimpleSAML_Configuration::getInstance()->getBoolean('debug.validatexml', false);
        if (!(in_array('validatexml', $debug, true) || array_key_exists('validatexml', $debug) && $debug['validatexml'] === true || $enabled)) {
            // XML validation is disabled
            return;
        }
        $result = true;
        switch ($type) {
            case 'saml11':
                $result = self::isValid($message, 'oasis-sstc-saml-schema-protocol-1.1.xsd');
                break;
            case 'saml20':
                $result = self::isValid($message, 'saml-schema-protocol-2.0.xsd');
                break;
            case 'saml-meta':
                $result = self::isValid($message, 'saml-schema-metadata-2.0.xsd');
        }
        if ($result !== true) {
            Logger::warning($result);
        }
    }

Usage Example

Example #1
0
 /**
  * Decode a received response.
  *
  * @param array $post  POST data received.
  * @return SimpleSAML_XML_Shib13_AuthnResponse  Response.
  */
 public function decodeResponse($post)
 {
     assert('is_array($post)');
     if (!array_key_exists('SAMLResponse', $post)) {
         throw new Exception('Missing required SAMLResponse parameter.');
     }
     $rawResponse = $post['SAMLResponse'];
     $samlResponseXML = base64_decode($rawResponse);
     \SimpleSAML\Utils\XML::debugSAMLMessage($samlResponseXML, 'in');
     \SimpleSAML\Utils\XML::checkSAMLMessage($samlResponseXML, 'saml11');
     $samlResponse = new SimpleSAML_XML_Shib13_AuthnResponse();
     $samlResponse->setXML($samlResponseXML);
     if (array_key_exists('TARGET', $post)) {
         $samlResponse->setRelayState($post['TARGET']);
     }
     return $samlResponse;
 }
All Usage Examples Of SimpleSAML\Utils\XML::checkSAMLMessage