wp-stateless
/
lib
/
Google
/
vendor
/
google
/
auth
/
src
/
Credentials
/
UserRefreshCredentials.php
UserRefreshCredentials.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/Google/vendor/google/auth/src/Credentials/UserRefreshCredentials.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 User Refresh credentials. |
| 25 | * |
| 26 | * This class allows authorizing requests from user refresh tokens. |
| 27 | * |
| 28 | * This the end of the result of a 3LO flow. E.g, the end result of |
| 29 | * 'gcloud auth login' saves a file with these contents in well known |
| 30 | * location |
| 31 | * |
| 32 | * cf [Application Default Credentials](http://goo.gl/mkAHpZ) |
| 33 | */ |
| 34 | class UserRefreshCredentials extends CredentialsLoader |
| 35 | { |
| 36 | /** |
| 37 | * The OAuth2 instance used to conduct authorization. |
| 38 | */ |
| 39 | protected $auth; |
| 40 | |
| 41 | /** |
| 42 | * Create a new UserRefreshCredentials. |
| 43 | * |
| 44 | * @param string|array $scope the scope of the access request, expressed |
| 45 | * either as an Array or as a space-delimited String. |
| 46 | * |
| 47 | * @param string|array $jsonKey JSON credential file path or JSON credentials |
| 48 | * as an associative array |
| 49 | */ |
| 50 | public function __construct( |
| 51 | $scope, |
| 52 | $jsonKey |
| 53 | ) { |
| 54 | if (is_string($jsonKey)) { |
| 55 | if (!file_exists($jsonKey)) { |
| 56 | throw new \InvalidArgumentException('file does not exist'); |
| 57 | } |
| 58 | $jsonKeyStream = file_get_contents($jsonKey); |
| 59 | if (!$jsonKey = json_decode($jsonKeyStream, true)) { |
| 60 | throw new \LogicException('invalid json for auth config'); |
| 61 | } |
| 62 | } |
| 63 | if (!array_key_exists('client_id', $jsonKey)) { |
| 64 | throw new \InvalidArgumentException( |
| 65 | 'json key is missing the client_id field'); |
| 66 | } |
| 67 | if (!array_key_exists('client_secret', $jsonKey)) { |
| 68 | throw new \InvalidArgumentException( |
| 69 | 'json key is missing the client_secret field'); |
| 70 | } |
| 71 | if (!array_key_exists('refresh_token', $jsonKey)) { |
| 72 | throw new \InvalidArgumentException( |
| 73 | 'json key is missing the refresh_token field'); |
| 74 | } |
| 75 | $this->auth = new OAuth2([ |
| 76 | 'clientId' => $jsonKey['client_id'], |
| 77 | 'clientSecret' => $jsonKey['client_secret'], |
| 78 | 'refresh_token' => $jsonKey['refresh_token'], |
| 79 | 'scope' => $scope, |
| 80 | 'tokenCredentialUri' => self::TOKEN_CREDENTIAL_URI |
| 81 | ]); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Implements FetchAuthTokenInterface#fetchAuthToken. |
| 86 | */ |
| 87 | public function fetchAuthToken(callable $httpHandler = null) |
| 88 | { |
| 89 | return $this->auth->fetchAuthToken($httpHandler); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Implements FetchAuthTokenInterface#getCacheKey. |
| 94 | */ |
| 95 | public function getCacheKey() |
| 96 | { |
| 97 | return $this->auth->getClientId() . ':' . $this->auth->getCacheKey(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Implements FetchAuthTokenInterface#getLastReceivedToken. |
| 102 | */ |
| 103 | public function getLastReceivedToken() |
| 104 | { |
| 105 | return $this->auth->getLastReceivedToken(); |
| 106 | } |
| 107 | } |
| 108 |