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 / AppIdentityCredentials.php

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

231 lines 6.7 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 /*
21 * The AppIdentityService class is automatically defined on App Engine,
22 * so including this dependency is not necessary, and will result in a
23 * PHP fatal error in the App Engine environment.
24 */
25 use google\appengine\api\app_identity\AppIdentityService;
26 use Google\Auth\CredentialsLoader;
27 use Google\Auth\ProjectIdProviderInterface;
28 use Google\Auth\SignBlobInterface;
29
30 /**
31 * AppIdentityCredentials supports authorization on Google App Engine.
32 *
33 * It can be used to authorize requests using the AuthTokenMiddleware or
34 * AuthTokenSubscriber, but will only succeed if being run on App Engine:
35 *
36 * Example:
37 * ```
38 * use Google\Auth\Credentials\AppIdentityCredentials;
39 * use Google\Auth\Middleware\AuthTokenMiddleware;
40 * use GuzzleHttp\Client;
41 * use GuzzleHttp\HandlerStack;
42 *
43 * $gae = new AppIdentityCredentials('https://www.googleapis.com/auth/books');
44 * $middleware = new AuthTokenMiddleware($gae);
45 * $stack = HandlerStack::create();
46 * $stack->push($middleware);
47 *
48 * $client = new Client([
49 * 'handler' => $stack,
50 * 'base_uri' => 'https://www.googleapis.com/books/v1',
51 * 'auth' => 'google_auth'
52 * ]);
53 *
54 * $res = $client->get('volumes?q=Henry+David+Thoreau&country=US');
55 * ```
56 */
57 class AppIdentityCredentials extends CredentialsLoader implements
58 SignBlobInterface,
59 ProjectIdProviderInterface
60 {
61 /**
62 * Result of fetchAuthToken.
63 *
64 * @var array
65 */
66 protected $lastReceivedToken;
67
68 /**
69 * Array of OAuth2 scopes to be requested.
70 *
71 * @var array
72 */
73 private $scope;
74
75 /**
76 * @var string
77 */
78 private $clientName;
79
80 /**
81 * @param array $scope One or more scopes.
82 */
83 public function __construct($scope = array())
84 {
85 $this->scope = $scope;
86 }
87
88 /**
89 * Determines if this an App Engine instance, by accessing the
90 * SERVER_SOFTWARE environment variable (prod) or the APPENGINE_RUNTIME
91 * environment variable (dev).
92 *
93 * @return bool true if this an App Engine Instance, false otherwise
94 */
95 public static function onAppEngine()
96 {
97 $appEngineProduction = isset($_SERVER['SERVER_SOFTWARE']) &&
98 0 === strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine');
99 if ($appEngineProduction) {
100 return true;
101 }
102 $appEngineDevAppServer = isset($_SERVER['APPENGINE_RUNTIME']) &&
103 $_SERVER['APPENGINE_RUNTIME'] == 'php';
104 if ($appEngineDevAppServer) {
105 return true;
106 }
107 return false;
108 }
109
110 /**
111 * Implements FetchAuthTokenInterface#fetchAuthToken.
112 *
113 * Fetches the auth tokens using the AppIdentityService if available.
114 * As the AppIdentityService uses protobufs to fetch the access token,
115 * the GuzzleHttp\ClientInterface instance passed in will not be used.
116 *
117 * @param callable $httpHandler callback which delivers psr7 request
118 * @return array A set of auth related metadata, containing the following
119 * keys:
120 * - access_token (string)
121 * - expiration_time (string)
122 */
123 public function fetchAuthToken(callable $httpHandler = null)
124 {
125 try {
126 $this->checkAppEngineContext();
127 } catch (\Exception $e) {
128 return [];
129 }
130
131 // AppIdentityService expects an array when multiple scopes are supplied
132 $scope = is_array($this->scope) ? $this->scope : explode(' ', $this->scope);
133
134 $token = AppIdentityService::getAccessToken($scope);
135 $this->lastReceivedToken = $token;
136
137 return $token;
138 }
139
140 /**
141 * Sign a string using AppIdentityService.
142 *
143 * @param string $stringToSign The string to sign.
144 * @param bool $forceOpenSsl [optional] Does not apply to this credentials
145 * type.
146 * @return string The signature, base64-encoded.
147 * @throws \Exception If AppEngine SDK or mock is not available.
148 */
149 public function signBlob($stringToSign, $forceOpenSsl = false)
150 {
151 $this->checkAppEngineContext();
152
153 return base64_encode(AppIdentityService::signForApp($stringToSign)['signature']);
154 }
155
156 /**
157 * Get the project ID from AppIdentityService.
158 *
159 * Returns null if AppIdentityService is unavailable.
160 *
161 * @param callable $httpHandler Not used by this type.
162 * @return string|null
163 */
164 public function getProjectId(callable $httpHander = null)
165 {
166 try {
167 $this->checkAppEngineContext();
168 } catch (\Exception $e) {
169 return null;
170 }
171
172 return AppIdentityService::getApplicationId();
173 }
174
175 /**
176 * Get the client name from AppIdentityService.
177 *
178 * Subsequent calls to this method will return a cached value.
179 *
180 * @param callable $httpHandler Not used in this implementation.
181 * @return string
182 * @throws \Exception If AppEngine SDK or mock is not available.
183 */
184 public function getClientName(callable $httpHandler = null)
185 {
186 $this->checkAppEngineContext();
187
188 if (!$this->clientName) {
189 $this->clientName = AppIdentityService::getServiceAccountName();
190 }
191
192 return $this->clientName;
193 }
194
195 /**
196 * @return array|null
197 */
198 public function getLastReceivedToken()
199 {
200 if ($this->lastReceivedToken) {
201 return [
202 'access_token' => $this->lastReceivedToken['access_token'],
203 'expires_at' => $this->lastReceivedToken['expiration_time'],
204 ];
205 }
206
207 return null;
208 }
209
210 /**
211 * Caching is handled by the underlying AppIdentityService, return empty string
212 * to prevent caching.
213 *
214 * @return string
215 */
216 public function getCacheKey()
217 {
218 return '';
219 }
220
221 private function checkAppEngineContext()
222 {
223 if (!self::onAppEngine() || !class_exists('google\appengine\api\app_identity\AppIdentityService')) {
224 throw new \Exception(
225 'This class must be run in App Engine, or you must include the AppIdentityService '
226 . 'mock class defined in tests/mocks/AppIdentityService.php'
227 );
228 }
229 }
230 }
231