summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/Auth/AuthHandler.php
diff options
context:
space:
mode:
authorroot <root@kabir-PC>2016-03-23 11:31:51 +0100
committerroot <root@kabir-PC>2016-03-23 11:31:51 +0100
commita26989103d70fb0dd3ff6834de107cae246778c3 (patch)
tree0f243c83b790ffb57f19261fc2a509131f6776ce /server/vendor/php-opencloud/common/src/Common/Auth/AuthHandler.php
parent1342db60283cb61a1c3810993575d35b9fb33ac0 (diff)
parent6e78d76f887d1149ea85bfb06db7ee7ad7435f5a (diff)
Merge branch 'develop' of https://github.com/manzerbredes/istic-openstack into develop
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/Auth/AuthHandler.php')
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Auth/AuthHandler.php73
1 files changed, 73 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';
+ }
+}