PluginProbe
WP-Stateless – Google Cloud Storage / 2.1.4
WP-Stateless – Google Cloud Storage v2.1.4
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 2.1.4, at lib/Google/vendor/google/auth/src/Credentials/AppIdentityCredentials.php

145 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
22 /**
23 * The AppIdentityService class is automatically defined on App Engine,
24 * so including this dependency is not necessary, and will result in a
25 * PHP fatal error in the App Engine environment.
26 */
27 use google\appengine\api\app_identity\AppIdentityService;
28
29 /**
30 * AppIdentityCredentials supports authorization on Google App Engine.
31 *
32 * It can be used to authorize requests using the AuthTokenMiddleware or
33 * AuthTokenSubscriber, but will only succeed if being run on App Engine:
34 *
35 * use Google\Auth\Credentials\AppIdentityCredentials;
36 * use Google\Auth\Middleware\AuthTokenMiddleware;
37 * use GuzzleHttp\Client;
38 * use GuzzleHttp\HandlerStack;
39 *
40 * $gae = new AppIdentityCredentials('https://www.googleapis.com/auth/books');
41 * $middleware = new AuthTokenMiddleware($gae);
42 * $stack = HandlerStack::create();
43 * $stack->push($middleware);
44 *
45 * $client = new Client([
46 * 'handler' => $stack,
47 * 'base_uri' => 'https://www.googleapis.com/books/v1',
48 * 'auth' => 'google_auth'
49 * ]);
50 *
51 * $res = $client->get('volumes?q=Henry+David+Thoreau&country=US');
52 */
53 class AppIdentityCredentials extends CredentialsLoader
54 {
55 /**
56 * Result of fetchAuthToken
57 */
58 protected $lastReceivedToken;
59
60 /**
61 * Array of OAuth2 scopes to be requested
62 */
63 private $scope;
64
65 public function __construct($scope = array())
66 {
67 $this->scope = $scope;
68 }
69
70 /**
71 * Determines if this an App Engine instance, by accessing the SERVER_SOFTWARE
72 * environment variable.
73 *
74 * @return true if this an App Engine Instance, false otherwise
75 */
76 public static function onAppEngine()
77 {
78 return (isset($_SERVER['SERVER_SOFTWARE']) &&
79 strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false);
80 }
81
82 /**
83 * Implements FetchAuthTokenInterface#fetchAuthToken.
84 *
85 * Fetches the auth tokens using the AppIdentityService if available.
86 * As the AppIdentityService uses protobufs to fetch the access token,
87 * the GuzzleHttp\ClientInterface instance passed in will not be used.
88 *
89 * @param callable $httpHandler callback which delivers psr7 request
90 * @return array the auth metadata:
91 * array(2) {
92 * ["access_token"]=>
93 * string(3) "xyz"
94 * ["expiration_time"]=>
95 * string(10) "1444339905"
96 * }
97 */
98 public function fetchAuthToken(callable $httpHandler = null)
99 {
100 if (!self::onAppEngine()) {
101 return array();
102 }
103
104 if (!class_exists('google\appengine\api\app_identity\AppIdentityService')) {
105 throw new \Exception(
106 'This class must be run in App Engine, or you must include the AppIdentityService '
107 . 'mock class defined in tests/mocks/AppIdentityService.php'
108 );
109 }
110
111 // AppIdentityService expects an array when multiple scopes are supplied
112 $scope = is_array($this->scope) ? $this->scope : explode(' ', $this->scope);
113
114 $token = AppIdentityService::getAccessToken($scope);
115 $this->lastReceivedToken = $token;
116
117 return $token;
118 }
119
120 /**
121 * Implements FetchAuthTokenInterface#getLastReceivedToken.
122 */
123 public function getLastReceivedToken()
124 {
125 if ($this->lastReceivedToken) {
126 return [
127 'access_token' => $this->lastReceivedToken['access_token'],
128 'expires_at' => $this->lastReceivedToken['expiration_time'],
129 ];
130 }
131
132 return null;
133 }
134
135 /**
136 * Implements FetchAuthTokenInterface#getCacheKey.
137 *
138 * @return 'GOOGLE_AUTH_PHP_APPIDENTITY'
139 */
140 public function getCacheKey()
141 {
142 return 'GOOGLE_AUTH_PHP_APPIDENTITY';
143 }
144 }
145