/zf/library/Zend/Auth/Adapter/InfoCard.php

http://github.com/eryx/php-framework-benchmark · PHP · 261 lines · 112 code · 20 blank · 129 comment · 1 complexity · 76be6c242498995497da9ce922b9e7ed MD5 · raw file

  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_Auth
  17. * @subpackage Zend_Auth_Adapter
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: InfoCard.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Auth_Adapter_Interface
  24. */
  25. require_once 'Zend/Auth/Adapter/Interface.php';
  26. /**
  27. * @see Zend_Auth_Result
  28. */
  29. require_once 'Zend/Auth/Result.php';
  30. /**
  31. * @see Zend_InfoCard
  32. */
  33. require_once 'Zend/InfoCard.php';
  34. /**
  35. * A Zend_Auth Authentication Adapter allowing the use of Information Cards as an
  36. * authentication mechanism
  37. *
  38. * @category Zend
  39. * @package Zend_Auth
  40. * @subpackage Zend_Auth_Adapter
  41. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Auth_Adapter_InfoCard implements Zend_Auth_Adapter_Interface
  45. {
  46. /**
  47. * The XML Token being authenticated
  48. *
  49. * @var string
  50. */
  51. protected $_xmlToken;
  52. /**
  53. * The instance of Zend_InfoCard
  54. *
  55. * @var Zend_InfoCard
  56. */
  57. protected $_infoCard;
  58. /**
  59. * Constructor
  60. *
  61. * @param string $strXmlDocument The XML Token provided by the client
  62. * @return void
  63. */
  64. public function __construct($strXmlDocument)
  65. {
  66. $this->_xmlToken = $strXmlDocument;
  67. $this->_infoCard = new Zend_InfoCard();
  68. }
  69. /**
  70. * Sets the InfoCard component Adapter to use
  71. *
  72. * @param Zend_InfoCard_Adapter_Interface $a
  73. * @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
  74. */
  75. public function setAdapter(Zend_InfoCard_Adapter_Interface $a)
  76. {
  77. $this->_infoCard->setAdapter($a);
  78. return $this;
  79. }
  80. /**
  81. * Retrieves the InfoCard component adapter being used
  82. *
  83. * @return Zend_InfoCard_Adapter_Interface
  84. */
  85. public function getAdapter()
  86. {
  87. return $this->_infoCard->getAdapter();
  88. }
  89. /**
  90. * Retrieves the InfoCard public key cipher object being used
  91. *
  92. * @return Zend_InfoCard_Cipher_PKI_Interface
  93. */
  94. public function getPKCipherObject()
  95. {
  96. return $this->_infoCard->getPKCipherObject();
  97. }
  98. /**
  99. * Sets the InfoCard public key cipher object to use
  100. *
  101. * @param Zend_InfoCard_Cipher_PKI_Interface $cipherObj
  102. * @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
  103. */
  104. public function setPKICipherObject(Zend_InfoCard_Cipher_PKI_Interface $cipherObj)
  105. {
  106. $this->_infoCard->setPKICipherObject($cipherObj);
  107. return $this;
  108. }
  109. /**
  110. * Retrieves the Symmetric cipher object being used
  111. *
  112. * @return Zend_InfoCard_Cipher_Symmetric_Interface
  113. */
  114. public function getSymCipherObject()
  115. {
  116. return $this->_infoCard->getSymCipherObject();
  117. }
  118. /**
  119. * Sets the InfoCard symmetric cipher object to use
  120. *
  121. * @param Zend_InfoCard_Cipher_Symmetric_Interface $cipherObj
  122. * @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
  123. */
  124. public function setSymCipherObject(Zend_InfoCard_Cipher_Symmetric_Interface $cipherObj)
  125. {
  126. $this->_infoCard->setSymCipherObject($cipherObj);
  127. return $this;
  128. }
  129. /**
  130. * Remove a Certificate Pair by Key ID from the search list
  131. *
  132. * @param string $key_id The Certificate Key ID returned from adding the certificate pair
  133. * @throws Zend_InfoCard_Exception
  134. * @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
  135. */
  136. public function removeCertificatePair($key_id)
  137. {
  138. $this->_infoCard->removeCertificatePair($key_id);
  139. return $this;
  140. }
  141. /**
  142. * Add a Certificate Pair to the list of certificates searched by the component
  143. *
  144. * @param string $private_key_file The path to the private key file for the pair
  145. * @param string $public_key_file The path to the certificate / public key for the pair
  146. * @param string $type (optional) The URI for the type of key pair this is (default RSA with OAEP padding)
  147. * @param string $password (optional) The password for the private key file if necessary
  148. * @throws Zend_InfoCard_Exception
  149. * @return string A key ID representing this key pair in the component
  150. */
  151. public function addCertificatePair($private_key_file, $public_key_file, $type = Zend_InfoCard_Cipher::ENC_RSA_OAEP_MGF1P, $password = null)
  152. {
  153. return $this->_infoCard->addCertificatePair($private_key_file, $public_key_file, $type, $password);
  154. }
  155. /**
  156. * Return a Certificate Pair from a key ID
  157. *
  158. * @param string $key_id The Key ID of the certificate pair in the component
  159. * @throws Zend_InfoCard_Exception
  160. * @return array An array containing the path to the private/public key files,
  161. * the type URI and the password if provided
  162. */
  163. public function getCertificatePair($key_id)
  164. {
  165. return $this->_infoCard->getCertificatePair($key_id);
  166. }
  167. /**
  168. * Set the XML Token to be processed
  169. *
  170. * @param string $strXmlToken The XML token to process
  171. * @return Zend_Auth_Adapter_InfoCard Provides a fluent interface
  172. */
  173. public function setXmlToken($strXmlToken)
  174. {
  175. $this->_xmlToken = $strXmlToken;
  176. return $this;
  177. }
  178. /**
  179. * Get the XML Token being processed
  180. *
  181. * @return string The XML token to be processed
  182. */
  183. public function getXmlToken()
  184. {
  185. return $this->_xmlToken;
  186. }
  187. /**
  188. * Authenticates the XML token
  189. *
  190. * @return Zend_Auth_Result The result of the authentication
  191. */
  192. public function authenticate()
  193. {
  194. try {
  195. $claims = $this->_infoCard->process($this->getXmlToken());
  196. } catch(Exception $e) {
  197. return new Zend_Auth_Result(Zend_Auth_Result::FAILURE , null, array('Exception Thrown',
  198. $e->getMessage(),
  199. $e->getTraceAsString(),
  200. serialize($e)));
  201. }
  202. if(!$claims->isValid()) {
  203. switch($claims->getCode()) {
  204. case Zend_infoCard_Claims::RESULT_PROCESSING_FAILURE:
  205. return new Zend_Auth_Result(
  206. Zend_Auth_Result::FAILURE,
  207. $claims,
  208. array(
  209. 'Processing Failure',
  210. $claims->getErrorMsg()
  211. )
  212. );
  213. break;
  214. case Zend_InfoCard_Claims::RESULT_VALIDATION_FAILURE:
  215. return new Zend_Auth_Result(
  216. Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
  217. $claims,
  218. array(
  219. 'Validation Failure',
  220. $claims->getErrorMsg()
  221. )
  222. );
  223. break;
  224. default:
  225. return new Zend_Auth_Result(
  226. Zend_Auth_Result::FAILURE,
  227. $claims,
  228. array(
  229. 'Unknown Failure',
  230. $claims->getErrorMsg()
  231. )
  232. );
  233. break;
  234. }
  235. }
  236. return new Zend_Auth_Result(
  237. Zend_Auth_Result::SUCCESS,
  238. $claims
  239. );
  240. }
  241. }