|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2018 Google LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace Apigee\Edge\Api\Management\Controller; |
| 20 | + |
| 21 | +use Apigee\Edge\Api\Management\Entity\AppCredential; |
| 22 | +use Apigee\Edge\Api\Management\Entity\AppCredentialInterface; |
| 23 | +use Apigee\Edge\Api\Management\Normalizer\AppCredentialNormalizer; |
| 24 | +use Apigee\Edge\Controller\EntityController; |
| 25 | +use Apigee\Edge\Controller\EntityCrudOperationsControllerTrait; |
| 26 | +use Apigee\Edge\Controller\StatusAwareEntityControllerTrait; |
| 27 | +use Apigee\Edge\Denormalizer\AttributesPropertyDenormalizer; |
| 28 | +use Apigee\Edge\Denormalizer\CredentialProductDenormalizer; |
| 29 | +use Apigee\Edge\Entity\EntityInterface; |
| 30 | +use Apigee\Edge\Normalizer\CredentialProductNormalizer; |
| 31 | +use Apigee\Edge\Structure\AttributesProperty; |
| 32 | + |
| 33 | +/** |
| 34 | + * Common implementation for company- and developer app credential controllers. |
| 35 | + */ |
| 36 | +abstract class AppCredentialController extends EntityController implements AppCredentialControllerInterface |
| 37 | +{ |
| 38 | + use EntityCrudOperationsControllerTrait { |
| 39 | + // These methods are not supported on this endpoint in the same way as on the others so do not allow to |
| 40 | + // use them here. |
| 41 | + create as private privateCreate; |
| 42 | + update as private privateUpdate; |
| 43 | + } |
| 44 | + use StatusAwareEntityControllerTrait; |
| 45 | + |
| 46 | + /** |
| 47 | + * String that should be sent to the API to change the status of a credential to approved. |
| 48 | + */ |
| 49 | + public const STATUS_APPROVE = 'approve'; |
| 50 | + |
| 51 | + /** |
| 52 | + * String that should be sent to the API to change the status of a credential to revoked. |
| 53 | + */ |
| 54 | + public const STATUS_REVOKE = 'revoke'; |
| 55 | + |
| 56 | + /** @var string Developer email or id. */ |
| 57 | + protected $companyName; |
| 58 | + |
| 59 | + /** @var string App name. */ |
| 60 | + protected $appName; |
| 61 | + |
| 62 | + public function __construct(string $organization, string $appName, \Apigee\Edge\ClientInterface $client, $entityNormalizers = []) |
| 63 | + { |
| 64 | + $this->appName = $appName; |
| 65 | + $entityNormalizers[] = new AppCredentialNormalizer(); |
| 66 | + $entityNormalizers[] = new CredentialProductDenormalizer(); |
| 67 | + $entityNormalizers[] = new CredentialProductNormalizer(); |
| 68 | + $entityNormalizers[] = new AttributesPropertyDenormalizer(); |
| 69 | + parent::__construct($organization, $client, $entityNormalizers); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @inheritdoc |
| 74 | + */ |
| 75 | + public function create(string $consumerKey, string $consumerSecret): AppCredentialInterface |
| 76 | + { |
| 77 | + $response = $this->client->post( |
| 78 | + // Just to spare some extra lines of code. |
| 79 | + $this->getEntityEndpointUri('create'), |
| 80 | + (string) json_encode((object) ['consumerKey' => $consumerKey, 'consumerSecret' => $consumerSecret]) |
| 81 | + ); |
| 82 | + |
| 83 | + return $this->entityTransformer->deserialize( |
| 84 | + (string) $response->getBody(), |
| 85 | + $this->getEntityClass(), |
| 86 | + 'json' |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @inheritdoc |
| 92 | + */ |
| 93 | + public function generate( |
| 94 | + array $apiProducts, |
| 95 | + array $scopes = [], |
| 96 | + string $keyExpiresIn = '-1' |
| 97 | + ): AppCredentialInterface { |
| 98 | + $response = $this->client->post( |
| 99 | + $this->getBaseEndpointUri(), |
| 100 | + (string) json_encode((object) [ |
| 101 | + 'apiProducts' => $apiProducts, |
| 102 | + 'scopes' => $scopes, |
| 103 | + 'keyExpiresIn' => $keyExpiresIn, |
| 104 | + ]) |
| 105 | + ); |
| 106 | + // It returns a complete developer app entity, but we only returns the newly created credential for the |
| 107 | + // sake of consistency. |
| 108 | + $responseArray = $this->responseToArray($response); |
| 109 | + $credentialArray = reset($responseArray['credentials']); |
| 110 | + |
| 111 | + return $this->entityTransformer->denormalize( |
| 112 | + $credentialArray, |
| 113 | + $this->getEntityClass() |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @inheritdoc |
| 119 | + */ |
| 120 | + public function addProducts(string $consumerKey, array $apiProducts): AppCredentialInterface |
| 121 | + { |
| 122 | + $response = $this->client->post( |
| 123 | + $this->getEntityEndpointUri($consumerKey), |
| 124 | + (string) json_encode((object) ['apiProducts' => $apiProducts]) |
| 125 | + ); |
| 126 | + |
| 127 | + return $this->entityTransformer->deserialize( |
| 128 | + (string) $response->getBody(), |
| 129 | + $this->getEntityClass(), |
| 130 | + 'json' |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @inheritdoc |
| 136 | + */ |
| 137 | + public function overrideAttributes(string $consumerKey, AttributesProperty $attributes): AppCredentialInterface |
| 138 | + { |
| 139 | + $response = $this->client->post( |
| 140 | + $this->getEntityEndpointUri($consumerKey), |
| 141 | + (string) json_encode((object) ['attributes' => $this->entityTransformer->normalize($attributes)]) |
| 142 | + ); |
| 143 | + |
| 144 | + return $this->entityTransformer->deserialize( |
| 145 | + (string) $response->getBody(), |
| 146 | + $this->getEntityClass(), |
| 147 | + 'json' |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @inheritdoc |
| 153 | + */ |
| 154 | + public function setApiProductStatus(string $consumerKey, string $apiProduct, string $status): void |
| 155 | + { |
| 156 | + $uri = $this->getBaseEndpointUri() |
| 157 | + ->withPath(sprintf('%s/keys/%s/apiproducts/%s', $this->getBaseEndpointUri(), $consumerKey, $apiProduct)) |
| 158 | + ->withQuery(http_build_query(['action' => $status])); |
| 159 | + $this->client->post($uri, null, ['Content-Type' => 'application/octet-stream']); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @inheritdoc |
| 164 | + */ |
| 165 | + public function deleteApiProduct(string $consumerKey, string $apiProduct): EntityInterface |
| 166 | + { |
| 167 | + $uri = $this->getBaseEndpointUri() |
| 168 | + ->withPath(sprintf('%s/keys/%s/apiproducts/%s', $this->getBaseEndpointUri(), $consumerKey, $apiProduct)); |
| 169 | + $response = $this->client->delete($uri); |
| 170 | + |
| 171 | + return $this->entityTransformer->deserialize( |
| 172 | + (string) $response->getBody(), |
| 173 | + $this->getEntityClass(), |
| 174 | + 'json' |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @inheritdoc |
| 180 | + */ |
| 181 | + public function overrideScopes(string $consumerKey, array $scopes): AppCredentialInterface |
| 182 | + { |
| 183 | + $response = $this->client->put( |
| 184 | + $this->getEntityEndpointUri($consumerKey), |
| 185 | + (string) json_encode((object) ['scopes' => $scopes]) |
| 186 | + ); |
| 187 | + |
| 188 | + return $this->entityTransformer->deserialize( |
| 189 | + (string) $response->getBody(), |
| 190 | + $this->getEntityClass(), |
| 191 | + 'json' |
| 192 | + ); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @inheritdoc |
| 197 | + */ |
| 198 | + protected function getEntityClass(): string |
| 199 | + { |
| 200 | + return AppCredential::class; |
| 201 | + } |
| 202 | +} |
0 commit comments