From 259d9cf98a232d1f75a5f12bceed1e22a2bed643 Mon Sep 17 00:00:00 2001 From: jbramleycl Date: Thu, 1 Aug 2019 13:42:27 -0400 Subject: [PATCH 1/2] Added OAuth2 Client Credentials support. --- src/Configuration.php | 16 +++++++++++++++- src/Session.php | 23 ++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index b4ab312..af71c4b 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -9,6 +9,7 @@ class Configuration { const AUTH_BASIC = 'basic'; const AUTH_DIGEST = 'digest'; + const AUTH_OAUTH2 = 'oauth2'; protected $username; protected $password; @@ -240,7 +241,7 @@ public function userAgentDigestHash(Session $session) */ public function setHttpAuthenticationMethod($auth_method) { - if (!in_array($auth_method, [self::AUTH_BASIC, self::AUTH_DIGEST])) { + if (!in_array($auth_method, [self::AUTH_BASIC, self::AUTH_DIGEST, self::AUTH_OAUTH2])) { throw new \InvalidArgumentException("Given authentication method is invalid. Must be 'basic' or 'digest'"); } $this->http_authentication = $auth_method; @@ -254,4 +255,17 @@ public function getHttpAuthenticationMethod() { return $this->http_authentication; } + + /** + * @param $base_uri + * @param $scope + * @return $this + */ + public function setOauth2Authentication($base_uri, $scope) + { + $this->setHttpAuthenticationMethod(self::AUTH_OAUTH2); + $this->setOption('oauth2_base_uri', $base_uri); + $this->setOption('oauth2_scope', $scope); + return $this; + } } diff --git a/src/Session.php b/src/Session.php index a99d319..c883245 100644 --- a/src/Session.php +++ b/src/Session.php @@ -4,8 +4,11 @@ use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Cookie\CookieJarInterface; use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\HandlerStack; use Illuminate\Container\Container; use Illuminate\Support\Collection; +use kamermans\OAuth2\GrantType\ClientCredentials; +use kamermans\OAuth2\OAuth2Middleware; use PHRETS\Exceptions\CapabilityUnavailable; use PHRETS\Exceptions\MetadataNotFound; use PHRETS\Exceptions\MissingConfiguration; @@ -42,7 +45,22 @@ public function __construct(Configuration $configuration) $defaults = []; // start up our Guzzle HTTP client - $this->client = PHRETSClient::make($defaults); + if ($this->configuration->getHttpAuthenticationMethod() == Configuration::AUTH_OAUTH2) { + $reauth_client = new Client(['base_uri' => $this->configuration->readOption('oauth2_base_uri')]); + $reauth_config = [ + 'client_id' => $this->configuration->getUsername(), + 'client_secret' => $this->configuration->getPassword(), + 'scope' => $this->configuration->readOption('oauth2_scope') + ]; + $grant_type = new ClientCredentials($reauth_client, $reauth_config); + $oauth = new OAuth2Middleware($grant_type); + $stack = HandlerStack::create(); + $stack->push($oauth); + + $this->client = PHRETSClient::make(['auth' => 'oauth', 'handler' => $stack]); + } else { + $this->client = PHRETSClient::make($defaults); + } $this->cookie_jar = new CookieJar; @@ -545,6 +563,9 @@ public function getDefaultOptions() ], 'curl' => [ CURLOPT_COOKIEFILE => tempnam('/tmp', 'phrets') ] ]; + if ($this->configuration->getHttpAuthenticationMethod() == Configuration::AUTH_OAUTH2) { + $defaults['auth'] = 'oauth'; + } // disable following 'Location' header (redirects) automatically if ($this->configuration->readOption('disable_follow_location')) { From 47e5f54ec64fd6d6b2009ac9a552697114e9cbe4 Mon Sep 17 00:00:00 2001 From: jbramleycl Date: Thu, 1 Aug 2019 13:45:04 -0400 Subject: [PATCH 2/2] Updated composer for OAuth2 support plugin --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3a4f87a..fc7b9cc 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "guzzlehttp/guzzle": ">=6.0", "illuminate/container": ">=4.2.0", "illuminate/support": ">=4.2.0", - "league/csv": ">=6.0" + "league/csv": ">=6.0", + "kamermans/guzzle-oauth2-subscriber": "~1.0" }, "require-dev": { "phpunit/phpunit": "5.*",