]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Users/authentication/SAMLAuthenticate/lib/onelogin/saml/response.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Users / authentication / SAMLAuthenticate / lib / onelogin / saml / response.php
1 <?php
2 /*********************************************************************************
3 Copyright (c) 2010, OneLogin, Inc.
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of the <organization> nor the
14       names of its contributors may be used to endorse or promote products
15       derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ONELOGIN, INC. BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  ********************************************************************************/
28  
29   require 'xmlsec.php';
30
31   /**
32    * Parse the SAML response and maintain the XML for it.
33    */
34   class SamlResponse {
35     /**
36      * A SamlResponse class provided to the constructor.
37      */
38     private $settings;
39
40     /**
41      * The decoded, unprocessed XML assertion provided to the constructor.
42      */
43     public $assertion;
44
45     /**
46      * A DOMDocument class loaded from the $assertion.
47      */
48     public $xml;
49
50     // At this time these private members are unused.
51     private $nameid;
52     private $xpath;
53
54     /**
55      * Construct the response object.
56      *
57      * @param SamlResponse $settings
58      *   A SamlResponse settings object containing the necessary
59      *   x509 certicate to decode the XML.
60      * @param string $assertion
61      *   A UUEncoded SAML assertion from the IdP.
62      */
63     function __construct($settings, $assertion) {
64       $this->settings = $settings;
65       $this->assertion = base64_decode($assertion);
66       $this->xml = new DOMDocument();
67       $this->xml->loadXML($this->assertion);
68     }
69
70     /**
71      * Determine if the SAML Response is valid using the certificate.
72      *
73      * @return
74      *   TRUE if the document passes. This could throw a generic Exception
75      *   if the document or key cannot be found.
76      */
77     function is_valid() {
78       $xmlsec = new SamlXmlSec($this->settings, $this->xml);
79       return $xmlsec->is_valid();
80     }
81
82     /**
83      * Get the NameID provided by the SAML response from the IdP.
84      */
85     function get_nameid() {
86       $xpath = new DOMXPath($this->xml);
87                         $xpath->registerNamespace("samlp","urn:oasis:names:tc:SAML:2.0:protocol");
88                         $xpath->registerNamespace("saml","urn:oasis:names:tc:SAML:2.0:assertion");
89       $query = "/samlp:Response/saml:Assertion/saml:Subject/saml:NameID";
90
91       $entries = $xpath->query($query);
92       return $entries->item(0)->nodeValue;
93     }
94   }
95
96 ?>