wp-stateless
/
lib
/
Google
/
vendor
/
google
/
auth
/
src
/
Credentials
/
ServiceAccountJwtAccessCredentials.php
ServiceAccountJwtAccessCredentials.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/Google/vendor/google/auth/src/Credentials/ServiceAccountJwtAccessCredentials.php
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright 2015 Google Inc. |
| 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\Auth\Credentials; |
| 19 | |
| 20 | use Google\Auth\CredentialsLoader; |
| 21 | use Google\Auth\OAuth2; |
| 22 | |
| 23 | /** |
| 24 | * Authenticates requests using Google's Service Account credentials via |
| 25 | * JWT Access. |
| 26 | * |
| 27 | * This class allows authorizing requests for service accounts directly |
| 28 | * from credentials from a json key file downloaded from the developer |
| 29 | * console (via 'Generate new Json Key'). It is not part of any OAuth2 |
| 30 | * flow, rather it creates a JWT and sends that as a credential. |
| 31 | */ |
| 32 | class ServiceAccountJwtAccessCredentials extends CredentialsLoader |
| 33 | { |
| 34 | |
| 35 | /** |
| 36 | * The OAuth2 instance used to conduct authorization. |
| 37 | */ |
| 38 | protected $auth; |
| 39 | |
| 40 | /** |
| 41 | * Create a new ServiceAccountJwtAccessCredentials. |
| 42 | * |
| 43 | * @param string|array $jsonKey JSON credential file path or JSON credentials |
| 44 | * as an associative array |
| 45 | */ |
| 46 | public function __construct($jsonKey) { |
| 47 | if (is_string($jsonKey)) { |
| 48 | if (!file_exists($jsonKey)) { |
| 49 | throw new \InvalidArgumentException('file does not exist'); |
| 50 | } |
| 51 | $jsonKeyStream = file_get_contents($jsonKey); |
| 52 | if (!$jsonKey = json_decode($jsonKeyStream, true)) { |
| 53 | throw new \LogicException('invalid json for auth config'); |
| 54 | } |
| 55 | } |
| 56 | if (!array_key_exists('client_email', $jsonKey)) { |
| 57 | throw new \InvalidArgumentException( |
| 58 | 'json key is missing the client_email field'); |
| 59 | } |
| 60 | if (!array_key_exists('private_key', $jsonKey)) { |
| 61 | throw new \InvalidArgumentException( |
| 62 | 'json key is missing the private_key field'); |
| 63 | } |
| 64 | $this->auth = new OAuth2([ |
| 65 | 'issuer' => $jsonKey['client_email'], |
| 66 | 'sub' => $jsonKey['client_email'], |
| 67 | 'signingAlgorithm' => 'RS256', |
| 68 | 'signingKey' => $jsonKey['private_key'], |
| 69 | ]); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Updates metadata with the authorization token |
| 74 | * |
| 75 | * @param array $metadata metadata hashmap |
| 76 | * @param string $authUri optional auth uri |
| 77 | * @param callable $httpHandler callback which delivers psr7 request |
| 78 | * |
| 79 | * @return array updated metadata hashmap |
| 80 | */ |
| 81 | public function updateMetadata( |
| 82 | $metadata, |
| 83 | $authUri = null, |
| 84 | callable $httpHandler = null |
| 85 | ) { |
| 86 | if (empty($authUri)) { |
| 87 | return $metadata; |
| 88 | } |
| 89 | |
| 90 | $this->auth->setAudience($authUri); |
| 91 | return parent::updateMetadata($metadata, $authUri, $httpHandler); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Implements FetchAuthTokenInterface#fetchAuthToken. |
| 96 | */ |
| 97 | public function fetchAuthToken(callable $httpHandler = null) |
| 98 | { |
| 99 | $audience = $this->auth->getAudience(); |
| 100 | if (empty($audience)) { |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | $access_token = $this->auth->toJwt(); |
| 105 | return array('access_token' => $access_token); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Implements FetchAuthTokenInterface#getCacheKey. |
| 110 | */ |
| 111 | public function getCacheKey() |
| 112 | { |
| 113 | return $this->auth->getCacheKey(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Implements FetchAuthTokenInterface#getLastReceivedToken. |
| 118 | */ |
| 119 | public function getLastReceivedToken() |
| 120 | { |
| 121 | return $this->auth->getLastReceivedToken(); |
| 122 | } |
| 123 | } |
| 124 |