Abstract.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Licensed to Jasig under one or more contributor license
  4. * agreements. See the NOTICE file distributed with this work for
  5. * additional information regarding copyright ownership.
  6. *
  7. * Jasig licenses this file to you under the Apache License,
  8. * Version 2.0 (the "License"); you may not use this file except in
  9. * compliance with the License. You may obtain a copy of the License at:
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * PHP Version 5
  20. *
  21. * @file CAS/ProxiedService/Abstract.php
  22. * @category Authentication
  23. * @package PhpCAS
  24. * @author Adam Franco <afranco@middlebury.edu>
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  26. * @link https://wiki.jasig.org/display/CASC/phpCAS
  27. */
  28. /**
  29. * This class implements common methods for ProxiedService implementations included
  30. * with phpCAS.
  31. *
  32. * @class CAS_ProxiedService_Abstract
  33. * @category Authentication
  34. * @package PhpCAS
  35. * @author Adam Franco <afranco@middlebury.edu>
  36. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  37. * @link https://wiki.jasig.org/display/CASC/phpCAS
  38. */
  39. abstract class CAS_ProxiedService_Abstract
  40. implements CAS_ProxiedService, CAS_ProxiedService_Testable
  41. {
  42. /**
  43. * The proxy ticket that can be used when making service requests.
  44. * @var string $_proxyTicket;
  45. */
  46. private $_proxyTicket;
  47. /**
  48. * Register a proxy ticket with the Proxy that it can use when making requests.
  49. *
  50. * @param string $proxyTicket proxy ticket
  51. *
  52. * @return void
  53. * @throws InvalidArgumentException If the $proxyTicket is invalid.
  54. * @throws CAS_OutOfSequenceException If called after a proxy ticket has
  55. * already been initialized/set.
  56. */
  57. public function setProxyTicket ($proxyTicket)
  58. {
  59. if (empty($proxyTicket)) {
  60. throw new CAS_InvalidArgumentException(
  61. 'Trying to initialize with an empty proxy ticket.'
  62. );
  63. }
  64. if (!empty($this->_proxyTicket)) {
  65. throw new CAS_OutOfSequenceException(
  66. 'Already initialized, cannot change the proxy ticket.'
  67. );
  68. }
  69. $this->_proxyTicket = $proxyTicket;
  70. }
  71. /**
  72. * Answer the proxy ticket to be used when making requests.
  73. *
  74. * @return string
  75. * @throws CAS_OutOfSequenceException If called before a proxy ticket has
  76. * already been initialized/set.
  77. */
  78. protected function getProxyTicket ()
  79. {
  80. if (empty($this->_proxyTicket)) {
  81. throw new CAS_OutOfSequenceException(
  82. 'No proxy ticket yet. Call $this->initializeProxyTicket() to aquire the proxy ticket.'
  83. );
  84. }
  85. return $this->_proxyTicket;
  86. }
  87. /**
  88. * @var CAS_Client $_casClient;
  89. */
  90. private $_casClient;
  91. /**
  92. * Use a particular CAS_Client->initializeProxiedService() rather than the
  93. * static phpCAS::initializeProxiedService().
  94. *
  95. * This method should not be called in standard operation, but is needed for unit
  96. * testing.
  97. *
  98. * @param CAS_Client $casClient cas client
  99. *
  100. * @return void
  101. * @throws CAS_OutOfSequenceException If called after a proxy ticket has
  102. * already been initialized/set.
  103. */
  104. public function setCasClient (CAS_Client $casClient)
  105. {
  106. if (!empty($this->_proxyTicket)) {
  107. throw new CAS_OutOfSequenceException(
  108. 'Already initialized, cannot change the CAS_Client.'
  109. );
  110. }
  111. $this->_casClient = $casClient;
  112. }
  113. /**
  114. * Fetch our proxy ticket.
  115. *
  116. * Descendent classes should call this method once their service URL is available
  117. * to initialize their proxy ticket.
  118. *
  119. * @return void
  120. * @throws CAS_OutOfSequenceException If called after a proxy ticket has
  121. * already been initialized.
  122. */
  123. protected function initializeProxyTicket()
  124. {
  125. if (!empty($this->_proxyTicket)) {
  126. throw new CAS_OutOfSequenceException(
  127. 'Already initialized, cannot initialize again.'
  128. );
  129. }
  130. // Allow usage of a particular CAS_Client for unit testing.
  131. if (empty($this->_casClient)) {
  132. phpCAS::initializeProxiedService($this);
  133. } else {
  134. $this->_casClient->initializeProxiedService($this);
  135. }
  136. }
  137. }
  138. ?>