| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright 2019 Google LLC |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
namespace Google\Cloud\Storage; |
| 19 |
|
| 20 |
use Google\Cloud\Storage\Connection\ConnectionInterface; |
| 21 |
|
| 22 |
/** |
| 23 |
* Represents a Service Account HMAC key. |
| 24 |
* |
| 25 |
* Example: |
| 26 |
* ``` |
| 27 |
* use Google\Cloud\Storage\StorageClient; |
| 28 |
* |
| 29 |
* $storage = new StorageClient(); |
| 30 |
* $hmacKey = $storage->hmacKey($accessId); |
| 31 |
* ``` |
| 32 |
*/ |
| 33 |
class HmacKey |
| 34 |
{ |
| 35 |
/** |
| 36 |
* @var ConnectionInterface |
| 37 |
*/ |
| 38 |
private $connection; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
private $projectId; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
private $accessId; |
| 49 |
|
| 50 |
/** |
| 51 |
* @var array|null |
| 52 |
*/ |
| 53 |
private $info; |
| 54 |
|
| 55 |
/** |
| 56 |
* @param ConnectionInterface $connection A connection to Cloud Storage. |
| 57 |
* @param string $projectId The current project ID. |
| 58 |
* @param string $accessId The key identifier. |
| 59 |
* @param array|null $info The key metadata. |
| 60 |
*/ |
| 61 |
public function __construct( |
| 62 |
ConnectionInterface $connection, |
| 63 |
$projectId, |
| 64 |
$accessId, |
| 65 |
array $info = [] |
| 66 |
) { |
| 67 |
$this->connection = $connection; |
| 68 |
$this->projectId = $projectId; |
| 69 |
$this->accessId = $accessId; |
| 70 |
$this->info = $info; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the HMAC Key Access ID. |
| 75 |
* |
| 76 |
* Example: |
| 77 |
* ``` |
| 78 |
* $accessId = $hmacKey->accessId(); |
| 79 |
* ``` |
| 80 |
* |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public function accessId() |
| 84 |
{ |
| 85 |
return $this->accessId; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Fetch the key metadata from Cloud Storage. |
| 90 |
* |
| 91 |
* Example: |
| 92 |
* ``` |
| 93 |
* $keyMetadata = $hmacKey->reload(); |
| 94 |
* ``` |
| 95 |
* |
| 96 |
* @param array $options { |
| 97 |
* Configuration Options |
| 98 |
* |
| 99 |
* @type string $userProject If set, this is the ID of the project which |
| 100 |
* will be billed for the request. **NOTE**: This option is |
| 101 |
* currently ignored by Cloud Storage. |
| 102 |
* } |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
public function reload(array $options = []) |
| 106 |
{ |
| 107 |
$this->info = $this->connection->getHmacKey([ |
| 108 |
'projectId' => $this->projectId, |
| 109 |
'accessId' => $this->accessId |
| 110 |
] + $options); |
| 111 |
|
| 112 |
return $this->info; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get the HMAC Key Metadata. |
| 117 |
* |
| 118 |
* If the metadata is not already available, it will be requested from Cloud |
| 119 |
* Storage. |
| 120 |
* |
| 121 |
* Example: |
| 122 |
* ``` |
| 123 |
* $keyMetadata = $hmacKey->info(); |
| 124 |
* ``` |
| 125 |
* |
| 126 |
* @param array $options { |
| 127 |
* Configuration Options |
| 128 |
* |
| 129 |
* @type string $userProject If set, this is the ID of the project which |
| 130 |
* will be billed for the request. **NOTE**: This option is |
| 131 |
* currently ignored by Cloud Storage. |
| 132 |
* } |
| 133 |
* @return array |
| 134 |
*/ |
| 135 |
public function info(array $options = []) |
| 136 |
{ |
| 137 |
return $this->info ?: $this->reload($options); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Update the HMAC Key state. |
| 142 |
* |
| 143 |
* Example: |
| 144 |
* ``` |
| 145 |
* $hmacKey->update('INACTIVE'); |
| 146 |
* ``` |
| 147 |
* |
| 148 |
* @param string $state The key state. Either `ACTIVE` or `INACTIVE`. |
| 149 |
* @param array $options { |
| 150 |
* Configuration Options |
| 151 |
* |
| 152 |
* @type string $userProject If set, this is the ID of the project which |
| 153 |
* will be billed for the request. **NOTE**: This option is |
| 154 |
* currently ignored by Cloud Storage. |
| 155 |
* } |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
public function update($state, array $options = []) |
| 159 |
{ |
| 160 |
$this->info = $this->connection->updateHmacKey([ |
| 161 |
'accessId' => $this->accessId, |
| 162 |
'projectId' => $this->projectId, |
| 163 |
'state' => $state |
| 164 |
] + $options); |
| 165 |
|
| 166 |
return $this->info; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Delete the HMAC Key. |
| 171 |
* |
| 172 |
* Key state must be set to `INACTIVE` prior to deletion. See |
| 173 |
* {@see Google\Cloud\Storage\HmacKey::update()} for details. |
| 174 |
* |
| 175 |
* Example: |
| 176 |
* ``` |
| 177 |
* $hmacKey->delete(); |
| 178 |
* ``` |
| 179 |
* |
| 180 |
* @param array $options { |
| 181 |
* Configuration Options |
| 182 |
* |
| 183 |
* @type string $userProject If set, this is the ID of the project which |
| 184 |
* will be billed for the request. **NOTE**: This option is |
| 185 |
* currently ignored by Cloud Storage. |
| 186 |
* } |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
public function delete(array $options = []) |
| 190 |
{ |
| 191 |
$this->connection->deleteHmacKey([ |
| 192 |
'accessId' => $this->accessId, |
| 193 |
'projectId' => $this->projectId, |
| 194 |
] + $options); |
| 195 |
} |
| 196 |
} |
| 197 |
|