PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.3
WP-Stateless – Google Cloud Storage v3.0.3
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / vendor / google / auth / src / Credentials / UserRefreshCredentials.php

UserRefreshCredentials.php in WP-Stateless – Google Cloud Storage 3.0.3, at lib/Google/vendor/google/auth/src/Credentials/UserRefreshCredentials.php

139 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\GetQuotaProjectInterface;
22 use Google\Auth\OAuth2;
23
24 /**
25 * Authenticates requests using User Refresh credentials.
26 *
27 * This class allows authorizing requests from user refresh tokens.
28 *
29 * This the end of the result of a 3LO flow. E.g, the end result of
30 * 'gcloud auth login' saves a file with these contents in well known
31 * location
32 *
33 * @see [Application Default Credentials](http://goo.gl/mkAHpZ)
34 */
35 class UserRefreshCredentials extends CredentialsLoader implements GetQuotaProjectInterface
36 {
37 /**
38 * The OAuth2 instance used to conduct authorization.
39 *
40 * @var OAuth2
41 */
42 protected $auth;
43
44 /**
45 * The quota project associated with the JSON credentials
46 */
47 protected $quotaProject;
48
49 /**
50 * Create a new UserRefreshCredentials.
51 *
52 * @param string|array $scope the scope of the access request, expressed
53 * either as an Array or as a space-delimited String.
54 * @param string|array $jsonKey JSON credential file path or JSON credentials
55 * as an associative array
56 */
57 public function __construct(
58 $scope,
59 $jsonKey
60 ) {
61 if (is_string($jsonKey)) {
62 if (!file_exists($jsonKey)) {
63 throw new \InvalidArgumentException('file does not exist');
64 }
65 $jsonKeyStream = file_get_contents($jsonKey);
66 if (!$jsonKey = json_decode($jsonKeyStream, true)) {
67 throw new \LogicException('invalid json for auth config');
68 }
69 }
70 if (!array_key_exists('client_id', $jsonKey)) {
71 throw new \InvalidArgumentException(
72 'json key is missing the client_id field'
73 );
74 }
75 if (!array_key_exists('client_secret', $jsonKey)) {
76 throw new \InvalidArgumentException(
77 'json key is missing the client_secret field'
78 );
79 }
80 if (!array_key_exists('refresh_token', $jsonKey)) {
81 throw new \InvalidArgumentException(
82 'json key is missing the refresh_token field'
83 );
84 }
85 $this->auth = new OAuth2([
86 'clientId' => $jsonKey['client_id'],
87 'clientSecret' => $jsonKey['client_secret'],
88 'refresh_token' => $jsonKey['refresh_token'],
89 'scope' => $scope,
90 'tokenCredentialUri' => self::TOKEN_CREDENTIAL_URI,
91 ]);
92 if (array_key_exists('quota_project', $jsonKey)) {
93 $this->quotaProject = (string) $jsonKey['quota_project'];
94 }
95 }
96
97 /**
98 * @param callable $httpHandler
99 *
100 * @return array A set of auth related metadata, containing the following
101 * keys:
102 * - access_token (string)
103 * - expires_in (int)
104 * - scope (string)
105 * - token_type (string)
106 * - id_token (string)
107 */
108 public function fetchAuthToken(callable $httpHandler = null)
109 {
110 return $this->auth->fetchAuthToken($httpHandler);
111 }
112
113 /**
114 * @return string
115 */
116 public function getCacheKey()
117 {
118 return $this->auth->getClientId() . ':' . $this->auth->getCacheKey();
119 }
120
121 /**
122 * @return array
123 */
124 public function getLastReceivedToken()
125 {
126 return $this->auth->getLastReceivedToken();
127 }
128
129 /**
130 * Get the quota project used for this API request
131 *
132 * @return string|null
133 */
134 public function getQuotaProject()
135 {
136 return $this->quotaProject;
137 }
138 }
139