Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
16 changes: 15 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Configuration
{
const AUTH_BASIC = 'basic';
const AUTH_DIGEST = 'digest';
const AUTH_OAUTH2 = 'oauth2';

protected $username;
protected $password;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
23 changes: 22 additions & 1 deletion src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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')) {
Expand Down