diff options
| author | EoleDev <root@serverpc.home> | 2016-03-09 16:17:33 +0100 |
|---|---|---|
| committer | EoleDev <root@serverpc.home> | 2016-03-09 16:17:33 +0100 |
| commit | 2b8decb81faeb7928bcbfda84c6f33a003f707fd (patch) | |
| tree | 0e491d7ae2bf91347b1cf50d2c475625d8f659b4 /server/vendor/php-opencloud/common/src/Common/Auth | |
| parent | b7ebe1272c1127df290535af2430622b28160bb0 (diff) | |
| parent | 03ef74d0cfe675a6e18a91f039182ca1b248d8f5 (diff) | |
Maj Library
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/Auth')
4 files changed, 121 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/src/Common/Auth/AuthHandler.php b/server/vendor/php-opencloud/common/src/Common/Auth/AuthHandler.php new file mode 100644 index 0000000..1a36cc0 --- /dev/null +++ b/server/vendor/php-opencloud/common/src/Common/Auth/AuthHandler.php @@ -0,0 +1,73 @@ +<?php + +namespace OpenCloud\Common\Auth; + +use function GuzzleHttp\Psr7\modify_request; +use Psr\Http\Message\RequestInterface; + +/** + * This class is responsible for three tasks: + * + * 1. performing the initial authentication for OpenCloud services + * 2. populating the ``X-Auth-Token`` header for every HTTP request + * 3. checking the token expiry before each request, and re-authenticating if necessary + */ +class AuthHandler +{ + private $nextHandler; + + private $tokenGenerator; + + private $token; + + /** + * @param callable $nextHandler + * @param callable $tokenGenerator + */ + public function __construct(callable $nextHandler, callable $tokenGenerator, Token $token = null) + { + $this->nextHandler = $nextHandler; + $this->tokenGenerator = $tokenGenerator; + $this->token = $token; + } + + /** + * This method is invoked before every HTTP request is sent to the API. When this happens, it + * checks to see whether a token is set and valid, and then sets the ``X-Auth-Token`` header + * for the HTTP request before letting it continue on its merry way. + * + * @param RequestInterface $request + * @param array $options + * + * @return mixed|void + */ + public function __invoke(RequestInterface $request, array $options) + { + $fn = $this->nextHandler; + + if ($this->shouldIgnore($request)) { + return $fn($request, $options); + } + + if (!$this->token || $this->token->hasExpired()) { + $this->token = call_user_func($this->tokenGenerator); + } + + $modify = ['set_headers' => ['X-Auth-Token' => $this->token->getId()]]; + + return $fn(modify_request($request, $modify), $options); + } + + /** + * Internal method which prevents infinite recursion. For certain requests, like the initial + * auth call itself, we do NOT want to send a token. + * + * @param RequestInterface $request + * + * @return bool + */ + private function shouldIgnore(RequestInterface $request) + { + return strpos((string) $request->getUri(), 'tokens') !== false && $request->getMethod() == 'POST'; + } +} diff --git a/server/vendor/php-opencloud/common/src/Common/Auth/Catalog.php b/server/vendor/php-opencloud/common/src/Common/Auth/Catalog.php new file mode 100644 index 0000000..b4ab381 --- /dev/null +++ b/server/vendor/php-opencloud/common/src/Common/Auth/Catalog.php @@ -0,0 +1,20 @@ +<?php + +namespace OpenCloud\Common\Auth; + +interface Catalog +{ + /** + * Attempts to retrieve the base URL for a service from the catalog according to the arguments provided. + * + * @param string $name The name of the service as it appears in the catalog + * @param string $type The type of the service as it appears in the catalog + * @param string $region The region of the service as it appears in the catalog + * @param string $urlType The URL type of the service as it appears in the catalog + * + * @throws \RuntimeException If no endpoint is matched + * + * @returns string + */ + public function getServiceUrl($name, $type, $region, $urlType); +} diff --git a/server/vendor/php-opencloud/common/src/Common/Auth/IdentityService.php b/server/vendor/php-opencloud/common/src/Common/Auth/IdentityService.php new file mode 100644 index 0000000..695e7b1 --- /dev/null +++ b/server/vendor/php-opencloud/common/src/Common/Auth/IdentityService.php @@ -0,0 +1,13 @@ +<?php + +namespace OpenCloud\Common\Auth; + +interface IdentityService +{ + /** + * Authenticates and retrieves back a token and catalog. + * + * @return array The FIRST key is {@see Token} instance, the SECOND key is a {@see Catalog} instance + */ + public function authenticate(array $options); +} diff --git a/server/vendor/php-opencloud/common/src/Common/Auth/Token.php b/server/vendor/php-opencloud/common/src/Common/Auth/Token.php new file mode 100644 index 0000000..200fcee --- /dev/null +++ b/server/vendor/php-opencloud/common/src/Common/Auth/Token.php @@ -0,0 +1,15 @@ +<?php + +namespace OpenCloud\Common\Auth; + +interface Token +{ + public function getId(); + + /** + * Indicates whether the token has expired or not. + * + * @return bool TRUE if the token has expired, FALSE if it is still valid + */ + public function hasExpired(); +} |
