Skip to content

Commit

Permalink
[#112] Change ClientInterface constant names and HybridOauth2 class t…
Browse files Browse the repository at this point in the history
…o fit new Apigee naming (#164)

* [#112] Change ClientInterface constant names and HybridOauth2 class to fit new Apigee naming

* [#112] Change ClientInterface constant names and HybridOauth2 class to fit new Apigee naming

* Fixes phpdoc_separation error
  • Loading branch information
phdhiren authored Jul 19, 2021
1 parent 291089a commit 658c2e5
Show file tree
Hide file tree
Showing 14 changed files with 485 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Api/Management/Controller/StatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,6 @@ private function fillGapsInMetricsData(bool $tsAscending, array $allTimeUnits, a
*/
private function isHybrid(): bool
{
return ClientInterface::HYBRID_ENDPOINT === $this->getClient()->getEndpoint();
return ClientInterface::APIGEE_ON_GCP_ENDPOINT === $this->getClient()->getEndpoint();
}
}
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function __construct(
array $options = []
) {
$this->authentication = $authentication;
$this->endpoint = $endpoint ?: self::DEFAULT_ENDPOINT;
$this->endpoint = $endpoint ?: self::EDGE_ENDPOINT;
$this->resolveConfiguration($options);
}

Expand Down
22 changes: 21 additions & 1 deletion src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,37 @@ interface ClientInterface extends HttpClient
* Default endpoint for Apigee Edge Public Cloud.
*
* @var string
*
* @deprecated in 2.0.9, will be removed in 3.0.0. No longer needed.
* https://github.com/apigee/apigee-client-php/issues/112
*/
public const DEFAULT_ENDPOINT = 'https://api.enterprise.apigee.com/v1';

/**
* Default endpoint for Apigee Edge Public Cloud.
*
* @var string
*/
public const EDGE_ENDPOINT = 'https://api.enterprise.apigee.com/v1';

/**
* Default endpoint for Apigee Edge Hybrid Cloud.
*
* @var string
*
* @deprecated in 2.0.9, will be removed in 3.0.0. No longer needed.
* https://github.com/apigee/apigee-client-php/issues/112
*/
public const HYBRID_ENDPOINT = 'https://apigee.googleapis.com/v1';

public const VERSION = '2.0.8';
/**
* Default endpoint for Apigee Management API on GCP.
*
* @var string
*/
public const APIGEE_ON_GCP_ENDPOINT = 'https://apigee.googleapis.com/v1';

public const VERSION = '2.0.9';

/**
* Allows access to the last request, response and exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getEntityIds(): array
];
$uri = $this->getBaseEndpointUri()->withQuery(http_build_query($query_params));
$response = $this->getClient()->get($uri);
$expandCompatibility = (ClientInterface::HYBRID_ENDPOINT === $this->getClient()->getEndpoint());
$expandCompatibility = (ClientInterface::APIGEE_ON_GCP_ENDPOINT === $this->getClient()->getEndpoint());

return $this->responseToArray($response, $expandCompatibility);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/PaginationHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ private function listEntityIdsWithCps(PagerInterface $pager = null, array $query
$query_params = [
'expand' => 'false',
] + $query_params;
$expandCompatibility = (ClientInterface::HYBRID_ENDPOINT === $this->getClient()->getEndpoint());
$expandCompatibility = (ClientInterface::APIGEE_ON_GCP_ENDPOINT === $this->getClient()->getEndpoint());
if ($pager) {
return $this->getResultsInRange($pager, $query_params, $expandCompatibility);
} else {
Expand Down Expand Up @@ -366,7 +366,7 @@ private function listEntityIdsWithoutCps(PagerInterface $pager = null, array $qu

$uri = $this->getBaseEndpointUri()->withQuery(http_build_query($query_params));
$response = $this->getClient()->get($uri);
$expandCompatibility = (ClientInterface::HYBRID_ENDPOINT === $this->getClient()->getEndpoint());
$expandCompatibility = (ClientInterface::APIGEE_ON_GCP_ENDPOINT === $this->getClient()->getEndpoint());

$ids = $this->responseToArray($response, $expandCompatibility);

Expand Down
26 changes: 26 additions & 0 deletions src/Exception/ApigeeOnGcpOauth2AuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Exception;

/**
* For ApigeeOnGcpOauth2Authentication authentication issues.
*/
class ApigeeOnGcpOauth2AuthenticationException extends ApiException
{
}
3 changes: 3 additions & 0 deletions src/Exception/HybridOauth2AuthenticationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

/**
* For HybridOauth2Authentication authentication issues.
*
* @deprecated in 2.0.9, will be removed in 3.0.0.
* https://github.com/apigee/apigee-client-php/issues/112
*/
class HybridOauth2AuthenticationException extends ApiException
{
Expand Down
135 changes: 135 additions & 0 deletions src/HttpClient/Plugin/Authentication/ApigeeOnGcpOauth2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\HttpClient\Plugin\Authentication;

use Apigee\Edge\Client;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Exception\ApigeeOnGcpOauth2AuthenticationException;
use DomainException;
use Firebase\JWT\JWT;
use Http\Client\Exception;

/**
* ApigeeOnGcpOauth2 authentication plugin for authenticating to GCP API.
*
* @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount
*/
class ApigeeOnGcpOauth2 extends AbstractOauth
{
/**
* Authorization server for Apigee on GCP authentication.
*
* @var string
*/
public const DEFAULT_AUTHORIZATION_SERVER = 'https://oauth2.googleapis.com/token';

/**
* A space-delimited list of the permissions that the application requests.
*
* @var string
*/
public const TOKEN_SCOPES = 'https://www.googleapis.com/auth/cloud-platform';

/**
* Grant type used in an access token request.
*
* @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount
*
* @var string
*/
public const GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:jwt-bearer';

/**
* The service account email.
*
* @var string
*/
protected $email;

/**
* The service account private key.
*
* @var string
*/
protected $privateKey;

/**
* Hybrid Oauth2 constructor.
*
* @param string $email
* The service account email.
* @param string $privateKey
* The service account private key.
* @param \Apigee\Edge\HttpClient\Plugin\Authentication\OauthTokenStorageInterface $tokenStorage
* Storage where access token gets saved.
* @param string|null $authServer
* Authentication server.
*/
public function __construct(string $email, string $privateKey, OauthTokenStorageInterface $tokenStorage, ?string $authServer = null)
{
$this->email = $email;
$this->privateKey = $privateKey;
parent::__construct($tokenStorage, $authServer ?: self::DEFAULT_AUTHORIZATION_SERVER);
}

/**
* {@inheritdoc}
*/
protected function authClient(): ClientInterface
{
return new Client(new NullAuthentication(), $this->authServer);
}

/**
* {@inheritdoc}
*
* @psalm-suppress InvalidCatch - Exception by interface can be caught in PHP >= 7.1.
*/
protected function getAccessToken(): void
{
$now = time();
$token = [
'iss' => $this->email,
'aud' => $this->authServer,
'scope' => self::TOKEN_SCOPES,
'iat' => $now,
// Have the token expire in the maximum allowed time of an hour.
'exp' => $now + (60 * 60),
];

try {
$jwt = JWT::encode($token, $this->privateKey, 'RS256');
} catch (DomainException $e) {
throw new ApigeeOnGcpOauth2AuthenticationException($e->getMessage(), $e->getCode(), $e);
}

$body = [
'grant_type' => self::GRANT_TYPE,
'assertion' => $jwt,
];

try {
$response = $this->authClient()->post('', http_build_query($body), ['Content-Type' => 'application/x-www-form-urlencoded']);
$decodedResponse = json_decode((string) $response->getBody(), true);
$this->tokenStorage->saveToken($decodedResponse);
} catch (Exception $e) {
throw new ApigeeOnGcpOauth2AuthenticationException($e->getMessage(), $e->getCode(), $e);
}
}
}
4 changes: 2 additions & 2 deletions src/HttpClient/Plugin/Authentication/GceServiceAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use Apigee\Edge\Client;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Exception\HybridOauth2AuthenticationException;
use Apigee\Edge\Exception\ApigeeOnGcpOauth2AuthenticationException;
use Http\Client\Exception;
use Http\Message\Authentication\Header;

Expand Down Expand Up @@ -86,7 +86,7 @@ protected function getAccessToken(): void
$decoded_token = json_decode((string) $response->getBody(), true);
$this->tokenStorage->saveToken($decoded_token);
} catch (Exception $e) {
throw new HybridOauth2AuthenticationException($e->getMessage(), $e->getCode(), $e);
throw new ApigeeOnGcpOauth2AuthenticationException($e->getMessage(), $e->getCode(), $e);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/HttpClient/Plugin/Authentication/HybridOauth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* HybridOauth2 authentication plugin for authenticating to Hybrid Cloud API.
*
* @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount
* @deprecated in 2.0.9, will be removed in 3.0.0.
* https://github.com/apigee/apigee-client-php/issues/112
*/
class HybridOauth2 extends AbstractOauth
{
Expand Down
Loading

0 comments on commit 658c2e5

Please sign in to comment.