2
0

Saml.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_InfoCard
  17. * @subpackage Zend_InfoCard_Xml
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_InfoCard_Xml_Element
  24. */
  25. require_once 'Zend/InfoCard/Xml/Element.php';
  26. /**
  27. * Zend_InfoCard_Xml_Assertion_Interface
  28. */
  29. require_once 'Zend/InfoCard/Xml/Assertion/Interface.php';
  30. /**
  31. * A Xml Assertion Document in SAML Token format
  32. *
  33. * @category Zend
  34. * @package Zend_InfoCard
  35. * @subpackage Zend_InfoCard_Xml
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_InfoCard_Xml_Assertion_Saml
  40. extends Zend_InfoCard_Xml_Element
  41. implements Zend_InfoCard_Xml_Assertion_Interface
  42. {
  43. /**
  44. * Audience Restriction Condition
  45. */
  46. const CONDITION_AUDIENCE = 'AudienceRestrictionCondition';
  47. /**
  48. * The URI for a 'bearer' confirmation
  49. */
  50. const CONFIRMATION_BEARER = 'urn:oasis:names:tc:SAML:1.0:cm:bearer';
  51. /**
  52. * The amount of time in seconds to buffer when checking conditions to ensure
  53. * that differences between client/server clocks don't interfer too much
  54. */
  55. const CONDITION_TIME_ADJ = 3600; // +- 5 minutes
  56. protected function _getServerName() {
  57. return $_SERVER['SERVER_NAME'];
  58. }
  59. protected function _getServerPort() {
  60. return $_SERVER['SERVER_PORT'];
  61. }
  62. /**
  63. * Validate the conditions array returned from the getConditions() call
  64. *
  65. * @param array $conditions An array of condtions for the assertion taken from getConditions()
  66. * @return mixed Boolean true on success, an array of condition, error message on failure
  67. */
  68. public function validateConditions(Array $conditions)
  69. {
  70. $currentTime = time();
  71. if(!empty($conditions)) {
  72. foreach($conditions as $condition => $conditionValue) {
  73. switch(strtolower($condition)) {
  74. case 'audiencerestrictioncondition':
  75. $serverName = $this->_getServerName();
  76. $serverPort = $this->_getServerPort();
  77. $self_aliases[] = $serverName;
  78. $self_aliases[] = "{{$serverName}:{$serverPort}";
  79. $found = false;
  80. if(is_array($conditionValue)) {
  81. foreach($conditionValue as $audience) {
  82. list(,,$audience) = explode('/', $audience);
  83. if(in_array($audience, $self_aliases)) {
  84. $found = true;
  85. break;
  86. }
  87. }
  88. }
  89. if(!$found) {
  90. return array($condition, 'Could not find self in allowed audience list');
  91. }
  92. break;
  93. case 'notbefore':
  94. $notbeforetime = strtotime($conditionValue);
  95. if($currentTime < $notbeforetime) {
  96. if($currentTime + self::CONDITION_TIME_ADJ < $notbeforetime) {
  97. return array($condition, 'Current time is before specified window');
  98. }
  99. }
  100. break;
  101. case 'notonorafter':
  102. $notonoraftertime = strtotime($conditionValue);
  103. if($currentTime >= $notonoraftertime) {
  104. if($currentTime - self::CONDITION_TIME_ADJ >= $notonoraftertime) {
  105. return array($condition, 'Current time is after specified window');
  106. }
  107. }
  108. break;
  109. }
  110. }
  111. }
  112. return true;
  113. }
  114. /**
  115. * Get the Assertion URI for this type of Assertion
  116. *
  117. * @return string the Assertion URI
  118. */
  119. public function getAssertionURI()
  120. {
  121. return Zend_InfoCard_Xml_Assertion::TYPE_SAML;
  122. }
  123. /**
  124. * Get the Major Version of the SAML Assertion
  125. *
  126. * @return integer The major version number
  127. */
  128. public function getMajorVersion()
  129. {
  130. return (int)(string)$this['MajorVersion'];
  131. }
  132. /**
  133. * The Minor Version of the SAML Assertion
  134. *
  135. * @return integer The minor version number
  136. */
  137. public function getMinorVersion()
  138. {
  139. return (int)(string)$this['MinorVersion'];
  140. }
  141. /**
  142. * Get the Assertion ID of the assertion
  143. *
  144. * @return string The Assertion ID
  145. */
  146. public function getAssertionID()
  147. {
  148. return (string)$this['AssertionID'];
  149. }
  150. /**
  151. * Get the Issuer URI of the assertion
  152. *
  153. * @return string the URI of the assertion Issuer
  154. */
  155. public function getIssuer()
  156. {
  157. return (string)$this['Issuer'];
  158. }
  159. /**
  160. * Get the Timestamp of when the assertion was issued
  161. *
  162. * @return integer a UNIX timestamp representing when the assertion was issued
  163. */
  164. public function getIssuedTimestamp()
  165. {
  166. return strtotime((string)$this['IssueInstant']);
  167. }
  168. /**
  169. * Return an array of conditions which the assertions are predicated on
  170. *
  171. * @throws Zend_InfoCard_Xml_Exception
  172. * @return array an array of conditions
  173. */
  174. public function getConditions()
  175. {
  176. list($conditions) = $this->xpath("//saml:Conditions");
  177. if(!($conditions instanceof Zend_InfoCard_Xml_Element)) {
  178. throw new Zend_InfoCard_Xml_Exception("Unable to find the saml:Conditions block");
  179. }
  180. $retval = array();
  181. foreach($conditions->children('urn:oasis:names:tc:SAML:1.0:assertion') as $key => $value) {
  182. switch($key) {
  183. case self::CONDITION_AUDIENCE:
  184. foreach($value->children('urn:oasis:names:tc:SAML:1.0:assertion') as $audience_key => $audience_value) {
  185. if($audience_key == 'Audience') {
  186. $retval[$key][] = (string)$audience_value;
  187. }
  188. }
  189. break;
  190. }
  191. }
  192. $retval['NotBefore'] = (string)$conditions['NotBefore'];
  193. $retval['NotOnOrAfter'] = (string)$conditions['NotOnOrAfter'];
  194. return $retval;
  195. }
  196. /**
  197. * Get they KeyInfo element for the Subject KeyInfo block
  198. *
  199. * @todo Not Yet Implemented
  200. * @ignore
  201. */
  202. public function getSubjectKeyInfo()
  203. {
  204. /**
  205. * @todo Not sure if this is part of the scope for now..
  206. */
  207. if($this->getConfirmationMethod() == self::CONFIRMATION_BEARER) {
  208. throw new Zend_InfoCard_Xml_Exception("Cannot get Subject Key Info when Confirmation Method was Bearer");
  209. }
  210. }
  211. /**
  212. * Return the Confirmation Method URI used in the Assertion
  213. *
  214. * @return string The confirmation method URI
  215. */
  216. public function getConfirmationMethod()
  217. {
  218. list($confirmation) = $this->xPath("//saml:ConfirmationMethod");
  219. return (string)$confirmation;
  220. }
  221. /**
  222. * Return an array of attributes (claims) contained within the assertion
  223. *
  224. * @return array An array of attributes / claims within the assertion
  225. */
  226. public function getAttributes()
  227. {
  228. $attributes = $this->xPath('//saml:Attribute');
  229. $retval = array();
  230. foreach($attributes as $key => $value) {
  231. $retkey = (string)$value['AttributeNamespace'].'/'.(string)$value['AttributeName'];
  232. $retval[$retkey]['name'] = (string)$value['AttributeName'];
  233. $retval[$retkey]['namespace'] = (string)$value['AttributeNamespace'];
  234. list($aValue) = $value->children('urn:oasis:names:tc:SAML:1.0:assertion');
  235. $retval[$retkey]['value'] = (string)$aValue;
  236. }
  237. return $retval;
  238. }
  239. }