EdgeKeyTypeBase.php in Apigee Edge 8
File
src/Plugin/EdgeKeyTypeBase.php
View source
<?php
namespace Drupal\apigee_edge\Plugin;
use Apigee\Edge\Client;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\HttpClient\Plugin\Authentication\Oauth;
use Drupal\apigee_edge\Exception\AuthenticationKeyValueMalformedException;
use Drupal\Component\Serialization\Json;
use Drupal\key\KeyInterface;
use Drupal\key\Plugin\KeyTypeBase;
abstract class EdgeKeyTypeBase extends KeyTypeBase implements EdgeKeyTypeInterface {
public function serialize(array $array) {
return Json::encode($array);
}
public function unserialize($value) {
return Json::decode($value);
}
public function getAuthenticationType(KeyInterface $key) : string {
if ($this
->getInstanceType($key) === EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID) {
if ($this
->useGcpDefaultServiceAccount($key)) {
return EdgeKeyTypeInterface::EDGE_AUTH_TYPE_DEFAULT_GCE_SERVICE_ACCOUNT;
}
else {
return EdgeKeyTypeInterface::EDGE_AUTH_TYPE_JWT;
}
}
if (!isset($key
->getKeyValues()['auth_type'])) {
throw new AuthenticationKeyValueMalformedException('auth_type');
}
return $key
->getKeyValues()['auth_type'];
}
public function getEndpoint(KeyInterface $key) : string {
if ($this
->getInstanceType($key) === EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID) {
return ClientInterface::APIGEE_ON_GCP_ENDPOINT;
}
elseif ($this
->getInstanceType($key) === EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC) {
return Client::EDGE_ENDPOINT;
}
return $key
->getKeyValues()['endpoint'];
}
public function getEndpointType(KeyInterface $key) : string {
if ($this
->getInstanceType($key) === EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC) {
return EdgeKeyTypeInterface::EDGE_ENDPOINT_TYPE_DEFAULT;
}
return EdgeKeyTypeInterface::EDGE_ENDPOINT_TYPE_CUSTOM;
}
public function getInstanceType(KeyInterface $key) : string {
$key_values = $key
->getKeyValues();
if (isset($key_values['instance_type'])) {
return $key_values['instance_type'];
}
if (empty($key_values['endpoint']) || $key_values['endpoint'] === ClientInterface::EDGE_ENDPOINT) {
return EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC;
}
return EdgeKeyTypeInterface::INSTANCE_TYPE_PRIVATE;
}
public function getOrganization(KeyInterface $key) : string {
if (!isset($key
->getKeyValues()['organization'])) {
throw new AuthenticationKeyValueMalformedException('organization');
}
return $key
->getKeyValues()['organization'];
}
public function getUsername(KeyInterface $key) : string {
if (!isset($key
->getKeyValues()['username'])) {
throw new AuthenticationKeyValueMalformedException('username');
}
return $key
->getKeyValues()['username'];
}
public function getPassword(KeyInterface $key) : string {
if (!isset($key
->getKeyValues()['password'])) {
throw new AuthenticationKeyValueMalformedException('password');
}
return $key
->getKeyValues()['password'];
}
public function getAuthorizationServer(KeyInterface $key) : string {
return $key
->getKeyValues()['authorization_server'] ?? Oauth::DEFAULT_AUTHORIZATION_SERVER;
}
public function getClientId(KeyInterface $key) : string {
return $key
->getKeyValues()['client_id'] ?? Oauth::DEFAULT_CLIENT_ID;
}
public function getClientSecret(KeyInterface $key) : string {
return $key
->getKeyValues()['client_secret'] ?? Oauth::DEFAULT_CLIENT_SECRET;
}
public function getAccountKey(KeyInterface $key) : array {
$value = $key
->getKeyValues()['account_json_key'] ?? '';
$json = json_decode($value, TRUE);
if (empty($json['private_key']) || empty($json['client_email'])) {
throw new AuthenticationKeyValueMalformedException('account_json_key');
}
return $json;
}
public function useGcpDefaultServiceAccount(KeyInterface $key) : bool {
return !empty($key
->getKeyValues()['gcp_hosted']);
}
}