summaryrefslogtreecommitdiff
path: root/inc/mailgun/php-http/message/src/Authentication
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2017-03-02 22:49:24 -0500
committerCarson Fleming <cflems@cflems.net>2017-03-02 22:49:24 -0500
commitb76e2ff898b23745d4c9aaee49eeb7d88f2896ab (patch)
tree9b794be8db310a575d70165d9ebde0a183b61b01 /inc/mailgun/php-http/message/src/Authentication
parentbfcc9f7a7656a2db0c905b3c13114664f00f6c37 (diff)
downloadbulletin-b76e2ff898b23745d4c9aaee49eeb7d88f2896ab.tar.gz
Updated mailgun plugin
Diffstat (limited to 'inc/mailgun/php-http/message/src/Authentication')
-rw-r--r--inc/mailgun/php-http/message/src/Authentication/AutoBasicAuth.php48
-rw-r--r--inc/mailgun/php-http/message/src/Authentication/BasicAuth.php44
-rw-r--r--inc/mailgun/php-http/message/src/Authentication/Bearer.php37
-rw-r--r--inc/mailgun/php-http/message/src/Authentication/Chain.php47
-rw-r--r--inc/mailgun/php-http/message/src/Authentication/Matching.php74
-rw-r--r--inc/mailgun/php-http/message/src/Authentication/QueryParam.php50
-rw-r--r--inc/mailgun/php-http/message/src/Authentication/RequestConditional.php47
-rw-r--r--inc/mailgun/php-http/message/src/Authentication/Wsse.php58
8 files changed, 405 insertions, 0 deletions
diff --git a/inc/mailgun/php-http/message/src/Authentication/AutoBasicAuth.php b/inc/mailgun/php-http/message/src/Authentication/AutoBasicAuth.php
new file mode 100644
index 0000000..7b6a429
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Authentication/AutoBasicAuth.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Http\Message\Authentication;
+
+use Http\Message\Authentication;
+use Psr\Http\Message\RequestInterface;
+
+/**
+ * Authenticate a PSR-7 Request using Basic Auth based on credentials in the URI.
+ *
+ * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
+ */
+final class AutoBasicAuth implements Authentication
+{
+ /**
+ * Whether user info should be removed from the URI.
+ *
+ * @var bool
+ */
+ private $shouldRemoveUserInfo;
+
+ /**
+ * @param bool|true $shouldRremoveUserInfo
+ */
+ public function __construct($shouldRremoveUserInfo = true)
+ {
+ $this->shouldRemoveUserInfo = (bool) $shouldRremoveUserInfo;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function authenticate(RequestInterface $request)
+ {
+ $uri = $request->getUri();
+ $userInfo = $uri->getUserInfo();
+
+ if (!empty($userInfo)) {
+ if ($this->shouldRemoveUserInfo) {
+ $request = $request->withUri($uri->withUserInfo(''));
+ }
+
+ $request = $request->withHeader('Authorization', sprintf('Basic %s', base64_encode($userInfo)));
+ }
+
+ return $request;
+ }
+}
diff --git a/inc/mailgun/php-http/message/src/Authentication/BasicAuth.php b/inc/mailgun/php-http/message/src/Authentication/BasicAuth.php
new file mode 100644
index 0000000..23618a5
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Authentication/BasicAuth.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Http\Message\Authentication;
+
+use Http\Message\Authentication;
+use Psr\Http\Message\RequestInterface;
+
+/**
+ * Authenticate a PSR-7 Request using Basic Auth.
+ *
+ * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
+ */
+final class BasicAuth implements Authentication
+{
+ /**
+ * @var string
+ */
+ private $username;
+
+ /**
+ * @var string
+ */
+ private $password;
+
+ /**
+ * @param string $username
+ * @param string $password
+ */
+ public function __construct($username, $password)
+ {
+ $this->username = $username;
+ $this->password = $password;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function authenticate(RequestInterface $request)
+ {
+ $header = sprintf('Basic %s', base64_encode(sprintf('%s:%s', $this->username, $this->password)));
+
+ return $request->withHeader('Authorization', $header);
+ }
+}
diff --git a/inc/mailgun/php-http/message/src/Authentication/Bearer.php b/inc/mailgun/php-http/message/src/Authentication/Bearer.php
new file mode 100644
index 0000000..a8fb21a
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Authentication/Bearer.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Http\Message\Authentication;
+
+use Http\Message\Authentication;
+use Psr\Http\Message\RequestInterface;
+
+/**
+ * Authenticate a PSR-7 Request using a token.
+ *
+ * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
+ */
+final class Bearer implements Authentication
+{
+ /**
+ * @var string
+ */
+ private $token;
+
+ /**
+ * @param string $token
+ */
+ public function __construct($token)
+ {
+ $this->token = $token;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function authenticate(RequestInterface $request)
+ {
+ $header = sprintf('Bearer %s', $this->token);
+
+ return $request->withHeader('Authorization', $header);
+ }
+}
diff --git a/inc/mailgun/php-http/message/src/Authentication/Chain.php b/inc/mailgun/php-http/message/src/Authentication/Chain.php
new file mode 100644
index 0000000..71002bb
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Authentication/Chain.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Http\Message\Authentication;
+
+use Http\Message\Authentication;
+use Psr\Http\Message\RequestInterface;
+
+/**
+ * Authenticate a PSR-7 Request with a multiple authentication methods.
+ *
+ * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
+ */
+final class Chain implements Authentication
+{
+ /**
+ * @var Authentication[]
+ */
+ private $authenticationChain = [];
+
+ /**
+ * @param Authentication[] $authenticationChain
+ */
+ public function __construct(array $authenticationChain = [])
+ {
+ foreach ($authenticationChain as $authentication) {
+ if (!$authentication instanceof Authentication) {
+ throw new \InvalidArgumentException(
+ 'Members of the authentication chain must be of type Http\Message\Authentication'
+ );
+ }
+ }
+
+ $this->authenticationChain = $authenticationChain;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function authenticate(RequestInterface $request)
+ {
+ foreach ($this->authenticationChain as $authentication) {
+ $request = $authentication->authenticate($request);
+ }
+
+ return $request;
+ }
+}
diff --git a/inc/mailgun/php-http/message/src/Authentication/Matching.php b/inc/mailgun/php-http/message/src/Authentication/Matching.php
new file mode 100644
index 0000000..4b89b50
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Authentication/Matching.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Http\Message\Authentication;
+
+use Http\Message\Authentication;
+use Http\Message\RequestMatcher\CallbackRequestMatcher;
+use Psr\Http\Message\RequestInterface;
+
+@trigger_error('The '.__NAMESPACE__.'\Matching class is deprecated since version 1.2 and will be removed in 2.0. Use Http\Message\Authentication\RequestConditional instead.', E_USER_DEPRECATED);
+
+/**
+ * Authenticate a PSR-7 Request if the request is matching.
+ *
+ * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
+ *
+ * @deprecated since since version 1.2, and will be removed in 2.0. Use {@link RequestConditional} instead.
+ */
+final class Matching implements Authentication
+{
+ /**
+ * @var Authentication
+ */
+ private $authentication;
+
+ /**
+ * @var CallbackRequestMatcher
+ */
+ private $matcher;
+
+ /**
+ * @param Authentication $authentication
+ * @param callable|null $matcher
+ */
+ public function __construct(Authentication $authentication, callable $matcher = null)
+ {
+ if (is_null($matcher)) {
+ $matcher = function () {
+ return true;
+ };
+ }
+
+ $this->authentication = $authentication;
+ $this->matcher = new CallbackRequestMatcher($matcher);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function authenticate(RequestInterface $request)
+ {
+ if ($this->matcher->matches($request)) {
+ return $this->authentication->authenticate($request);
+ }
+
+ return $request;
+ }
+
+ /**
+ * Creates a matching authentication for an URL.
+ *
+ * @param Authentication $authentication
+ * @param string $url
+ *
+ * @return self
+ */
+ public static function createUrlMatcher(Authentication $authentication, $url)
+ {
+ $matcher = function (RequestInterface $request) use ($url) {
+ return preg_match($url, $request->getRequestTarget());
+ };
+
+ return new static($authentication, $matcher);
+ }
+}
diff --git a/inc/mailgun/php-http/message/src/Authentication/QueryParam.php b/inc/mailgun/php-http/message/src/Authentication/QueryParam.php
new file mode 100644
index 0000000..14b58ff
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Authentication/QueryParam.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Http\Message\Authentication;
+
+use Http\Message\Authentication;
+use Psr\Http\Message\RequestInterface;
+
+/**
+ * Authenticate a PSR-7 Request by adding parameters to its query.
+ *
+ * Note: Although in some cases it can be useful, we do not recommend using query parameters for authentication.
+ * Credentials in the URL is generally unsafe as they are not encrypted, anyone can see them.
+ *
+ * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
+ */
+final class QueryParam implements Authentication
+{
+ /**
+ * @var array
+ */
+ private $params = [];
+
+ /**
+ * @param array $params
+ */
+ public function __construct(array $params)
+ {
+ $this->params = $params;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function authenticate(RequestInterface $request)
+ {
+ $uri = $request->getUri();
+ $query = $uri->getQuery();
+ $params = [];
+
+ parse_str($query, $params);
+
+ $params = array_merge($params, $this->params);
+
+ $query = http_build_query($params);
+
+ $uri = $uri->withQuery($query);
+
+ return $request->withUri($uri);
+ }
+}
diff --git a/inc/mailgun/php-http/message/src/Authentication/RequestConditional.php b/inc/mailgun/php-http/message/src/Authentication/RequestConditional.php
new file mode 100644
index 0000000..5477440
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Authentication/RequestConditional.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Http\Message\Authentication;
+
+use Http\Message\Authentication;
+use Http\Message\RequestMatcher;
+use Psr\Http\Message\RequestInterface;
+
+/**
+ * Authenticate a PSR-7 Request if the request is matching the given request matcher.
+ *
+ * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
+ */
+final class RequestConditional implements Authentication
+{
+ /**
+ * @var RequestMatcher
+ */
+ private $requestMatcher;
+
+ /**
+ * @var Authentication
+ */
+ private $authentication;
+
+ /**
+ * @param RequestMatcher $requestMatcher
+ * @param Authentication $authentication
+ */
+ public function __construct(RequestMatcher $requestMatcher, Authentication $authentication)
+ {
+ $this->requestMatcher = $requestMatcher;
+ $this->authentication = $authentication;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function authenticate(RequestInterface $request)
+ {
+ if ($this->requestMatcher->matches($request)) {
+ return $this->authentication->authenticate($request);
+ }
+
+ return $request;
+ }
+}
diff --git a/inc/mailgun/php-http/message/src/Authentication/Wsse.php b/inc/mailgun/php-http/message/src/Authentication/Wsse.php
new file mode 100644
index 0000000..fbbde33
--- /dev/null
+++ b/inc/mailgun/php-http/message/src/Authentication/Wsse.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace Http\Message\Authentication;
+
+use Http\Message\Authentication;
+use Psr\Http\Message\RequestInterface;
+
+/**
+ * Authenticate a PSR-7 Request using WSSE.
+ *
+ * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
+ */
+final class Wsse implements Authentication
+{
+ /**
+ * @var string
+ */
+ private $username;
+
+ /**
+ * @var string
+ */
+ private $password;
+
+ /**
+ * @param string $username
+ * @param string $password
+ */
+ public function __construct($username, $password)
+ {
+ $this->username = $username;
+ $this->password = $password;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function authenticate(RequestInterface $request)
+ {
+ // TODO: generate better nonce?
+ $nonce = substr(md5(uniqid(uniqid().'_', true)), 0, 16);
+ $created = date('c');
+ $digest = base64_encode(sha1(base64_decode($nonce).$created.$this->password, true));
+
+ $wsse = sprintf(
+ 'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
+ $this->username,
+ $digest,
+ $nonce,
+ $created
+ );
+
+ return $request
+ ->withHeader('Authorization', 'WSSE profile="UsernameToken"')
+ ->withHeader('X-WSSE', $wsse)
+ ;
+ }
+}