PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / google / auth / src / Credentials / AppIdentityCredentials.php

AppIdentityCredentials.php in Media Cloud Sync 1.2.12, at includes/sdk/google/google/auth/src/Credentials/AppIdentityCredentials.php

210 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * Copyright 2015 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 namespace Dudlewebs\WPMCS\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 Dudlewebs\WPMCS\google\appengine\api\app_identity\AppIdentityService;
26 use Dudlewebs\WPMCS\Google\Auth\CredentialsLoader;
27 use Dudlewebs\WPMCS\Google\Auth\ProjectIdProviderInterface;
28 use Dudlewebs\WPMCS\Google\Auth\SignBlobInterface;
29 /**
30 * @deprecated
31 *
32 * AppIdentityCredentials supports authorization on Google App Engine.
33 *
34 * It can be used to authorize requests using the AuthTokenMiddleware or
35 * AuthTokenSubscriber, but will only succeed if being run on App Engine:
36 *
37 * Example:
38 * ```
39 * use Google\Auth\Credentials\AppIdentityCredentials;
40 * use Google\Auth\Middleware\AuthTokenMiddleware;
41 * use GuzzleHttp\Client;
42 * use GuzzleHttp\HandlerStack;
43 *
44 * $gae = new AppIdentityCredentials('https://www.googleapis.com/auth/books');
45 * $middleware = new AuthTokenMiddleware($gae);
46 * $stack = HandlerStack::create();
47 * $stack->push($middleware);
48 *
49 * $client = new Client([
50 * 'handler' => $stack,
51 * 'base_uri' => 'https://www.googleapis.com/books/v1',
52 * 'auth' => 'google_auth'
53 * ]);
54 *
55 * $res = $client->get('volumes?q=Henry+David+Thoreau&country=US');
56 * ```
57 */
58 class AppIdentityCredentials extends CredentialsLoader implements SignBlobInterface, ProjectIdProviderInterface
59 {
60 /**
61 * Result of fetchAuthToken.
62 *
63 * @var array<mixed>
64 */
65 protected $lastReceivedToken;
66 /**
67 * Array of OAuth2 scopes to be requested.
68 *
69 * @var string[]
70 */
71 private $scope;
72 /**
73 * @var string
74 */
75 private $clientName;
76 /**
77 * @param string|string[] $scope One or more scopes.
78 */
79 public function __construct($scope = [])
80 {
81 $this->scope = is_array($scope) ? $scope : explode(' ', (string) $scope);
82 }
83 /**
84 * Determines if this an App Engine instance, by accessing the
85 * SERVER_SOFTWARE environment variable (prod) or the APPENGINE_RUNTIME
86 * environment variable (dev).
87 *
88 * @return bool true if this an App Engine Instance, false otherwise
89 */
90 public static function onAppEngine()
91 {
92 $appEngineProduction = isset($_SERVER['SERVER_SOFTWARE']) && 0 === strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine');
93 if ($appEngineProduction) {
94 return \true;
95 }
96 $appEngineDevAppServer = isset($_SERVER['APPENGINE_RUNTIME']) && $_SERVER['APPENGINE_RUNTIME'] == 'php';
97 if ($appEngineDevAppServer) {
98 return \true;
99 }
100 return \false;
101 }
102 /**
103 * Implements FetchAuthTokenInterface#fetchAuthToken.
104 *
105 * Fetches the auth tokens using the AppIdentityService if available.
106 * As the AppIdentityService uses protobufs to fetch the access token,
107 * the GuzzleHttp\ClientInterface instance passed in will not be used.
108 *
109 * @param callable $httpHandler callback which delivers psr7 request
110 * @return array<mixed> {
111 * A set of auth related metadata, containing the following
112 *
113 * @type string $access_token
114 * @type string $expiration_time
115 * }
116 */
117 public function fetchAuthToken(callable $httpHandler = null)
118 {
119 try {
120 $this->checkAppEngineContext();
121 } catch (\Exception $e) {
122 return [];
123 }
124 /** @phpstan-ignore-next-line */
125 $token = AppIdentityService::getAccessToken($this->scope);
126 $this->lastReceivedToken = $token;
127 return $token;
128 }
129 /**
130 * Sign a string using AppIdentityService.
131 *
132 * @param string $stringToSign The string to sign.
133 * @param bool $forceOpenSsl [optional] Does not apply to this credentials
134 * type.
135 * @return string The signature, base64-encoded.
136 * @throws \Exception If AppEngine SDK or mock is not available.
137 */
138 public function signBlob($stringToSign, $forceOpenSsl = \false)
139 {
140 $this->checkAppEngineContext();
141 /** @phpstan-ignore-next-line */
142 return base64_encode(AppIdentityService::signForApp($stringToSign)['signature']);
143 }
144 /**
145 * Get the project ID from AppIdentityService.
146 *
147 * Returns null if AppIdentityService is unavailable.
148 *
149 * @param callable $httpHandler Not used by this type.
150 * @return string|null
151 */
152 public function getProjectId(callable $httpHandler = null)
153 {
154 try {
155 $this->checkAppEngineContext();
156 } catch (\Exception $e) {
157 return null;
158 }
159 /** @phpstan-ignore-next-line */
160 return AppIdentityService::getApplicationId();
161 }
162 /**
163 * Get the client name from AppIdentityService.
164 *
165 * Subsequent calls to this method will return a cached value.
166 *
167 * @param callable $httpHandler Not used in this implementation.
168 * @return string
169 * @throws \Exception If AppEngine SDK or mock is not available.
170 */
171 public function getClientName(callable $httpHandler = null)
172 {
173 $this->checkAppEngineContext();
174 if (!$this->clientName) {
175 /** @phpstan-ignore-next-line */
176 $this->clientName = AppIdentityService::getServiceAccountName();
177 }
178 return $this->clientName;
179 }
180 /**
181 * @return array{access_token:string,expires_at:int}|null
182 */
183 public function getLastReceivedToken()
184 {
185 if ($this->lastReceivedToken) {
186 return ['access_token' => $this->lastReceivedToken['access_token'], 'expires_at' => $this->lastReceivedToken['expiration_time']];
187 }
188 return null;
189 }
190 /**
191 * Caching is handled by the underlying AppIdentityService, return empty string
192 * to prevent caching.
193 *
194 * @return string
195 */
196 public function getCacheKey()
197 {
198 return '';
199 }
200 /**
201 * @return void
202 */
203 private function checkAppEngineContext()
204 {
205 if (!self::onAppEngine() || !class_exists('Dudlewebs\WPMCS\google\appengine\api\app_identity\AppIdentityService')) {
206 throw new \Exception('This class must be run in App Engine, or you must include the AppIdentityService ' . 'mock class defined in tests/mocks/AppIdentityService.php');
207 }
208 }
209 }
210