Skip to content

Commit

Permalink
First pass at oauth 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
eporama authored and Adam Malone committed Nov 24, 2019
1 parent b1dd447 commit aed86d2
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 119 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
],
"license": "MIT",
"require": {
"acquia/http-hmac-php": "^3.1",
"guzzlehttp/guzzle": "^6.3",
"php": ">=5.6"
"php": ">=5.6",
"league/oauth2-client": "^2.4"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.0.0",
Expand Down
218 changes: 112 additions & 106 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 29 additions & 11 deletions src/CloudApi/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace AcquiaCloudApi\CloudApi;

use Acquia\Hmac\Guzzle\HmacAuthMiddleware;
use Acquia\Hmac\Key;
use League\OAuth2\Client\Provider\GenericProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use GuzzleHttp\HandlerStack;

/**
* Class Connector
Expand All @@ -21,6 +20,16 @@ class Connector implements ConnectorInterface
*/
const BASE_URI = 'https://cloud.acquia.com/api';

/**
* @var GenericProvider The OAuth 2.0 provider to use in communication.
*/
protected $provider;

/**
* @var string The generated OAuth 2.0 access token.
*/
protected $accessToken;

/**
* @var GuzzleClient The Guzzle Client to communicate with the API.
*/
Expand All @@ -38,13 +47,12 @@ class Connector implements ConnectorInterface
*/
public function __construct($config)
{
$key = new Key($config['key'], $config['secret']);
$middleware = new HmacAuthMiddleware($key);
$stack = HandlerStack::create();
$stack->push($middleware);

$this->client = new GuzzleClient([
'handler' => $stack,
$this->provider = new GenericProvider([
'clientId' => $config['key'],
'clientSecret' => $config['secret'],
'urlAuthorize' => '',
'urlAccessToken' => 'https://accounts.acquia.com/api/auth/oauth/token',
'urlResourceOwnerDetails' => '',
]);
}

Expand Down Expand Up @@ -82,8 +90,18 @@ public function request(string $verb, string $path, array $query = [], array $op
*/
public function makeRequest(string $verb, string $path, array $query = [], array $options = [])
{
if (! isset($this->accessToken)) {
$this->accessToken = $this->provider->getAccessToken('client_credentials');
}

try {
$response = $this->client->$verb(self::BASE_URI . $path, $options);
$request = $this->provider->getAuthenticatedRequest(
$verb,
self::BASE_URI . $path,
$this->accessToken
);
$client = new GuzzleClient();
$response = $client->send($request, $options);
} catch (ClientException $e) {
print $e->getMessage();
$response = $e->getResponse();
Expand Down

0 comments on commit aed86d2

Please sign in to comment.