AllowedList.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/ProxyChain/AllowedList.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. * ProxyChain is a container for storing chains of valid proxies that can
  30. * be used to validate proxied requests to a service
  31. *
  32. * @class CAS_ProxyChain_AllowedList
  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. class CAS_ProxyChain_AllowedList
  40. {
  41. private $_chains = array();
  42. /**
  43. * Check whether proxies are allowed by configuration
  44. *
  45. * @return bool
  46. */
  47. public function isProxyingAllowed()
  48. {
  49. return (count($this->_chains) > 0);
  50. }
  51. /**
  52. * Add a chain of proxies to the list of possible chains
  53. *
  54. * @param CAS_ProxyChain_Interface $chain A chain of proxies
  55. *
  56. * @return void
  57. */
  58. public function allowProxyChain(CAS_ProxyChain_Interface $chain)
  59. {
  60. $this->_chains[] = $chain;
  61. }
  62. /**
  63. * Check if the proxies found in the response match the allowed proxies
  64. *
  65. * @param array $proxies list of proxies to check
  66. *
  67. * @return bool whether the proxies match the allowed proxies
  68. */
  69. public function isProxyListAllowed(array $proxies)
  70. {
  71. phpCAS::traceBegin();
  72. if (empty($proxies)) {
  73. phpCAS::trace("No proxies were found in the response");
  74. phpCAS::traceEnd(true);
  75. return true;
  76. } elseif (!$this->isProxyingAllowed()) {
  77. phpCAS::trace("Proxies are not allowed");
  78. phpCAS::traceEnd(false);
  79. return false;
  80. } else {
  81. $res = $this->contains($proxies);
  82. phpCAS::traceEnd($res);
  83. return $res;
  84. }
  85. }
  86. /**
  87. * Validate the proxies from the proxy ticket validation against the
  88. * chains that were definded.
  89. *
  90. * @param array $list List of proxies from the proxy ticket validation.
  91. *
  92. * @return bool if any chain fully matches the supplied list
  93. */
  94. public function contains(array $list)
  95. {
  96. phpCAS::traceBegin();
  97. $count = 0;
  98. foreach ($this->_chains as $chain) {
  99. phpCAS::trace("Checking chain ". $count++);
  100. if ($chain->matches($list)) {
  101. phpCAS::traceEnd(true);
  102. return true;
  103. }
  104. }
  105. phpCAS::trace("No proxy chain matches.");
  106. phpCAS::traceEnd(false);
  107. return false;
  108. }
  109. }
  110. ?>