PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/public/plugin/ckfinder/core/connector/php/vendor/microsoft/windowsazure/WindowsAzure/ServiceBus/Internal/WrapTokenManager.php

https://gitlab.com/vietdhtn/myweb
PHP | 234 lines | 103 code | 33 blank | 98 comment | 8 complexity | 2402bc9d6189d0a8b39ffb25389830c9 MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\ServiceBus\Internal
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/WindowsAzure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\ServiceBus\Internal;
  24. use WindowsAzure\Common\Configuration;
  25. use WindowsAzure\Common\ServicesBuilder;
  26. use WindowsAzure\Common\Internal\Resources;
  27. use WindowsAzure\Common\Internal\Validate;
  28. use WindowsAzure\ServiceBus\Internal\WrapRestProxy;
  29. use WindowsAzure\ServiceBus\Internal\ActiveToken;
  30. /**
  31. * Manages WRAP tokens.
  32. *
  33. * @category Microsoft
  34. * @package WindowsAzure\ServiceBus\Internal
  35. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  36. * @copyright 2012 Microsoft Corporation
  37. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  38. * @version Release: 0.4.0_2014-01
  39. * @link https://github.com/WindowsAzure/azure-sdk-for-php
  40. */
  41. class WrapTokenManager
  42. {
  43. /**
  44. * The Uri of the WRAP service.
  45. *
  46. * @var string
  47. */
  48. private $_wrapUri;
  49. /**
  50. * The user name of the WRAP service.
  51. *
  52. * @var string
  53. */
  54. private $_wrapName;
  55. /**
  56. * The password of the WRAP service.
  57. *
  58. * @var string
  59. */
  60. private $_wrapPassword;
  61. /**
  62. * The proxy of the WRAP service.
  63. *
  64. * @var string
  65. */
  66. private $_wrapRestProxy;
  67. /**
  68. * The active WRAP access tokens.
  69. *
  70. * @var array
  71. */
  72. private $_activeTokens;
  73. /**
  74. * Creates a WRAP token manager with specified parameters.
  75. *
  76. * @param string $wrapUri The URI of the WRAP service.
  77. * @param string $wrapName The user name of the WRAP service.
  78. * @param string $wrapPassword The password of the WRAP service.
  79. * @param IWrap $wrapRestProxy The WRAP service REST proxy.
  80. */
  81. public function __construct($wrapUri, $wrapName, $wrapPassword, $wrapRestProxy)
  82. {
  83. Validate::isString($wrapUri, 'wrapUri');
  84. Validate::isString($wrapName, 'wrapName');
  85. Validate::isString($wrapPassword, 'wrapPassword');
  86. Validate::notNullOrEmpty($wrapRestProxy, 'wrapRestProxy');
  87. $this->_wrapUri = $wrapUri;
  88. $this->_wrapName = $wrapName;
  89. $this->_wrapPassword = $wrapPassword;
  90. $this->_wrapRestProxy = $wrapRestProxy;
  91. $this->_activeTokens = array();
  92. }
  93. /**
  94. * Gets WRAP access token with sepcified target Uri.
  95. *
  96. * @param string $targetUri The target Uri of the WRAP access Token.
  97. *
  98. * @return string
  99. */
  100. public function getAccessToken($targetUri)
  101. {
  102. Validate::isString($targetUri, '$targetUri');
  103. $this->_sweepExpiredTokens();
  104. $scopeUri = $this->_createScopeUri($targetUri);
  105. if (array_key_exists($scopeUri, $this->_activeTokens)) {
  106. $activeToken = $this->_activeTokens[$scopeUri];
  107. return $activeToken->getWrapAccessTokenResult()->getAccessToken();
  108. }
  109. $wrapAccessTokenResult = $this->_wrapRestProxy->wrapAccessToken(
  110. $this->_wrapUri,
  111. $this->_wrapName,
  112. $this->_wrapPassword,
  113. $scopeUri
  114. );
  115. $expirationDateTime = new \DateTime("now");
  116. $expiresIn = intval($wrapAccessTokenResult->getExpiresIn() / 2);
  117. $expirationDateTime = $expirationDateTime->add(
  118. new \DateInterval('PT'.$expiresIn.'S')
  119. );
  120. $acquiredActiveToken = new ActiveToken($wrapAccessTokenResult);
  121. $acquiredActiveToken->setExpirationDateTime($expirationDateTime);
  122. $this->_activeTokens[$scopeUri] = $acquiredActiveToken;
  123. return $wrapAccessTokenResult->getAccessToken();
  124. }
  125. /**
  126. * Removes the expired WRAP access tokens.
  127. *
  128. * @return none
  129. */
  130. private function _sweepExpiredTokens()
  131. {
  132. foreach ($this->_activeTokens as $scopeUri => $activeToken) {
  133. $currentDateTime = new \DateTime("now");
  134. if ($activeToken->getExpirationDateTime() < $currentDateTime ) {
  135. unset($this->_activeTokens[$scopeUri]);
  136. }
  137. }
  138. }
  139. /**
  140. * Creates a SCOPE URI with specified target URI.
  141. *
  142. * @param array $targetUri The target URI.
  143. *
  144. * @return string
  145. */
  146. private function _createScopeUri($targetUri)
  147. {
  148. $targetUriComponents = parse_url($targetUri);
  149. $scopeUri = Resources::EMPTY_STRING;
  150. $authority = Resources::EMPTY_STRING;
  151. if ($this->_containsValidAuthority($targetUriComponents)) {
  152. $authority = $this->_createAuthority($targetUriComponents);
  153. }
  154. $scopeUri = 'http://'
  155. .$authority
  156. .$targetUriComponents[Resources::PHP_URL_HOST];
  157. if (array_key_exists(Resources::PHP_URL_PATH, $targetUriComponents)) {
  158. $scopeUri .= $targetUriComponents[Resources::PHP_URL_PATH];
  159. }
  160. return $scopeUri;
  161. }
  162. /**
  163. * Gets whether the authority related elements are valid.
  164. *
  165. * @param array $uriComponents The components of an URI.
  166. *
  167. * @return boolean
  168. */
  169. private function _containsValidAuthority($uriComponents)
  170. {
  171. if (! array_key_exists(Resources::PHP_URL_USER, $uriComponents)) {
  172. return false;
  173. }
  174. if (empty($uriComponents[Resources::PHP_URL_USER])) {
  175. return false;
  176. }
  177. if (! array_key_exists(Resources::PHP_URL_PASS, $uriComponents)) {
  178. return false;
  179. }
  180. if (empty($uriComponents[Resources::PHP_URL_PASS])) {
  181. return false;
  182. }
  183. return true;
  184. }
  185. /**
  186. * Creates an authority string with specified Uri components.
  187. *
  188. * @param array $uriComponents The URI components
  189. *
  190. * @return string
  191. */
  192. private function _createAuthority($uriComponents)
  193. {
  194. $authority = sprintf(
  195. Resources::AUTHORITY_FORMAT,
  196. $uriComponents[Resources::PHP_URL_USER],
  197. $uriComponents[Resources::PHP_URL_PASS]
  198. );
  199. return $authority;
  200. }
  201. }