registerXPathNamespace('t', 'http://schemas.xmlsoap.org/ws/2005/02/trust'); $xml->registerXPathNamespace('wsu', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'); $xml->registerXPathNamespace('wsp', 'http://schemas.xmlsoap.org/ws/2004/09/policy'); $xml->registerXPathNamespace('wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'); $xml->registerXPathNamespace('addr', 'http://www.w3.org/2005/08/addressing'); $addressQuery = $xml->xpath('//t:RequestSecurityTokenResponse/wsp:AppliesTo/addr:EndpointReference/addr:Address'); if (!count($addressQuery) || $addressQuery[0] != $audience) { throw new Exception('The realm in the token response was not valid. Realm: '.@$addressQuery[0]); } $expiresQuery = $xml->xpath('//t:RequestSecurityTokenResponse/t:Lifetime/wsu:Expires'); if (!count($expiresQuery)) { throw new Exception('The expiration time was missing in the token response.'); } else { //2010-08-01T00:56:52.804Z preg_match('/(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2}).*/', $expiresQuery[0], $n); if (count($n) != 7) { throw new Exception('The expiration time was invalid in the token response.'); } $timestamp = mktime($n[4],$n[5],$n[6],$n[2],$n[3],$n[1]); if (time() > $timestamp) { throw new Exception('The token response has expired.'); } } $tokenTypeQuery = $xml->xpath('//t:RequestSecurityTokenResponse/t:TokenType'); if (!count($tokenTypeQuery) || $tokenTypeQuery[0] != $tokenType) { throw new Exception('Invalid token type received: '.@$tokenTypeQuery[0]); } $tokenQuery = $xml->xpath('//t:RequestSecurityTokenResponse/t:RequestedSecurityToken/wsse:BinarySecurityToken'); if (count($tokenQuery)) { return base64_decode($tokenQuery[0]); } throw new Exception('Response token was missing or invalid.'); } } ?>