From b76e2ff898b23745d4c9aaee49eeb7d88f2896ab Mon Sep 17 00:00:00 2001 From: Carson Fleming Date: Thu, 2 Mar 2017 22:49:24 -0500 Subject: Updated mailgun plugin --- .../message/src/Authentication/AutoBasicAuth.php | 48 ++++++++++++++ .../message/src/Authentication/BasicAuth.php | 44 +++++++++++++ .../php-http/message/src/Authentication/Bearer.php | 37 +++++++++++ .../php-http/message/src/Authentication/Chain.php | 47 ++++++++++++++ .../message/src/Authentication/Matching.php | 74 ++++++++++++++++++++++ .../message/src/Authentication/QueryParam.php | 50 +++++++++++++++ .../src/Authentication/RequestConditional.php | 47 ++++++++++++++ .../php-http/message/src/Authentication/Wsse.php | 58 +++++++++++++++++ 8 files changed, 405 insertions(+) create mode 100644 inc/mailgun/php-http/message/src/Authentication/AutoBasicAuth.php create mode 100644 inc/mailgun/php-http/message/src/Authentication/BasicAuth.php create mode 100644 inc/mailgun/php-http/message/src/Authentication/Bearer.php create mode 100644 inc/mailgun/php-http/message/src/Authentication/Chain.php create mode 100644 inc/mailgun/php-http/message/src/Authentication/Matching.php create mode 100644 inc/mailgun/php-http/message/src/Authentication/QueryParam.php create mode 100644 inc/mailgun/php-http/message/src/Authentication/RequestConditional.php create mode 100644 inc/mailgun/php-http/message/src/Authentication/Wsse.php (limited to 'inc/mailgun/php-http/message/src/Authentication') 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 @@ + + */ +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 @@ + + */ +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 @@ + + */ +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 @@ + + */ +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 @@ + + * + * @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 @@ + + */ +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 @@ + + */ +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 @@ + + */ +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) + ; + } +} -- cgit v1.2.3