ProxyChain.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.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. * A normal proxy-chain definition that lists each level of the chain as either
  30. * a string or regular expression.
  31. *
  32. * @class CAS_ProxyChain
  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
  40. implements CAS_ProxyChain_Interface
  41. {
  42. protected $chain = array();
  43. /**
  44. * A chain is an array of strings or regexp strings that will be matched
  45. * against. Regexp will be matched with preg_match and strings will be
  46. * matched from the beginning. A string must fully match the beginning of
  47. * an proxy url. So you can define a full domain as acceptable or go further
  48. * down.
  49. * Proxies have to be defined in reverse from the service to the user. If a
  50. * user hits service A get proxied via B to service C the list of acceptable
  51. * proxies on C would be array(B,A);
  52. *
  53. * @param array $chain A chain of proxies
  54. */
  55. public function __construct(array $chain)
  56. {
  57. // Ensure that we have an indexed array
  58. $this->chain = array_values($chain);
  59. }
  60. /**
  61. * Match a list of proxies.
  62. *
  63. * @param array $list The list of proxies in front of this service.
  64. *
  65. * @return bool
  66. */
  67. public function matches(array $list)
  68. {
  69. $list = array_values($list); // Ensure that we have an indexed array
  70. if ($this->isSizeValid($list)) {
  71. $mismatch = false;
  72. foreach ($this->chain as $i => $search) {
  73. $proxy_url = $list[$i];
  74. if (preg_match('/^\/.*\/[ixASUXu]*$/s', $search)) {
  75. if (preg_match($search, $proxy_url)) {
  76. phpCAS::trace(
  77. "Found regexp " . $search . " matching " . $proxy_url
  78. );
  79. } else {
  80. phpCAS::trace(
  81. "No regexp match " . $search . " != " . $proxy_url
  82. );
  83. $mismatch = true;
  84. break;
  85. }
  86. } else {
  87. if (strncasecmp($search, $proxy_url, strlen($search)) == 0) {
  88. phpCAS::trace(
  89. "Found string " . $search . " matching " . $proxy_url
  90. );
  91. } else {
  92. phpCAS::trace(
  93. "No match " . $search . " != " . $proxy_url
  94. );
  95. $mismatch = true;
  96. break;
  97. }
  98. }
  99. }
  100. if (!$mismatch) {
  101. phpCAS::trace("Proxy chain matches");
  102. return true;
  103. }
  104. } else {
  105. phpCAS::trace("Proxy chain skipped: size mismatch");
  106. }
  107. return false;
  108. }
  109. /**
  110. * Validate the size of the the list as compared to our chain.
  111. *
  112. * @param array $list List of proxies
  113. *
  114. * @return bool
  115. */
  116. protected function isSizeValid (array $list)
  117. {
  118. return (sizeof($this->chain) == sizeof($list));
  119. }
  120. }