@@ -20,23 +20,23 @@ |
||
20 | 20 | |
21 | 21 | class Google_AuthHandler_AuthHandlerFactory |
22 | 22 | { |
23 | - /** |
|
24 | - * Builds out a default http handler for the installed version of guzzle. |
|
25 | - * |
|
26 | - * @return Google_AuthHandler_Guzzle5AuthHandler|Google_AuthHandler_Guzzle6AuthHandler |
|
27 | - * @throws Exception |
|
28 | - */ |
|
29 | - public static function build($cache = null, array $cacheConfig = []) |
|
30 | - { |
|
23 | + /** |
|
24 | + * Builds out a default http handler for the installed version of guzzle. |
|
25 | + * |
|
26 | + * @return Google_AuthHandler_Guzzle5AuthHandler|Google_AuthHandler_Guzzle6AuthHandler |
|
27 | + * @throws Exception |
|
28 | + */ |
|
29 | + public static function build($cache = null, array $cacheConfig = []) |
|
30 | + { |
|
31 | 31 | $version = ClientInterface::VERSION; |
32 | 32 | |
33 | 33 | switch ($version[0]) { |
34 | - case '5': |
|
34 | + case '5': |
|
35 | 35 | return new Google_AuthHandler_Guzzle5AuthHandler($cache, $cacheConfig); |
36 | - case '6': |
|
36 | + case '6': |
|
37 | 37 | return new Google_AuthHandler_Guzzle6AuthHandler($cache, $cacheConfig); |
38 | - default: |
|
38 | + default: |
|
39 | 39 | throw new Exception('Version not supported'); |
40 | 40 | } |
41 | - } |
|
41 | + } |
|
42 | 42 | } |
@@ -15,27 +15,27 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class Google_AuthHandler_Guzzle6AuthHandler |
17 | 17 | { |
18 | - protected $cache; |
|
19 | - protected $cacheConfig; |
|
18 | + protected $cache; |
|
19 | + protected $cacheConfig; |
|
20 | 20 | |
21 | - public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) |
|
22 | - { |
|
21 | + public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) |
|
22 | + { |
|
23 | 23 | $this->cache = $cache; |
24 | 24 | $this->cacheConfig = $cacheConfig; |
25 | - } |
|
25 | + } |
|
26 | 26 | |
27 | - public function attachCredentials( |
|
28 | - ClientInterface $http, |
|
29 | - CredentialsLoader $credentials, |
|
30 | - callable $tokenCallback = null |
|
31 | - ) { |
|
27 | + public function attachCredentials( |
|
28 | + ClientInterface $http, |
|
29 | + CredentialsLoader $credentials, |
|
30 | + callable $tokenCallback = null |
|
31 | + ) { |
|
32 | 32 | // use the provided cache |
33 | 33 | if ($this->cache) { |
34 | - $credentials = new FetchAuthTokenCache( |
|
35 | - $credentials, |
|
36 | - $this->cacheConfig, |
|
37 | - $this->cache |
|
38 | - ); |
|
34 | + $credentials = new FetchAuthTokenCache( |
|
35 | + $credentials, |
|
36 | + $this->cacheConfig, |
|
37 | + $this->cache |
|
38 | + ); |
|
39 | 39 | } |
40 | 40 | // if we end up needing to make an HTTP request to retrieve credentials, we |
41 | 41 | // can use our existing one, but we need to throw exceptions so the error |
@@ -55,12 +55,12 @@ discard block |
||
55 | 55 | $http = new Client($config); |
56 | 56 | |
57 | 57 | return $http; |
58 | - } |
|
58 | + } |
|
59 | 59 | |
60 | - public function attachToken(ClientInterface $http, array $token, array $scopes) |
|
61 | - { |
|
60 | + public function attachToken(ClientInterface $http, array $token, array $scopes) |
|
61 | + { |
|
62 | 62 | $tokenFunc = function ($scopes) use ($token) { |
63 | - return $token['access_token']; |
|
63 | + return $token['access_token']; |
|
64 | 64 | }; |
65 | 65 | |
66 | 66 | $middleware = new ScopedAccessTokenMiddleware( |
@@ -77,10 +77,10 @@ discard block |
||
77 | 77 | $http = new Client($config); |
78 | 78 | |
79 | 79 | return $http; |
80 | - } |
|
80 | + } |
|
81 | 81 | |
82 | - public function attachKey(ClientInterface $http, $key) |
|
83 | - { |
|
82 | + public function attachKey(ClientInterface $http, $key) |
|
83 | + { |
|
84 | 84 | $middleware = new SimpleMiddleware(['key' => $key]); |
85 | 85 | |
86 | 86 | $config = $http->getConfig(); |
@@ -90,17 +90,17 @@ discard block |
||
90 | 90 | $http = new Client($config); |
91 | 91 | |
92 | 92 | return $http; |
93 | - } |
|
93 | + } |
|
94 | 94 | |
95 | - private function createAuthHttp(ClientInterface $http) |
|
96 | - { |
|
95 | + private function createAuthHttp(ClientInterface $http) |
|
96 | + { |
|
97 | 97 | return new Client( |
98 | 98 | [ |
99 | - 'base_uri' => $http->getConfig('base_uri'), |
|
100 | - 'exceptions' => true, |
|
101 | - 'verify' => $http->getConfig('verify'), |
|
102 | - 'proxy' => $http->getConfig('proxy'), |
|
99 | + 'base_uri' => $http->getConfig('base_uri'), |
|
100 | + 'exceptions' => true, |
|
101 | + 'verify' => $http->getConfig('verify'), |
|
102 | + 'proxy' => $http->getConfig('proxy'), |
|
103 | 103 | ] |
104 | 104 | ); |
105 | - } |
|
105 | + } |
|
106 | 106 | } |
@@ -56,7 +56,7 @@ |
||
56 | 56 | |
57 | 57 | public function attachToken(ClientInterface $http, array $token, array $scopes) |
58 | 58 | { |
59 | - $tokenFunc = function ($scopes) use ($token) { |
|
59 | + $tokenFunc = function($scopes) use ($token) { |
|
60 | 60 | return $token['access_token']; |
61 | 61 | }; |
62 | 62 |
@@ -9,9 +9,9 @@ |
||
9 | 9 | $file = __DIR__ . '/../../vendor/autoload.php'; |
10 | 10 | |
11 | 11 | if (!file_exists($file)) { |
12 | - $exception = 'This library must be installed via composer or by downloading the full package.'; |
|
13 | - $exception .= ' See the instructions at https://github.com/google/google-api-php-client#installation.'; |
|
14 | - throw new Exception($exception); |
|
12 | + $exception = 'This library must be installed via composer or by downloading the full package.'; |
|
13 | + $exception .= ' See the instructions at https://github.com/google/google-api-php-client#installation.'; |
|
14 | + throw new Exception($exception); |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | $error = 'google-api-php-client\'s autoloader was moved to vendor/autoload.php in 2.0.0. This '; |
@@ -38,153 +38,153 @@ discard block |
||
38 | 38 | */ |
39 | 39 | class Google_Client |
40 | 40 | { |
41 | - const LIBVER = "2.2.3"; |
|
42 | - const USER_AGENT_SUFFIX = "google-api-php-client/"; |
|
43 | - const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; |
|
44 | - const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; |
|
45 | - const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth'; |
|
46 | - const API_BASE_PATH = 'https://www.googleapis.com'; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var Google\Auth\OAuth2 $auth |
|
50 | - */ |
|
51 | - private $auth; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var GuzzleHttp\ClientInterface $http |
|
55 | - */ |
|
56 | - private $http; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var Psr\Cache\CacheItemPoolInterface $cache |
|
60 | - */ |
|
61 | - private $cache; |
|
62 | - |
|
63 | - /** |
|
64 | - * @var array access token |
|
65 | - */ |
|
66 | - private $token; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var array $config |
|
70 | - */ |
|
71 | - private $config; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var Psr\Log\LoggerInterface $logger |
|
75 | - */ |
|
76 | - private $logger; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var boolean $deferExecution |
|
80 | - */ |
|
81 | - private $deferExecution = false; |
|
82 | - |
|
83 | - /** @var array $scopes */ |
|
84 | - // Scopes requested by the client |
|
85 | - protected $requestedScopes = []; |
|
86 | - |
|
87 | - /** |
|
88 | - * Construct the Google Client. |
|
89 | - * |
|
90 | - * @param array $config |
|
91 | - */ |
|
92 | - public function __construct(array $config = array()) |
|
93 | - { |
|
41 | + const LIBVER = "2.2.3"; |
|
42 | + const USER_AGENT_SUFFIX = "google-api-php-client/"; |
|
43 | + const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; |
|
44 | + const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; |
|
45 | + const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth'; |
|
46 | + const API_BASE_PATH = 'https://www.googleapis.com'; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var Google\Auth\OAuth2 $auth |
|
50 | + */ |
|
51 | + private $auth; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var GuzzleHttp\ClientInterface $http |
|
55 | + */ |
|
56 | + private $http; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var Psr\Cache\CacheItemPoolInterface $cache |
|
60 | + */ |
|
61 | + private $cache; |
|
62 | + |
|
63 | + /** |
|
64 | + * @var array access token |
|
65 | + */ |
|
66 | + private $token; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var array $config |
|
70 | + */ |
|
71 | + private $config; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var Psr\Log\LoggerInterface $logger |
|
75 | + */ |
|
76 | + private $logger; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var boolean $deferExecution |
|
80 | + */ |
|
81 | + private $deferExecution = false; |
|
82 | + |
|
83 | + /** @var array $scopes */ |
|
84 | + // Scopes requested by the client |
|
85 | + protected $requestedScopes = []; |
|
86 | + |
|
87 | + /** |
|
88 | + * Construct the Google Client. |
|
89 | + * |
|
90 | + * @param array $config |
|
91 | + */ |
|
92 | + public function __construct(array $config = array()) |
|
93 | + { |
|
94 | 94 | $this->config = array_merge( |
95 | 95 | [ |
96 | - 'application_name' => '', |
|
97 | - |
|
98 | - // Don't change these unless you're working against a special development |
|
99 | - // or testing environment. |
|
100 | - 'base_path' => self::API_BASE_PATH, |
|
101 | - |
|
102 | - // https://developers.google.com/console |
|
103 | - 'client_id' => '', |
|
104 | - 'client_secret' => '', |
|
105 | - 'redirect_uri' => null, |
|
106 | - 'state' => null, |
|
107 | - |
|
108 | - // Simple API access key, also from the API console. Ensure you get |
|
109 | - // a Server key, and not a Browser key. |
|
110 | - 'developer_key' => '', |
|
111 | - |
|
112 | - // For use with Google Cloud Platform |
|
113 | - // fetch the ApplicationDefaultCredentials, if applicable |
|
114 | - // @see https://developers.google.com/identity/protocols/application-default-credentials |
|
115 | - 'use_application_default_credentials' => false, |
|
116 | - 'signing_key' => null, |
|
117 | - 'signing_algorithm' => null, |
|
118 | - 'subject' => null, |
|
119 | - |
|
120 | - // Other OAuth2 parameters. |
|
121 | - 'hd' => '', |
|
122 | - 'prompt' => '', |
|
123 | - 'openid.realm' => '', |
|
124 | - 'include_granted_scopes' => null, |
|
125 | - 'login_hint' => '', |
|
126 | - 'request_visible_actions' => '', |
|
127 | - 'access_type' => 'online', |
|
128 | - 'approval_prompt' => 'auto', |
|
129 | - |
|
130 | - // Task Runner retry configuration |
|
131 | - // @see Google_Task_Runner |
|
132 | - 'retry' => array(), |
|
133 | - 'retry_map' => null, |
|
134 | - |
|
135 | - // cache config for downstream auth caching |
|
136 | - 'cache_config' => [], |
|
137 | - |
|
138 | - // function to be called when an access token is fetched |
|
139 | - // follows the signature function ($cacheKey, $accessToken) |
|
140 | - 'token_callback' => null, |
|
141 | - |
|
142 | - // Service class used in Google_Client::verifyIdToken. |
|
143 | - // Explicitly pass this in to avoid setting JWT::$leeway |
|
144 | - 'jwt' => null, |
|
145 | - |
|
146 | - // Setting api_format_v2 will return more detailed error messages |
|
147 | - // from certain APIs. |
|
148 | - 'api_format_v2' => false |
|
96 | + 'application_name' => '', |
|
97 | + |
|
98 | + // Don't change these unless you're working against a special development |
|
99 | + // or testing environment. |
|
100 | + 'base_path' => self::API_BASE_PATH, |
|
101 | + |
|
102 | + // https://developers.google.com/console |
|
103 | + 'client_id' => '', |
|
104 | + 'client_secret' => '', |
|
105 | + 'redirect_uri' => null, |
|
106 | + 'state' => null, |
|
107 | + |
|
108 | + // Simple API access key, also from the API console. Ensure you get |
|
109 | + // a Server key, and not a Browser key. |
|
110 | + 'developer_key' => '', |
|
111 | + |
|
112 | + // For use with Google Cloud Platform |
|
113 | + // fetch the ApplicationDefaultCredentials, if applicable |
|
114 | + // @see https://developers.google.com/identity/protocols/application-default-credentials |
|
115 | + 'use_application_default_credentials' => false, |
|
116 | + 'signing_key' => null, |
|
117 | + 'signing_algorithm' => null, |
|
118 | + 'subject' => null, |
|
119 | + |
|
120 | + // Other OAuth2 parameters. |
|
121 | + 'hd' => '', |
|
122 | + 'prompt' => '', |
|
123 | + 'openid.realm' => '', |
|
124 | + 'include_granted_scopes' => null, |
|
125 | + 'login_hint' => '', |
|
126 | + 'request_visible_actions' => '', |
|
127 | + 'access_type' => 'online', |
|
128 | + 'approval_prompt' => 'auto', |
|
129 | + |
|
130 | + // Task Runner retry configuration |
|
131 | + // @see Google_Task_Runner |
|
132 | + 'retry' => array(), |
|
133 | + 'retry_map' => null, |
|
134 | + |
|
135 | + // cache config for downstream auth caching |
|
136 | + 'cache_config' => [], |
|
137 | + |
|
138 | + // function to be called when an access token is fetched |
|
139 | + // follows the signature function ($cacheKey, $accessToken) |
|
140 | + 'token_callback' => null, |
|
141 | + |
|
142 | + // Service class used in Google_Client::verifyIdToken. |
|
143 | + // Explicitly pass this in to avoid setting JWT::$leeway |
|
144 | + 'jwt' => null, |
|
145 | + |
|
146 | + // Setting api_format_v2 will return more detailed error messages |
|
147 | + // from certain APIs. |
|
148 | + 'api_format_v2' => false |
|
149 | 149 | ], |
150 | 150 | $config |
151 | 151 | ); |
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Get a string containing the version of the library. |
|
156 | - * |
|
157 | - * @return string |
|
158 | - */ |
|
159 | - public function getLibraryVersion() |
|
160 | - { |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Get a string containing the version of the library. |
|
156 | + * |
|
157 | + * @return string |
|
158 | + */ |
|
159 | + public function getLibraryVersion() |
|
160 | + { |
|
161 | 161 | return self::LIBVER; |
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * For backwards compatibility |
|
166 | - * alias for fetchAccessTokenWithAuthCode |
|
167 | - * |
|
168 | - * @param $code string code from accounts.google.com |
|
169 | - * @return array access token |
|
170 | - * @deprecated |
|
171 | - */ |
|
172 | - public function authenticate($code) |
|
173 | - { |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * For backwards compatibility |
|
166 | + * alias for fetchAccessTokenWithAuthCode |
|
167 | + * |
|
168 | + * @param $code string code from accounts.google.com |
|
169 | + * @return array access token |
|
170 | + * @deprecated |
|
171 | + */ |
|
172 | + public function authenticate($code) |
|
173 | + { |
|
174 | 174 | return $this->fetchAccessTokenWithAuthCode($code); |
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Attempt to exchange a code for an valid authentication token. |
|
179 | - * Helper wrapped around the OAuth 2.0 implementation. |
|
180 | - * |
|
181 | - * @param $code string code from accounts.google.com |
|
182 | - * @return array access token |
|
183 | - */ |
|
184 | - public function fetchAccessTokenWithAuthCode($code) |
|
185 | - { |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Attempt to exchange a code for an valid authentication token. |
|
179 | + * Helper wrapped around the OAuth 2.0 implementation. |
|
180 | + * |
|
181 | + * @param $code string code from accounts.google.com |
|
182 | + * @return array access token |
|
183 | + */ |
|
184 | + public function fetchAccessTokenWithAuthCode($code) |
|
185 | + { |
|
186 | 186 | if (strlen($code) == 0) { |
187 | - throw new InvalidArgumentException("Invalid code"); |
|
187 | + throw new InvalidArgumentException("Invalid code"); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | $auth = $this->getOAuth2Service(); |
@@ -194,40 +194,40 @@ discard block |
||
194 | 194 | $httpHandler = HttpHandlerFactory::build($this->getHttpClient()); |
195 | 195 | $creds = $auth->fetchAuthToken($httpHandler); |
196 | 196 | if ($creds && isset($creds['access_token'])) { |
197 | - $creds['created'] = time(); |
|
198 | - $this->setAccessToken($creds); |
|
197 | + $creds['created'] = time(); |
|
198 | + $this->setAccessToken($creds); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | return $creds; |
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * For backwards compatibility |
|
206 | - * alias for fetchAccessTokenWithAssertion |
|
207 | - * |
|
208 | - * @return array access token |
|
209 | - * @deprecated |
|
210 | - */ |
|
211 | - public function refreshTokenWithAssertion() |
|
212 | - { |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * For backwards compatibility |
|
206 | + * alias for fetchAccessTokenWithAssertion |
|
207 | + * |
|
208 | + * @return array access token |
|
209 | + * @deprecated |
|
210 | + */ |
|
211 | + public function refreshTokenWithAssertion() |
|
212 | + { |
|
213 | 213 | return $this->fetchAccessTokenWithAssertion(); |
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Fetches a fresh access token with a given assertion token. |
|
218 | - * @param ClientInterface $authHttp optional. |
|
219 | - * @return array access token |
|
220 | - */ |
|
221 | - public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null) |
|
222 | - { |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Fetches a fresh access token with a given assertion token. |
|
218 | + * @param ClientInterface $authHttp optional. |
|
219 | + * @return array access token |
|
220 | + */ |
|
221 | + public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null) |
|
222 | + { |
|
223 | 223 | if (!$this->isUsingApplicationDefaultCredentials()) { |
224 | - throw new DomainException( |
|
225 | - 'set the JSON service account credentials using' |
|
226 | - . ' Google_Client::setAuthConfig or set the path to your JSON file' |
|
227 | - . ' with the "GOOGLE_APPLICATION_CREDENTIALS" environment variable' |
|
228 | - . ' and call Google_Client::useApplicationDefaultCredentials to' |
|
229 | - . ' refresh a token with assertion.' |
|
230 | - ); |
|
224 | + throw new DomainException( |
|
225 | + 'set the JSON service account credentials using' |
|
226 | + . ' Google_Client::setAuthConfig or set the path to your JSON file' |
|
227 | + . ' with the "GOOGLE_APPLICATION_CREDENTIALS" environment variable' |
|
228 | + . ' and call Google_Client::useApplicationDefaultCredentials to' |
|
229 | + . ' refresh a token with assertion.' |
|
230 | + ); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | $this->getLogger()->log( |
@@ -240,39 +240,39 @@ discard block |
||
240 | 240 | $httpHandler = HttpHandlerFactory::build($authHttp); |
241 | 241 | $creds = $credentials->fetchAuthToken($httpHandler); |
242 | 242 | if ($creds && isset($creds['access_token'])) { |
243 | - $creds['created'] = time(); |
|
244 | - $this->setAccessToken($creds); |
|
243 | + $creds['created'] = time(); |
|
244 | + $this->setAccessToken($creds); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | return $creds; |
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * For backwards compatibility |
|
252 | - * alias for fetchAccessTokenWithRefreshToken |
|
253 | - * |
|
254 | - * @param string $refreshToken |
|
255 | - * @return array access token |
|
256 | - */ |
|
257 | - public function refreshToken($refreshToken) |
|
258 | - { |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * For backwards compatibility |
|
252 | + * alias for fetchAccessTokenWithRefreshToken |
|
253 | + * |
|
254 | + * @param string $refreshToken |
|
255 | + * @return array access token |
|
256 | + */ |
|
257 | + public function refreshToken($refreshToken) |
|
258 | + { |
|
259 | 259 | return $this->fetchAccessTokenWithRefreshToken($refreshToken); |
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Fetches a fresh OAuth 2.0 access token with the given refresh token. |
|
264 | - * @param string $refreshToken |
|
265 | - * @return array access token |
|
266 | - */ |
|
267 | - public function fetchAccessTokenWithRefreshToken($refreshToken = null) |
|
268 | - { |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Fetches a fresh OAuth 2.0 access token with the given refresh token. |
|
264 | + * @param string $refreshToken |
|
265 | + * @return array access token |
|
266 | + */ |
|
267 | + public function fetchAccessTokenWithRefreshToken($refreshToken = null) |
|
268 | + { |
|
269 | 269 | if (null === $refreshToken) { |
270 | - if (!isset($this->token['refresh_token'])) { |
|
270 | + if (!isset($this->token['refresh_token'])) { |
|
271 | 271 | throw new LogicException( |
272 | 272 | 'refresh token must be passed in or set as part of setAccessToken' |
273 | 273 | ); |
274 | - } |
|
275 | - $refreshToken = $this->token['refresh_token']; |
|
274 | + } |
|
275 | + $refreshToken = $this->token['refresh_token']; |
|
276 | 276 | } |
277 | 277 | $this->getLogger()->info('OAuth2 access token refresh'); |
278 | 278 | $auth = $this->getOAuth2Service(); |
@@ -281,30 +281,30 @@ discard block |
||
281 | 281 | $httpHandler = HttpHandlerFactory::build($this->getHttpClient()); |
282 | 282 | $creds = $auth->fetchAuthToken($httpHandler); |
283 | 283 | if ($creds && isset($creds['access_token'])) { |
284 | - $creds['created'] = time(); |
|
285 | - if (!isset($creds['refresh_token'])) { |
|
284 | + $creds['created'] = time(); |
|
285 | + if (!isset($creds['refresh_token'])) { |
|
286 | 286 | $creds['refresh_token'] = $refreshToken; |
287 | - } |
|
288 | - $this->setAccessToken($creds); |
|
287 | + } |
|
288 | + $this->setAccessToken($creds); |
|
289 | 289 | } |
290 | 290 | |
291 | 291 | return $creds; |
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Create a URL to obtain user authorization. |
|
296 | - * The authorization endpoint allows the user to first |
|
297 | - * authenticate, and then grant/deny the access request. |
|
298 | - * @param string|array $scope The scope is expressed as an array or list of space-delimited strings. |
|
299 | - * @return string |
|
300 | - */ |
|
301 | - public function createAuthUrl($scope = null) |
|
302 | - { |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Create a URL to obtain user authorization. |
|
296 | + * The authorization endpoint allows the user to first |
|
297 | + * authenticate, and then grant/deny the access request. |
|
298 | + * @param string|array $scope The scope is expressed as an array or list of space-delimited strings. |
|
299 | + * @return string |
|
300 | + */ |
|
301 | + public function createAuthUrl($scope = null) |
|
302 | + { |
|
303 | 303 | if (empty($scope)) { |
304 | - $scope = $this->prepareScopes(); |
|
304 | + $scope = $this->prepareScopes(); |
|
305 | 305 | } |
306 | 306 | if (is_array($scope)) { |
307 | - $scope = implode(' ', $scope); |
|
307 | + $scope = implode(' ', $scope); |
|
308 | 308 | } |
309 | 309 | |
310 | 310 | // only accept one of prompt or approval_prompt |
@@ -319,16 +319,16 @@ discard block |
||
319 | 319 | |
320 | 320 | $params = array_filter( |
321 | 321 | [ |
322 | - 'access_type' => $this->config['access_type'], |
|
323 | - 'approval_prompt' => $approvalPrompt, |
|
324 | - 'hd' => $this->config['hd'], |
|
325 | - 'include_granted_scopes' => $includeGrantedScopes, |
|
326 | - 'login_hint' => $this->config['login_hint'], |
|
327 | - 'openid.realm' => $this->config['openid.realm'], |
|
328 | - 'prompt' => $this->config['prompt'], |
|
329 | - 'response_type' => 'code', |
|
330 | - 'scope' => $scope, |
|
331 | - 'state' => $this->config['state'], |
|
322 | + 'access_type' => $this->config['access_type'], |
|
323 | + 'approval_prompt' => $approvalPrompt, |
|
324 | + 'hd' => $this->config['hd'], |
|
325 | + 'include_granted_scopes' => $includeGrantedScopes, |
|
326 | + 'login_hint' => $this->config['login_hint'], |
|
327 | + 'openid.realm' => $this->config['openid.realm'], |
|
328 | + 'prompt' => $this->config['prompt'], |
|
329 | + 'response_type' => 'code', |
|
330 | + 'scope' => $scope, |
|
331 | + 'state' => $this->config['state'], |
|
332 | 332 | ] |
333 | 333 | ); |
334 | 334 | |
@@ -342,22 +342,22 @@ discard block |
||
342 | 342 | $auth = $this->getOAuth2Service(); |
343 | 343 | |
344 | 344 | return (string) $auth->buildFullAuthorizationUri($params); |
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Adds auth listeners to the HTTP client based on the credentials |
|
349 | - * set in the Google API Client object |
|
350 | - * |
|
351 | - * @param GuzzleHttp\ClientInterface $http the http client object. |
|
352 | - * @return GuzzleHttp\ClientInterface the http client object |
|
353 | - */ |
|
354 | - public function authorize(ClientInterface $http = null) |
|
355 | - { |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Adds auth listeners to the HTTP client based on the credentials |
|
349 | + * set in the Google API Client object |
|
350 | + * |
|
351 | + * @param GuzzleHttp\ClientInterface $http the http client object. |
|
352 | + * @return GuzzleHttp\ClientInterface the http client object |
|
353 | + */ |
|
354 | + public function authorize(ClientInterface $http = null) |
|
355 | + { |
|
356 | 356 | $credentials = null; |
357 | 357 | $token = null; |
358 | 358 | $scopes = null; |
359 | 359 | if (null === $http) { |
360 | - $http = $this->getHttpClient(); |
|
360 | + $http = $this->getHttpClient(); |
|
361 | 361 | } |
362 | 362 | |
363 | 363 | // These conditionals represent the decision tree for authentication |
@@ -366,349 +366,349 @@ discard block |
||
366 | 366 | // 3a. Check for an Access Token |
367 | 367 | // 3b. If access token exists but is expired, try to refresh it |
368 | 368 | if ($this->isUsingApplicationDefaultCredentials()) { |
369 | - $credentials = $this->createApplicationDefaultCredentials(); |
|
369 | + $credentials = $this->createApplicationDefaultCredentials(); |
|
370 | 370 | } elseif ($token = $this->getAccessToken()) { |
371 | - $scopes = $this->prepareScopes(); |
|
372 | - // add refresh subscriber to request a new token |
|
373 | - if (isset($token['refresh_token']) && $this->isAccessTokenExpired()) { |
|
371 | + $scopes = $this->prepareScopes(); |
|
372 | + // add refresh subscriber to request a new token |
|
373 | + if (isset($token['refresh_token']) && $this->isAccessTokenExpired()) { |
|
374 | 374 | $credentials = $this->createUserRefreshCredentials( |
375 | 375 | $scopes, |
376 | 376 | $token['refresh_token'] |
377 | 377 | ); |
378 | - } |
|
378 | + } |
|
379 | 379 | } |
380 | 380 | |
381 | 381 | $authHandler = $this->getAuthHandler(); |
382 | 382 | |
383 | 383 | if ($credentials) { |
384 | - $callback = $this->config['token_callback']; |
|
385 | - $http = $authHandler->attachCredentials($http, $credentials, $callback); |
|
384 | + $callback = $this->config['token_callback']; |
|
385 | + $http = $authHandler->attachCredentials($http, $credentials, $callback); |
|
386 | 386 | } elseif ($token) { |
387 | - $http = $authHandler->attachToken($http, $token, (array) $scopes); |
|
387 | + $http = $authHandler->attachToken($http, $token, (array) $scopes); |
|
388 | 388 | } elseif ($key = $this->config['developer_key']) { |
389 | - $http = $authHandler->attachKey($http, $key); |
|
389 | + $http = $authHandler->attachKey($http, $key); |
|
390 | 390 | } |
391 | 391 | |
392 | 392 | return $http; |
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Set the configuration to use application default credentials for |
|
397 | - * authentication |
|
398 | - * |
|
399 | - * @see https://developers.google.com/identity/protocols/application-default-credentials |
|
400 | - * @param boolean $useAppCreds |
|
401 | - */ |
|
402 | - public function useApplicationDefaultCredentials($useAppCreds = true) |
|
403 | - { |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Set the configuration to use application default credentials for |
|
397 | + * authentication |
|
398 | + * |
|
399 | + * @see https://developers.google.com/identity/protocols/application-default-credentials |
|
400 | + * @param boolean $useAppCreds |
|
401 | + */ |
|
402 | + public function useApplicationDefaultCredentials($useAppCreds = true) |
|
403 | + { |
|
404 | 404 | $this->config['use_application_default_credentials'] = $useAppCreds; |
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * To prevent useApplicationDefaultCredentials from inappropriately being |
|
409 | - * called in a conditional |
|
410 | - * |
|
411 | - * @see https://developers.google.com/identity/protocols/application-default-credentials |
|
412 | - */ |
|
413 | - public function isUsingApplicationDefaultCredentials() |
|
414 | - { |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * To prevent useApplicationDefaultCredentials from inappropriately being |
|
409 | + * called in a conditional |
|
410 | + * |
|
411 | + * @see https://developers.google.com/identity/protocols/application-default-credentials |
|
412 | + */ |
|
413 | + public function isUsingApplicationDefaultCredentials() |
|
414 | + { |
|
415 | 415 | return $this->config['use_application_default_credentials']; |
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * @param string|array $token |
|
420 | - * @throws InvalidArgumentException |
|
421 | - */ |
|
422 | - public function setAccessToken($token) |
|
423 | - { |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * @param string|array $token |
|
420 | + * @throws InvalidArgumentException |
|
421 | + */ |
|
422 | + public function setAccessToken($token) |
|
423 | + { |
|
424 | 424 | if (is_string($token)) { |
425 | - if ($json = json_decode($token, true)) { |
|
425 | + if ($json = json_decode($token, true)) { |
|
426 | 426 | $token = $json; |
427 | - } else { |
|
427 | + } else { |
|
428 | 428 | // assume $token is just the token string |
429 | 429 | $token = array( |
430 | - 'access_token' => $token, |
|
430 | + 'access_token' => $token, |
|
431 | 431 | ); |
432 | - } |
|
432 | + } |
|
433 | 433 | } |
434 | 434 | if ($token == null) { |
435 | - throw new InvalidArgumentException('invalid json token'); |
|
435 | + throw new InvalidArgumentException('invalid json token'); |
|
436 | 436 | } |
437 | 437 | if (!isset($token['access_token'])) { |
438 | - throw new InvalidArgumentException("Invalid token format"); |
|
438 | + throw new InvalidArgumentException("Invalid token format"); |
|
439 | 439 | } |
440 | 440 | $this->token = $token; |
441 | - } |
|
441 | + } |
|
442 | 442 | |
443 | - public function getAccessToken() |
|
444 | - { |
|
443 | + public function getAccessToken() |
|
444 | + { |
|
445 | 445 | return $this->token; |
446 | - } |
|
446 | + } |
|
447 | 447 | |
448 | - /** |
|
449 | - * @return string|null |
|
450 | - */ |
|
451 | - public function getRefreshToken() |
|
452 | - { |
|
448 | + /** |
|
449 | + * @return string|null |
|
450 | + */ |
|
451 | + public function getRefreshToken() |
|
452 | + { |
|
453 | 453 | if (isset($this->token['refresh_token'])) { |
454 | - return $this->token['refresh_token']; |
|
454 | + return $this->token['refresh_token']; |
|
455 | 455 | } |
456 | 456 | |
457 | 457 | return null; |
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * Returns if the access_token is expired. |
|
462 | - * @return bool Returns True if the access_token is expired. |
|
463 | - */ |
|
464 | - public function isAccessTokenExpired() |
|
465 | - { |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * Returns if the access_token is expired. |
|
462 | + * @return bool Returns True if the access_token is expired. |
|
463 | + */ |
|
464 | + public function isAccessTokenExpired() |
|
465 | + { |
|
466 | 466 | if (!$this->token) { |
467 | - return true; |
|
467 | + return true; |
|
468 | 468 | } |
469 | 469 | |
470 | 470 | $created = 0; |
471 | 471 | if (isset($this->token['created'])) { |
472 | - $created = $this->token['created']; |
|
472 | + $created = $this->token['created']; |
|
473 | 473 | } elseif (isset($this->token['id_token'])) { |
474 | - // check the ID token for "iat" |
|
475 | - // signature verification is not required here, as we are just |
|
476 | - // using this for convenience to save a round trip request |
|
477 | - // to the Google API server |
|
478 | - $idToken = $this->token['id_token']; |
|
479 | - if (substr_count($idToken, '.') == 2) { |
|
474 | + // check the ID token for "iat" |
|
475 | + // signature verification is not required here, as we are just |
|
476 | + // using this for convenience to save a round trip request |
|
477 | + // to the Google API server |
|
478 | + $idToken = $this->token['id_token']; |
|
479 | + if (substr_count($idToken, '.') == 2) { |
|
480 | 480 | $parts = explode('.', $idToken); |
481 | 481 | $payload = json_decode(base64_decode($parts[1]), true); |
482 | 482 | if ($payload && isset($payload['iat'])) { |
483 | - $created = $payload['iat']; |
|
483 | + $created = $payload['iat']; |
|
484 | + } |
|
484 | 485 | } |
485 | - } |
|
486 | 486 | } |
487 | 487 | |
488 | 488 | // If the token is set to expire in the next 30 seconds. |
489 | 489 | return ($created + ($this->token['expires_in'] - 30)) < time(); |
490 | - } |
|
490 | + } |
|
491 | 491 | |
492 | - /** |
|
493 | - * @deprecated See UPGRADING.md for more information |
|
494 | - */ |
|
495 | - public function getAuth() |
|
496 | - { |
|
492 | + /** |
|
493 | + * @deprecated See UPGRADING.md for more information |
|
494 | + */ |
|
495 | + public function getAuth() |
|
496 | + { |
|
497 | 497 | throw new BadMethodCallException( |
498 | 498 | 'This function no longer exists. See UPGRADING.md for more information' |
499 | 499 | ); |
500 | - } |
|
500 | + } |
|
501 | 501 | |
502 | - /** |
|
503 | - * @deprecated See UPGRADING.md for more information |
|
504 | - */ |
|
505 | - public function setAuth($auth) |
|
506 | - { |
|
502 | + /** |
|
503 | + * @deprecated See UPGRADING.md for more information |
|
504 | + */ |
|
505 | + public function setAuth($auth) |
|
506 | + { |
|
507 | 507 | throw new BadMethodCallException( |
508 | 508 | 'This function no longer exists. See UPGRADING.md for more information' |
509 | 509 | ); |
510 | - } |
|
511 | - |
|
512 | - /** |
|
513 | - * Set the OAuth 2.0 Client ID. |
|
514 | - * @param string $clientId |
|
515 | - */ |
|
516 | - public function setClientId($clientId) |
|
517 | - { |
|
510 | + } |
|
511 | + |
|
512 | + /** |
|
513 | + * Set the OAuth 2.0 Client ID. |
|
514 | + * @param string $clientId |
|
515 | + */ |
|
516 | + public function setClientId($clientId) |
|
517 | + { |
|
518 | 518 | $this->config['client_id'] = $clientId; |
519 | - } |
|
519 | + } |
|
520 | 520 | |
521 | - public function getClientId() |
|
522 | - { |
|
521 | + public function getClientId() |
|
522 | + { |
|
523 | 523 | return $this->config['client_id']; |
524 | - } |
|
525 | - |
|
526 | - /** |
|
527 | - * Set the OAuth 2.0 Client Secret. |
|
528 | - * @param string $clientSecret |
|
529 | - */ |
|
530 | - public function setClientSecret($clientSecret) |
|
531 | - { |
|
524 | + } |
|
525 | + |
|
526 | + /** |
|
527 | + * Set the OAuth 2.0 Client Secret. |
|
528 | + * @param string $clientSecret |
|
529 | + */ |
|
530 | + public function setClientSecret($clientSecret) |
|
531 | + { |
|
532 | 532 | $this->config['client_secret'] = $clientSecret; |
533 | - } |
|
533 | + } |
|
534 | 534 | |
535 | - public function getClientSecret() |
|
536 | - { |
|
535 | + public function getClientSecret() |
|
536 | + { |
|
537 | 537 | return $this->config['client_secret']; |
538 | - } |
|
539 | - |
|
540 | - /** |
|
541 | - * Set the OAuth 2.0 Redirect URI. |
|
542 | - * @param string $redirectUri |
|
543 | - */ |
|
544 | - public function setRedirectUri($redirectUri) |
|
545 | - { |
|
538 | + } |
|
539 | + |
|
540 | + /** |
|
541 | + * Set the OAuth 2.0 Redirect URI. |
|
542 | + * @param string $redirectUri |
|
543 | + */ |
|
544 | + public function setRedirectUri($redirectUri) |
|
545 | + { |
|
546 | 546 | $this->config['redirect_uri'] = $redirectUri; |
547 | - } |
|
547 | + } |
|
548 | 548 | |
549 | - public function getRedirectUri() |
|
550 | - { |
|
549 | + public function getRedirectUri() |
|
550 | + { |
|
551 | 551 | return $this->config['redirect_uri']; |
552 | - } |
|
553 | - |
|
554 | - /** |
|
555 | - * Set OAuth 2.0 "state" parameter to achieve per-request customization. |
|
556 | - * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 |
|
557 | - * @param string $state |
|
558 | - */ |
|
559 | - public function setState($state) |
|
560 | - { |
|
552 | + } |
|
553 | + |
|
554 | + /** |
|
555 | + * Set OAuth 2.0 "state" parameter to achieve per-request customization. |
|
556 | + * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 |
|
557 | + * @param string $state |
|
558 | + */ |
|
559 | + public function setState($state) |
|
560 | + { |
|
561 | 561 | $this->config['state'] = $state; |
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * @param string $accessType Possible values for access_type include: |
|
566 | - * {@code "offline"} to request offline access from the user. |
|
567 | - * {@code "online"} to request online access from the user. |
|
568 | - */ |
|
569 | - public function setAccessType($accessType) |
|
570 | - { |
|
562 | + } |
|
563 | + |
|
564 | + /** |
|
565 | + * @param string $accessType Possible values for access_type include: |
|
566 | + * {@code "offline"} to request offline access from the user. |
|
567 | + * {@code "online"} to request online access from the user. |
|
568 | + */ |
|
569 | + public function setAccessType($accessType) |
|
570 | + { |
|
571 | 571 | $this->config['access_type'] = $accessType; |
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * @param string $approvalPrompt Possible values for approval_prompt include: |
|
576 | - * {@code "force"} to force the approval UI to appear. |
|
577 | - * {@code "auto"} to request auto-approval when possible. (This is the default value) |
|
578 | - */ |
|
579 | - public function setApprovalPrompt($approvalPrompt) |
|
580 | - { |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * @param string $approvalPrompt Possible values for approval_prompt include: |
|
576 | + * {@code "force"} to force the approval UI to appear. |
|
577 | + * {@code "auto"} to request auto-approval when possible. (This is the default value) |
|
578 | + */ |
|
579 | + public function setApprovalPrompt($approvalPrompt) |
|
580 | + { |
|
581 | 581 | $this->config['approval_prompt'] = $approvalPrompt; |
582 | - } |
|
583 | - |
|
584 | - /** |
|
585 | - * Set the login hint, email address or sub id. |
|
586 | - * @param string $loginHint |
|
587 | - */ |
|
588 | - public function setLoginHint($loginHint) |
|
589 | - { |
|
582 | + } |
|
583 | + |
|
584 | + /** |
|
585 | + * Set the login hint, email address or sub id. |
|
586 | + * @param string $loginHint |
|
587 | + */ |
|
588 | + public function setLoginHint($loginHint) |
|
589 | + { |
|
590 | 590 | $this->config['login_hint'] = $loginHint; |
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * Set the application name, this is included in the User-Agent HTTP header. |
|
595 | - * @param string $applicationName |
|
596 | - */ |
|
597 | - public function setApplicationName($applicationName) |
|
598 | - { |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * Set the application name, this is included in the User-Agent HTTP header. |
|
595 | + * @param string $applicationName |
|
596 | + */ |
|
597 | + public function setApplicationName($applicationName) |
|
598 | + { |
|
599 | 599 | $this->config['application_name'] = $applicationName; |
600 | - } |
|
601 | - |
|
602 | - /** |
|
603 | - * If 'plus.login' is included in the list of requested scopes, you can use |
|
604 | - * this method to define types of app activities that your app will write. |
|
605 | - * You can find a list of available types here: |
|
606 | - * @link https://developers.google.com/+/api/moment-types |
|
607 | - * |
|
608 | - * @param array $requestVisibleActions Array of app activity types |
|
609 | - */ |
|
610 | - public function setRequestVisibleActions($requestVisibleActions) |
|
611 | - { |
|
600 | + } |
|
601 | + |
|
602 | + /** |
|
603 | + * If 'plus.login' is included in the list of requested scopes, you can use |
|
604 | + * this method to define types of app activities that your app will write. |
|
605 | + * You can find a list of available types here: |
|
606 | + * @link https://developers.google.com/+/api/moment-types |
|
607 | + * |
|
608 | + * @param array $requestVisibleActions Array of app activity types |
|
609 | + */ |
|
610 | + public function setRequestVisibleActions($requestVisibleActions) |
|
611 | + { |
|
612 | 612 | if (is_array($requestVisibleActions)) { |
613 | - $requestVisibleActions = implode(" ", $requestVisibleActions); |
|
613 | + $requestVisibleActions = implode(" ", $requestVisibleActions); |
|
614 | 614 | } |
615 | 615 | $this->config['request_visible_actions'] = $requestVisibleActions; |
616 | - } |
|
617 | - |
|
618 | - /** |
|
619 | - * Set the developer key to use, these are obtained through the API Console. |
|
620 | - * @see http://code.google.com/apis/console-help/#generatingdevkeys |
|
621 | - * @param string $developerKey |
|
622 | - */ |
|
623 | - public function setDeveloperKey($developerKey) |
|
624 | - { |
|
616 | + } |
|
617 | + |
|
618 | + /** |
|
619 | + * Set the developer key to use, these are obtained through the API Console. |
|
620 | + * @see http://code.google.com/apis/console-help/#generatingdevkeys |
|
621 | + * @param string $developerKey |
|
622 | + */ |
|
623 | + public function setDeveloperKey($developerKey) |
|
624 | + { |
|
625 | 625 | $this->config['developer_key'] = $developerKey; |
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Set the hd (hosted domain) parameter streamlines the login process for |
|
630 | - * Google Apps hosted accounts. By including the domain of the user, you |
|
631 | - * restrict sign-in to accounts at that domain. |
|
632 | - * @param $hd string - the domain to use. |
|
633 | - */ |
|
634 | - public function setHostedDomain($hd) |
|
635 | - { |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Set the hd (hosted domain) parameter streamlines the login process for |
|
630 | + * Google Apps hosted accounts. By including the domain of the user, you |
|
631 | + * restrict sign-in to accounts at that domain. |
|
632 | + * @param $hd string - the domain to use. |
|
633 | + */ |
|
634 | + public function setHostedDomain($hd) |
|
635 | + { |
|
636 | 636 | $this->config['hd'] = $hd; |
637 | - } |
|
638 | - |
|
639 | - /** |
|
640 | - * Set the prompt hint. Valid values are none, consent and select_account. |
|
641 | - * If no value is specified and the user has not previously authorized |
|
642 | - * access, then the user is shown a consent screen. |
|
643 | - * @param $prompt string |
|
644 | - * {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values. |
|
645 | - * {@code "consent"} Prompt the user for consent. |
|
646 | - * {@code "select_account"} Prompt the user to select an account. |
|
647 | - */ |
|
648 | - public function setPrompt($prompt) |
|
649 | - { |
|
637 | + } |
|
638 | + |
|
639 | + /** |
|
640 | + * Set the prompt hint. Valid values are none, consent and select_account. |
|
641 | + * If no value is specified and the user has not previously authorized |
|
642 | + * access, then the user is shown a consent screen. |
|
643 | + * @param $prompt string |
|
644 | + * {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values. |
|
645 | + * {@code "consent"} Prompt the user for consent. |
|
646 | + * {@code "select_account"} Prompt the user to select an account. |
|
647 | + */ |
|
648 | + public function setPrompt($prompt) |
|
649 | + { |
|
650 | 650 | $this->config['prompt'] = $prompt; |
651 | - } |
|
652 | - |
|
653 | - /** |
|
654 | - * openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth |
|
655 | - * 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which |
|
656 | - * an authentication request is valid. |
|
657 | - * @param $realm string - the URL-space to use. |
|
658 | - */ |
|
659 | - public function setOpenidRealm($realm) |
|
660 | - { |
|
651 | + } |
|
652 | + |
|
653 | + /** |
|
654 | + * openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth |
|
655 | + * 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which |
|
656 | + * an authentication request is valid. |
|
657 | + * @param $realm string - the URL-space to use. |
|
658 | + */ |
|
659 | + public function setOpenidRealm($realm) |
|
660 | + { |
|
661 | 661 | $this->config['openid.realm'] = $realm; |
662 | - } |
|
663 | - |
|
664 | - /** |
|
665 | - * If this is provided with the value true, and the authorization request is |
|
666 | - * granted, the authorization will include any previous authorizations |
|
667 | - * granted to this user/application combination for other scopes. |
|
668 | - * @param $include boolean - the URL-space to use. |
|
669 | - */ |
|
670 | - public function setIncludeGrantedScopes($include) |
|
671 | - { |
|
662 | + } |
|
663 | + |
|
664 | + /** |
|
665 | + * If this is provided with the value true, and the authorization request is |
|
666 | + * granted, the authorization will include any previous authorizations |
|
667 | + * granted to this user/application combination for other scopes. |
|
668 | + * @param $include boolean - the URL-space to use. |
|
669 | + */ |
|
670 | + public function setIncludeGrantedScopes($include) |
|
671 | + { |
|
672 | 672 | $this->config['include_granted_scopes'] = $include; |
673 | - } |
|
674 | - |
|
675 | - /** |
|
676 | - * sets function to be called when an access token is fetched |
|
677 | - * @param callable $tokenCallback - function ($cacheKey, $accessToken) |
|
678 | - */ |
|
679 | - public function setTokenCallback(callable $tokenCallback) |
|
680 | - { |
|
673 | + } |
|
674 | + |
|
675 | + /** |
|
676 | + * sets function to be called when an access token is fetched |
|
677 | + * @param callable $tokenCallback - function ($cacheKey, $accessToken) |
|
678 | + */ |
|
679 | + public function setTokenCallback(callable $tokenCallback) |
|
680 | + { |
|
681 | 681 | $this->config['token_callback'] = $tokenCallback; |
682 | - } |
|
683 | - |
|
684 | - /** |
|
685 | - * Revoke an OAuth2 access token or refresh token. This method will revoke the current access |
|
686 | - * token, if a token isn't provided. |
|
687 | - * |
|
688 | - * @param string|array|null $token The token (access token or a refresh token) that should be revoked. |
|
689 | - * @return boolean Returns True if the revocation was successful, otherwise False. |
|
690 | - */ |
|
691 | - public function revokeToken($token = null) |
|
692 | - { |
|
682 | + } |
|
683 | + |
|
684 | + /** |
|
685 | + * Revoke an OAuth2 access token or refresh token. This method will revoke the current access |
|
686 | + * token, if a token isn't provided. |
|
687 | + * |
|
688 | + * @param string|array|null $token The token (access token or a refresh token) that should be revoked. |
|
689 | + * @return boolean Returns True if the revocation was successful, otherwise False. |
|
690 | + */ |
|
691 | + public function revokeToken($token = null) |
|
692 | + { |
|
693 | 693 | $tokenRevoker = new Google_AccessToken_Revoke( |
694 | 694 | $this->getHttpClient() |
695 | 695 | ); |
696 | 696 | |
697 | 697 | return $tokenRevoker->revokeToken($token ?: $this->getAccessToken()); |
698 | - } |
|
699 | - |
|
700 | - /** |
|
701 | - * Verify an id_token. This method will verify the current id_token, if one |
|
702 | - * isn't provided. |
|
703 | - * |
|
704 | - * @throws LogicException If no token was provided and no token was set using `setAccessToken`. |
|
705 | - * @throws UnexpectedValueException If the token is not a valid JWT. |
|
706 | - * @param string|null $idToken The token (id_token) that should be verified. |
|
707 | - * @return array|false Returns the token payload as an array if the verification was |
|
708 | - * successful, false otherwise. |
|
709 | - */ |
|
710 | - public function verifyIdToken($idToken = null) |
|
711 | - { |
|
698 | + } |
|
699 | + |
|
700 | + /** |
|
701 | + * Verify an id_token. This method will verify the current id_token, if one |
|
702 | + * isn't provided. |
|
703 | + * |
|
704 | + * @throws LogicException If no token was provided and no token was set using `setAccessToken`. |
|
705 | + * @throws UnexpectedValueException If the token is not a valid JWT. |
|
706 | + * @param string|null $idToken The token (id_token) that should be verified. |
|
707 | + * @return array|false Returns the token payload as an array if the verification was |
|
708 | + * successful, false otherwise. |
|
709 | + */ |
|
710 | + public function verifyIdToken($idToken = null) |
|
711 | + { |
|
712 | 712 | $tokenVerifier = new Google_AccessToken_Verify( |
713 | 713 | $this->getHttpClient(), |
714 | 714 | $this->getCache(), |
@@ -716,83 +716,83 @@ discard block |
||
716 | 716 | ); |
717 | 717 | |
718 | 718 | if (null === $idToken) { |
719 | - $token = $this->getAccessToken(); |
|
720 | - if (!isset($token['id_token'])) { |
|
719 | + $token = $this->getAccessToken(); |
|
720 | + if (!isset($token['id_token'])) { |
|
721 | 721 | throw new LogicException( |
722 | 722 | 'id_token must be passed in or set as part of setAccessToken' |
723 | 723 | ); |
724 | - } |
|
725 | - $idToken = $token['id_token']; |
|
724 | + } |
|
725 | + $idToken = $token['id_token']; |
|
726 | 726 | } |
727 | 727 | |
728 | 728 | return $tokenVerifier->verifyIdToken( |
729 | 729 | $idToken, |
730 | 730 | $this->getClientId() |
731 | 731 | ); |
732 | - } |
|
733 | - |
|
734 | - /** |
|
735 | - * Set the scopes to be requested. Must be called before createAuthUrl(). |
|
736 | - * Will remove any previously configured scopes. |
|
737 | - * @param string|array $scope_or_scopes, ie: array('https://www.googleapis.com/auth/plus.login', |
|
738 | - * 'https://www.googleapis.com/auth/moderator') |
|
739 | - */ |
|
740 | - public function setScopes($scope_or_scopes) |
|
741 | - { |
|
732 | + } |
|
733 | + |
|
734 | + /** |
|
735 | + * Set the scopes to be requested. Must be called before createAuthUrl(). |
|
736 | + * Will remove any previously configured scopes. |
|
737 | + * @param string|array $scope_or_scopes, ie: array('https://www.googleapis.com/auth/plus.login', |
|
738 | + * 'https://www.googleapis.com/auth/moderator') |
|
739 | + */ |
|
740 | + public function setScopes($scope_or_scopes) |
|
741 | + { |
|
742 | 742 | $this->requestedScopes = array(); |
743 | 743 | $this->addScope($scope_or_scopes); |
744 | - } |
|
745 | - |
|
746 | - /** |
|
747 | - * This functions adds a scope to be requested as part of the OAuth2.0 flow. |
|
748 | - * Will append any scopes not previously requested to the scope parameter. |
|
749 | - * A single string will be treated as a scope to request. An array of strings |
|
750 | - * will each be appended. |
|
751 | - * @param $scope_or_scopes string|array e.g. "profile" |
|
752 | - */ |
|
753 | - public function addScope($scope_or_scopes) |
|
754 | - { |
|
744 | + } |
|
745 | + |
|
746 | + /** |
|
747 | + * This functions adds a scope to be requested as part of the OAuth2.0 flow. |
|
748 | + * Will append any scopes not previously requested to the scope parameter. |
|
749 | + * A single string will be treated as a scope to request. An array of strings |
|
750 | + * will each be appended. |
|
751 | + * @param $scope_or_scopes string|array e.g. "profile" |
|
752 | + */ |
|
753 | + public function addScope($scope_or_scopes) |
|
754 | + { |
|
755 | 755 | if (is_string($scope_or_scopes) && !in_array($scope_or_scopes, $this->requestedScopes)) { |
756 | - $this->requestedScopes[] = $scope_or_scopes; |
|
756 | + $this->requestedScopes[] = $scope_or_scopes; |
|
757 | 757 | } else if (is_array($scope_or_scopes)) { |
758 | - foreach ($scope_or_scopes as $scope) { |
|
758 | + foreach ($scope_or_scopes as $scope) { |
|
759 | 759 | $this->addScope($scope); |
760 | - } |
|
761 | - } |
|
762 | - } |
|
763 | - |
|
764 | - /** |
|
765 | - * Returns the list of scopes requested by the client |
|
766 | - * @return array the list of scopes |
|
767 | - * |
|
768 | - */ |
|
769 | - public function getScopes() |
|
770 | - { |
|
771 | - return $this->requestedScopes; |
|
772 | - } |
|
773 | - |
|
774 | - /** |
|
775 | - * @return string|null |
|
776 | - * @visible For Testing |
|
777 | - */ |
|
778 | - public function prepareScopes() |
|
779 | - { |
|
760 | + } |
|
761 | + } |
|
762 | + } |
|
763 | + |
|
764 | + /** |
|
765 | + * Returns the list of scopes requested by the client |
|
766 | + * @return array the list of scopes |
|
767 | + * |
|
768 | + */ |
|
769 | + public function getScopes() |
|
770 | + { |
|
771 | + return $this->requestedScopes; |
|
772 | + } |
|
773 | + |
|
774 | + /** |
|
775 | + * @return string|null |
|
776 | + * @visible For Testing |
|
777 | + */ |
|
778 | + public function prepareScopes() |
|
779 | + { |
|
780 | 780 | if (empty($this->requestedScopes)) { |
781 | - return null; |
|
781 | + return null; |
|
782 | 782 | } |
783 | 783 | |
784 | 784 | return implode(' ', $this->requestedScopes); |
785 | - } |
|
786 | - |
|
787 | - /** |
|
788 | - * Helper method to execute deferred HTTP requests. |
|
789 | - * |
|
790 | - * @param $request Psr\Http\Message\RequestInterface|Google_Http_Batch |
|
791 | - * @throws Google_Exception |
|
792 | - * @return object of the type of the expected class or Psr\Http\Message\ResponseInterface. |
|
793 | - */ |
|
794 | - public function execute(RequestInterface $request, $expectedClass = null) |
|
795 | - { |
|
785 | + } |
|
786 | + |
|
787 | + /** |
|
788 | + * Helper method to execute deferred HTTP requests. |
|
789 | + * |
|
790 | + * @param $request Psr\Http\Message\RequestInterface|Google_Http_Batch |
|
791 | + * @throws Google_Exception |
|
792 | + * @return object of the type of the expected class or Psr\Http\Message\ResponseInterface. |
|
793 | + */ |
|
794 | + public function execute(RequestInterface $request, $expectedClass = null) |
|
795 | + { |
|
796 | 796 | $request = $request->withHeader( |
797 | 797 | 'User-Agent', |
798 | 798 | $this->config['application_name'] |
@@ -818,324 +818,324 @@ discard block |
||
818 | 818 | $this->config['retry'], |
819 | 819 | $this->config['retry_map'] |
820 | 820 | ); |
821 | - } |
|
822 | - |
|
823 | - /** |
|
824 | - * Declare whether batch calls should be used. This may increase throughput |
|
825 | - * by making multiple requests in one connection. |
|
826 | - * |
|
827 | - * @param boolean $useBatch True if the batch support should |
|
828 | - * be enabled. Defaults to False. |
|
829 | - */ |
|
830 | - public function setUseBatch($useBatch) |
|
831 | - { |
|
821 | + } |
|
822 | + |
|
823 | + /** |
|
824 | + * Declare whether batch calls should be used. This may increase throughput |
|
825 | + * by making multiple requests in one connection. |
|
826 | + * |
|
827 | + * @param boolean $useBatch True if the batch support should |
|
828 | + * be enabled. Defaults to False. |
|
829 | + */ |
|
830 | + public function setUseBatch($useBatch) |
|
831 | + { |
|
832 | 832 | // This is actually an alias for setDefer. |
833 | 833 | $this->setDefer($useBatch); |
834 | - } |
|
835 | - |
|
836 | - /** |
|
837 | - * Are we running in Google AppEngine? |
|
838 | - * return bool |
|
839 | - */ |
|
840 | - public function isAppEngine() |
|
841 | - { |
|
834 | + } |
|
835 | + |
|
836 | + /** |
|
837 | + * Are we running in Google AppEngine? |
|
838 | + * return bool |
|
839 | + */ |
|
840 | + public function isAppEngine() |
|
841 | + { |
|
842 | 842 | return (isset($_SERVER['SERVER_SOFTWARE']) && |
843 | 843 | strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false); |
844 | - } |
|
844 | + } |
|
845 | 845 | |
846 | - public function setConfig($name, $value) |
|
847 | - { |
|
846 | + public function setConfig($name, $value) |
|
847 | + { |
|
848 | 848 | $this->config[$name] = $value; |
849 | - } |
|
849 | + } |
|
850 | 850 | |
851 | - public function getConfig($name, $default = null) |
|
852 | - { |
|
851 | + public function getConfig($name, $default = null) |
|
852 | + { |
|
853 | 853 | return isset($this->config[$name]) ? $this->config[$name] : $default; |
854 | - } |
|
855 | - |
|
856 | - /** |
|
857 | - * For backwards compatibility |
|
858 | - * alias for setAuthConfig |
|
859 | - * |
|
860 | - * @param string $file the configuration file |
|
861 | - * @throws Google_Exception |
|
862 | - * @deprecated |
|
863 | - */ |
|
864 | - public function setAuthConfigFile($file) |
|
865 | - { |
|
854 | + } |
|
855 | + |
|
856 | + /** |
|
857 | + * For backwards compatibility |
|
858 | + * alias for setAuthConfig |
|
859 | + * |
|
860 | + * @param string $file the configuration file |
|
861 | + * @throws Google_Exception |
|
862 | + * @deprecated |
|
863 | + */ |
|
864 | + public function setAuthConfigFile($file) |
|
865 | + { |
|
866 | 866 | $this->setAuthConfig($file); |
867 | - } |
|
868 | - |
|
869 | - /** |
|
870 | - * Set the auth config from new or deprecated JSON config. |
|
871 | - * This structure should match the file downloaded from |
|
872 | - * the "Download JSON" button on in the Google Developer |
|
873 | - * Console. |
|
874 | - * @param string|array $config the configuration json |
|
875 | - * @throws Google_Exception |
|
876 | - */ |
|
877 | - public function setAuthConfig($config) |
|
878 | - { |
|
867 | + } |
|
868 | + |
|
869 | + /** |
|
870 | + * Set the auth config from new or deprecated JSON config. |
|
871 | + * This structure should match the file downloaded from |
|
872 | + * the "Download JSON" button on in the Google Developer |
|
873 | + * Console. |
|
874 | + * @param string|array $config the configuration json |
|
875 | + * @throws Google_Exception |
|
876 | + */ |
|
877 | + public function setAuthConfig($config) |
|
878 | + { |
|
879 | 879 | if (is_string($config)) { |
880 | - if (!file_exists($config)) { |
|
880 | + if (!file_exists($config)) { |
|
881 | 881 | throw new InvalidArgumentException(sprintf('file "%s" does not exist', $config)); |
882 | - } |
|
882 | + } |
|
883 | 883 | |
884 | - $json = file_get_contents($config); |
|
884 | + $json = file_get_contents($config); |
|
885 | 885 | |
886 | - if (!$config = json_decode($json, true)) { |
|
886 | + if (!$config = json_decode($json, true)) { |
|
887 | 887 | throw new LogicException('invalid json for auth config'); |
888 | - } |
|
888 | + } |
|
889 | 889 | } |
890 | 890 | |
891 | 891 | $key = isset($config['installed']) ? 'installed' : 'web'; |
892 | 892 | if (isset($config['type']) && $config['type'] == 'service_account') { |
893 | - // application default credentials |
|
894 | - $this->useApplicationDefaultCredentials(); |
|
895 | - |
|
896 | - // set the information from the config |
|
897 | - $this->setClientId($config['client_id']); |
|
898 | - $this->config['client_email'] = $config['client_email']; |
|
899 | - $this->config['signing_key'] = $config['private_key']; |
|
900 | - $this->config['signing_algorithm'] = 'HS256'; |
|
893 | + // application default credentials |
|
894 | + $this->useApplicationDefaultCredentials(); |
|
895 | + |
|
896 | + // set the information from the config |
|
897 | + $this->setClientId($config['client_id']); |
|
898 | + $this->config['client_email'] = $config['client_email']; |
|
899 | + $this->config['signing_key'] = $config['private_key']; |
|
900 | + $this->config['signing_algorithm'] = 'HS256'; |
|
901 | 901 | } elseif (isset($config[$key])) { |
902 | - // old-style |
|
903 | - $this->setClientId($config[$key]['client_id']); |
|
904 | - $this->setClientSecret($config[$key]['client_secret']); |
|
905 | - if (isset($config[$key]['redirect_uris'])) { |
|
902 | + // old-style |
|
903 | + $this->setClientId($config[$key]['client_id']); |
|
904 | + $this->setClientSecret($config[$key]['client_secret']); |
|
905 | + if (isset($config[$key]['redirect_uris'])) { |
|
906 | 906 | $this->setRedirectUri($config[$key]['redirect_uris'][0]); |
907 | - } |
|
907 | + } |
|
908 | 908 | } else { |
909 | - // new-style |
|
910 | - $this->setClientId($config['client_id']); |
|
911 | - $this->setClientSecret($config['client_secret']); |
|
912 | - if (isset($config['redirect_uris'])) { |
|
909 | + // new-style |
|
910 | + $this->setClientId($config['client_id']); |
|
911 | + $this->setClientSecret($config['client_secret']); |
|
912 | + if (isset($config['redirect_uris'])) { |
|
913 | 913 | $this->setRedirectUri($config['redirect_uris'][0]); |
914 | - } |
|
914 | + } |
|
915 | + } |
|
915 | 916 | } |
916 | - } |
|
917 | 917 | |
918 | - /** |
|
919 | - * Use when the service account has been delegated domain wide access. |
|
920 | - * |
|
921 | - * @param string $subject an email address account to impersonate |
|
922 | - */ |
|
923 | - public function setSubject($subject) |
|
924 | - { |
|
918 | + /** |
|
919 | + * Use when the service account has been delegated domain wide access. |
|
920 | + * |
|
921 | + * @param string $subject an email address account to impersonate |
|
922 | + */ |
|
923 | + public function setSubject($subject) |
|
924 | + { |
|
925 | 925 | $this->config['subject'] = $subject; |
926 | - } |
|
927 | - |
|
928 | - /** |
|
929 | - * Declare whether making API calls should make the call immediately, or |
|
930 | - * return a request which can be called with ->execute(); |
|
931 | - * |
|
932 | - * @param boolean $defer True if calls should not be executed right away. |
|
933 | - */ |
|
934 | - public function setDefer($defer) |
|
935 | - { |
|
926 | + } |
|
927 | + |
|
928 | + /** |
|
929 | + * Declare whether making API calls should make the call immediately, or |
|
930 | + * return a request which can be called with ->execute(); |
|
931 | + * |
|
932 | + * @param boolean $defer True if calls should not be executed right away. |
|
933 | + */ |
|
934 | + public function setDefer($defer) |
|
935 | + { |
|
936 | 936 | $this->deferExecution = $defer; |
937 | - } |
|
938 | - |
|
939 | - /** |
|
940 | - * Whether or not to return raw requests |
|
941 | - * @return boolean |
|
942 | - */ |
|
943 | - public function shouldDefer() |
|
944 | - { |
|
937 | + } |
|
938 | + |
|
939 | + /** |
|
940 | + * Whether or not to return raw requests |
|
941 | + * @return boolean |
|
942 | + */ |
|
943 | + public function shouldDefer() |
|
944 | + { |
|
945 | 945 | return $this->deferExecution; |
946 | - } |
|
946 | + } |
|
947 | 947 | |
948 | - /** |
|
949 | - * @return Google\Auth\OAuth2 implementation |
|
950 | - */ |
|
951 | - public function getOAuth2Service() |
|
952 | - { |
|
948 | + /** |
|
949 | + * @return Google\Auth\OAuth2 implementation |
|
950 | + */ |
|
951 | + public function getOAuth2Service() |
|
952 | + { |
|
953 | 953 | if (!isset($this->auth)) { |
954 | - $this->auth = $this->createOAuth2Service(); |
|
954 | + $this->auth = $this->createOAuth2Service(); |
|
955 | 955 | } |
956 | 956 | |
957 | 957 | return $this->auth; |
958 | - } |
|
958 | + } |
|
959 | 959 | |
960 | - /** |
|
961 | - * create a default google auth object |
|
962 | - */ |
|
963 | - protected function createOAuth2Service() |
|
964 | - { |
|
960 | + /** |
|
961 | + * create a default google auth object |
|
962 | + */ |
|
963 | + protected function createOAuth2Service() |
|
964 | + { |
|
965 | 965 | $auth = new OAuth2( |
966 | 966 | [ |
967 | - 'clientId' => $this->getClientId(), |
|
968 | - 'clientSecret' => $this->getClientSecret(), |
|
969 | - 'authorizationUri' => self::OAUTH2_AUTH_URL, |
|
970 | - 'tokenCredentialUri' => self::OAUTH2_TOKEN_URI, |
|
971 | - 'redirectUri' => $this->getRedirectUri(), |
|
972 | - 'issuer' => $this->config['client_id'], |
|
973 | - 'signingKey' => $this->config['signing_key'], |
|
974 | - 'signingAlgorithm' => $this->config['signing_algorithm'], |
|
967 | + 'clientId' => $this->getClientId(), |
|
968 | + 'clientSecret' => $this->getClientSecret(), |
|
969 | + 'authorizationUri' => self::OAUTH2_AUTH_URL, |
|
970 | + 'tokenCredentialUri' => self::OAUTH2_TOKEN_URI, |
|
971 | + 'redirectUri' => $this->getRedirectUri(), |
|
972 | + 'issuer' => $this->config['client_id'], |
|
973 | + 'signingKey' => $this->config['signing_key'], |
|
974 | + 'signingAlgorithm' => $this->config['signing_algorithm'], |
|
975 | 975 | ] |
976 | 976 | ); |
977 | 977 | |
978 | 978 | return $auth; |
979 | - } |
|
980 | - |
|
981 | - /** |
|
982 | - * Set the Cache object |
|
983 | - * @param Psr\Cache\CacheItemPoolInterface $cache |
|
984 | - */ |
|
985 | - public function setCache(CacheItemPoolInterface $cache) |
|
986 | - { |
|
979 | + } |
|
980 | + |
|
981 | + /** |
|
982 | + * Set the Cache object |
|
983 | + * @param Psr\Cache\CacheItemPoolInterface $cache |
|
984 | + */ |
|
985 | + public function setCache(CacheItemPoolInterface $cache) |
|
986 | + { |
|
987 | 987 | $this->cache = $cache; |
988 | - } |
|
988 | + } |
|
989 | 989 | |
990 | - /** |
|
991 | - * @return Psr\Cache\CacheItemPoolInterface Cache implementation |
|
992 | - */ |
|
993 | - public function getCache() |
|
994 | - { |
|
990 | + /** |
|
991 | + * @return Psr\Cache\CacheItemPoolInterface Cache implementation |
|
992 | + */ |
|
993 | + public function getCache() |
|
994 | + { |
|
995 | 995 | if (!$this->cache) { |
996 | - $this->cache = $this->createDefaultCache(); |
|
996 | + $this->cache = $this->createDefaultCache(); |
|
997 | 997 | } |
998 | 998 | |
999 | 999 | return $this->cache; |
1000 | - } |
|
1000 | + } |
|
1001 | 1001 | |
1002 | - /** |
|
1003 | - * @param array $cacheConfig |
|
1004 | - */ |
|
1005 | - public function setCacheConfig(array $cacheConfig) |
|
1006 | - { |
|
1002 | + /** |
|
1003 | + * @param array $cacheConfig |
|
1004 | + */ |
|
1005 | + public function setCacheConfig(array $cacheConfig) |
|
1006 | + { |
|
1007 | 1007 | $this->config['cache_config'] = $cacheConfig; |
1008 | - } |
|
1009 | - |
|
1010 | - /** |
|
1011 | - * Set the Logger object |
|
1012 | - * @param Psr\Log\LoggerInterface $logger |
|
1013 | - */ |
|
1014 | - public function setLogger(LoggerInterface $logger) |
|
1015 | - { |
|
1008 | + } |
|
1009 | + |
|
1010 | + /** |
|
1011 | + * Set the Logger object |
|
1012 | + * @param Psr\Log\LoggerInterface $logger |
|
1013 | + */ |
|
1014 | + public function setLogger(LoggerInterface $logger) |
|
1015 | + { |
|
1016 | 1016 | $this->logger = $logger; |
1017 | - } |
|
1017 | + } |
|
1018 | 1018 | |
1019 | - /** |
|
1020 | - * @return Psr\Log\LoggerInterface implementation |
|
1021 | - */ |
|
1022 | - public function getLogger() |
|
1023 | - { |
|
1019 | + /** |
|
1020 | + * @return Psr\Log\LoggerInterface implementation |
|
1021 | + */ |
|
1022 | + public function getLogger() |
|
1023 | + { |
|
1024 | 1024 | if (!isset($this->logger)) { |
1025 | - $this->logger = $this->createDefaultLogger(); |
|
1025 | + $this->logger = $this->createDefaultLogger(); |
|
1026 | 1026 | } |
1027 | 1027 | |
1028 | 1028 | return $this->logger; |
1029 | - } |
|
1029 | + } |
|
1030 | 1030 | |
1031 | - protected function createDefaultLogger() |
|
1032 | - { |
|
1031 | + protected function createDefaultLogger() |
|
1032 | + { |
|
1033 | 1033 | $logger = new Logger('google-api-php-client'); |
1034 | 1034 | if ($this->isAppEngine()) { |
1035 | - $handler = new MonologSyslogHandler('app', LOG_USER, Logger::NOTICE); |
|
1035 | + $handler = new MonologSyslogHandler('app', LOG_USER, Logger::NOTICE); |
|
1036 | 1036 | } else { |
1037 | - $handler = new MonologStreamHandler('php://stderr', Logger::NOTICE); |
|
1037 | + $handler = new MonologStreamHandler('php://stderr', Logger::NOTICE); |
|
1038 | 1038 | } |
1039 | 1039 | $logger->pushHandler($handler); |
1040 | 1040 | |
1041 | 1041 | return $logger; |
1042 | - } |
|
1042 | + } |
|
1043 | 1043 | |
1044 | - protected function createDefaultCache() |
|
1045 | - { |
|
1044 | + protected function createDefaultCache() |
|
1045 | + { |
|
1046 | 1046 | return new MemoryCacheItemPool; |
1047 | - } |
|
1048 | - |
|
1049 | - /** |
|
1050 | - * Set the Http Client object |
|
1051 | - * @param GuzzleHttp\ClientInterface $http |
|
1052 | - */ |
|
1053 | - public function setHttpClient(ClientInterface $http) |
|
1054 | - { |
|
1047 | + } |
|
1048 | + |
|
1049 | + /** |
|
1050 | + * Set the Http Client object |
|
1051 | + * @param GuzzleHttp\ClientInterface $http |
|
1052 | + */ |
|
1053 | + public function setHttpClient(ClientInterface $http) |
|
1054 | + { |
|
1055 | 1055 | $this->http = $http; |
1056 | - } |
|
1056 | + } |
|
1057 | 1057 | |
1058 | - /** |
|
1059 | - * @return GuzzleHttp\ClientInterface implementation |
|
1060 | - */ |
|
1061 | - public function getHttpClient() |
|
1062 | - { |
|
1058 | + /** |
|
1059 | + * @return GuzzleHttp\ClientInterface implementation |
|
1060 | + */ |
|
1061 | + public function getHttpClient() |
|
1062 | + { |
|
1063 | 1063 | if (null === $this->http) { |
1064 | - $this->http = $this->createDefaultHttpClient(); |
|
1064 | + $this->http = $this->createDefaultHttpClient(); |
|
1065 | 1065 | } |
1066 | 1066 | |
1067 | 1067 | return $this->http; |
1068 | - } |
|
1069 | - |
|
1070 | - /** |
|
1071 | - * Set the API format version. |
|
1072 | - * |
|
1073 | - * `true` will use V2, which may return more useful error messages. |
|
1074 | - * |
|
1075 | - * @param bool $value |
|
1076 | - */ |
|
1077 | - public function setApiFormatV2($value) |
|
1078 | - { |
|
1068 | + } |
|
1069 | + |
|
1070 | + /** |
|
1071 | + * Set the API format version. |
|
1072 | + * |
|
1073 | + * `true` will use V2, which may return more useful error messages. |
|
1074 | + * |
|
1075 | + * @param bool $value |
|
1076 | + */ |
|
1077 | + public function setApiFormatV2($value) |
|
1078 | + { |
|
1079 | 1079 | $this->config['api_format_v2'] = (bool) $value; |
1080 | - } |
|
1080 | + } |
|
1081 | 1081 | |
1082 | - protected function createDefaultHttpClient() |
|
1083 | - { |
|
1082 | + protected function createDefaultHttpClient() |
|
1083 | + { |
|
1084 | 1084 | $options = ['exceptions' => false]; |
1085 | 1085 | |
1086 | 1086 | $version = ClientInterface::VERSION; |
1087 | 1087 | if ('5' === $version[0]) { |
1088 | - $options = [ |
|
1088 | + $options = [ |
|
1089 | 1089 | 'base_url' => $this->config['base_path'], |
1090 | 1090 | 'defaults' => $options, |
1091 | - ]; |
|
1092 | - if ($this->isAppEngine()) { |
|
1091 | + ]; |
|
1092 | + if ($this->isAppEngine()) { |
|
1093 | 1093 | // set StreamHandler on AppEngine by default |
1094 | 1094 | $options['handler'] = new StreamHandler(); |
1095 | 1095 | $options['defaults']['verify'] = '/etc/ca-certificates.crt'; |
1096 | - } |
|
1096 | + } |
|
1097 | 1097 | } else { |
1098 | - // guzzle 6 |
|
1099 | - $options['base_uri'] = $this->config['base_path']; |
|
1098 | + // guzzle 6 |
|
1099 | + $options['base_uri'] = $this->config['base_path']; |
|
1100 | 1100 | } |
1101 | 1101 | |
1102 | 1102 | return new Client($options); |
1103 | - } |
|
1103 | + } |
|
1104 | 1104 | |
1105 | - private function createApplicationDefaultCredentials() |
|
1106 | - { |
|
1105 | + private function createApplicationDefaultCredentials() |
|
1106 | + { |
|
1107 | 1107 | $scopes = $this->prepareScopes(); |
1108 | 1108 | $sub = $this->config['subject']; |
1109 | 1109 | $signingKey = $this->config['signing_key']; |
1110 | 1110 | |
1111 | 1111 | // create credentials using values supplied in setAuthConfig |
1112 | 1112 | if ($signingKey) { |
1113 | - $serviceAccountCredentials = array( |
|
1113 | + $serviceAccountCredentials = array( |
|
1114 | 1114 | 'client_id' => $this->config['client_id'], |
1115 | 1115 | 'client_email' => $this->config['client_email'], |
1116 | 1116 | 'private_key' => $signingKey, |
1117 | 1117 | 'type' => 'service_account', |
1118 | - ); |
|
1119 | - $credentials = CredentialsLoader::makeCredentials($scopes, $serviceAccountCredentials); |
|
1118 | + ); |
|
1119 | + $credentials = CredentialsLoader::makeCredentials($scopes, $serviceAccountCredentials); |
|
1120 | 1120 | } else { |
1121 | - $credentials = ApplicationDefaultCredentials::getCredentials($scopes); |
|
1121 | + $credentials = ApplicationDefaultCredentials::getCredentials($scopes); |
|
1122 | 1122 | } |
1123 | 1123 | |
1124 | 1124 | // for service account domain-wide authority (impersonating a user) |
1125 | 1125 | // @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount |
1126 | 1126 | if ($sub) { |
1127 | - if (!$credentials instanceof ServiceAccountCredentials) { |
|
1127 | + if (!$credentials instanceof ServiceAccountCredentials) { |
|
1128 | 1128 | throw new DomainException('domain-wide authority requires service account credentials'); |
1129 | - } |
|
1129 | + } |
|
1130 | 1130 | |
1131 | - $credentials->setSub($sub); |
|
1131 | + $credentials->setSub($sub); |
|
1132 | 1132 | } |
1133 | 1133 | |
1134 | 1134 | return $credentials; |
1135 | - } |
|
1135 | + } |
|
1136 | 1136 | |
1137 | - protected function getAuthHandler() |
|
1138 | - { |
|
1137 | + protected function getAuthHandler() |
|
1138 | + { |
|
1139 | 1139 | // Be very careful using the cache, as the underlying auth library's cache |
1140 | 1140 | // implementation is naive, and the cache keys do not account for user |
1141 | 1141 | // sessions. |
@@ -1145,18 +1145,18 @@ discard block |
||
1145 | 1145 | $this->getCache(), |
1146 | 1146 | $this->config['cache_config'] |
1147 | 1147 | ); |
1148 | - } |
|
1148 | + } |
|
1149 | 1149 | |
1150 | - private function createUserRefreshCredentials($scope, $refreshToken) |
|
1151 | - { |
|
1150 | + private function createUserRefreshCredentials($scope, $refreshToken) |
|
1151 | + { |
|
1152 | 1152 | $creds = array_filter( |
1153 | 1153 | array( |
1154 | - 'client_id' => $this->getClientId(), |
|
1155 | - 'client_secret' => $this->getClientSecret(), |
|
1156 | - 'refresh_token' => $refreshToken, |
|
1154 | + 'client_id' => $this->getClientId(), |
|
1155 | + 'client_secret' => $this->getClientSecret(), |
|
1156 | + 'refresh_token' => $refreshToken, |
|
1157 | 1157 | ) |
1158 | 1158 | ); |
1159 | 1159 | |
1160 | 1160 | return new UserRefreshCredentials($scope, $creds); |
1161 | - } |
|
1161 | + } |
|
1162 | 1162 | } |
@@ -341,7 +341,7 @@ discard block |
||
341 | 341 | |
342 | 342 | $auth = $this->getOAuth2Service(); |
343 | 343 | |
344 | - return (string) $auth->buildFullAuthorizationUri($params); |
|
344 | + return (string)$auth->buildFullAuthorizationUri($params); |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | /** |
@@ -384,7 +384,7 @@ discard block |
||
384 | 384 | $callback = $this->config['token_callback']; |
385 | 385 | $http = $authHandler->attachCredentials($http, $credentials, $callback); |
386 | 386 | } elseif ($token) { |
387 | - $http = $authHandler->attachToken($http, $token, (array) $scopes); |
|
387 | + $http = $authHandler->attachToken($http, $token, (array)$scopes); |
|
388 | 388 | } elseif ($key = $this->config['developer_key']) { |
389 | 389 | $http = $authHandler->attachKey($http, $key); |
390 | 390 | } |
@@ -1076,7 +1076,7 @@ discard block |
||
1076 | 1076 | */ |
1077 | 1077 | public function setApiFormatV2($value) |
1078 | 1078 | { |
1079 | - $this->config['api_format_v2'] = (bool) $value; |
|
1079 | + $this->config['api_format_v2'] = (bool)$value; |
|
1080 | 1080 | } |
1081 | 1081 | |
1082 | 1082 | protected function createDefaultHttpClient() |
@@ -1091,7 +1091,7 @@ discard block |
||
1091 | 1091 | ]; |
1092 | 1092 | if ($this->isAppEngine()) { |
1093 | 1093 | // set StreamHandler on AppEngine by default |
1094 | - $options['handler'] = new StreamHandler(); |
|
1094 | + $options['handler'] = new StreamHandler(); |
|
1095 | 1095 | $options['defaults']['verify'] = '/etc/ca-certificates.crt'; |
1096 | 1096 | } |
1097 | 1097 | } else { |
@@ -23,295 +23,295 @@ |
||
23 | 23 | */ |
24 | 24 | class Google_Model implements ArrayAccess |
25 | 25 | { |
26 | - /** |
|
27 | - * If you need to specify a NULL JSON value, use Google_Model::NULL_VALUE |
|
28 | - * instead - it will be replaced when converting to JSON with a real null. |
|
29 | - */ |
|
30 | - const NULL_VALUE = "{}gapi-php-null"; |
|
31 | - protected $internal_gapi_mappings = array(); |
|
32 | - protected $modelData = array(); |
|
33 | - protected $processed = array(); |
|
26 | + /** |
|
27 | + * If you need to specify a NULL JSON value, use Google_Model::NULL_VALUE |
|
28 | + * instead - it will be replaced when converting to JSON with a real null. |
|
29 | + */ |
|
30 | + const NULL_VALUE = "{}gapi-php-null"; |
|
31 | + protected $internal_gapi_mappings = array(); |
|
32 | + protected $modelData = array(); |
|
33 | + protected $processed = array(); |
|
34 | 34 | |
35 | - /** |
|
36 | - * Polymorphic - accepts a variable number of arguments dependent |
|
37 | - * on the type of the model subclass. |
|
38 | - */ |
|
39 | - final public function __construct() |
|
40 | - { |
|
35 | + /** |
|
36 | + * Polymorphic - accepts a variable number of arguments dependent |
|
37 | + * on the type of the model subclass. |
|
38 | + */ |
|
39 | + final public function __construct() |
|
40 | + { |
|
41 | 41 | if (func_num_args() == 1 && is_array(func_get_arg(0))) { |
42 | - // Initialize the model with the array's contents. |
|
43 | - $array = func_get_arg(0); |
|
44 | - $this->mapTypes($array); |
|
42 | + // Initialize the model with the array's contents. |
|
43 | + $array = func_get_arg(0); |
|
44 | + $this->mapTypes($array); |
|
45 | 45 | } |
46 | 46 | $this->gapiInit(); |
47 | - } |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Getter that handles passthrough access to the data array, and lazy object creation. |
|
51 | - * @param string $key Property name. |
|
52 | - * @return mixed The value if any, or null. |
|
53 | - */ |
|
54 | - public function __get($key) |
|
55 | - { |
|
49 | + /** |
|
50 | + * Getter that handles passthrough access to the data array, and lazy object creation. |
|
51 | + * @param string $key Property name. |
|
52 | + * @return mixed The value if any, or null. |
|
53 | + */ |
|
54 | + public function __get($key) |
|
55 | + { |
|
56 | 56 | $keyType = $this->keyType($key); |
57 | 57 | $keyDataType = $this->dataType($key); |
58 | 58 | if ($keyType && !isset($this->processed[$key])) { |
59 | - if (isset($this->modelData[$key])) { |
|
59 | + if (isset($this->modelData[$key])) { |
|
60 | 60 | $val = $this->modelData[$key]; |
61 | - } elseif ($keyDataType == 'array' || $keyDataType == 'map') { |
|
61 | + } elseif ($keyDataType == 'array' || $keyDataType == 'map') { |
|
62 | 62 | $val = array(); |
63 | - } else { |
|
63 | + } else { |
|
64 | 64 | $val = null; |
65 | - } |
|
65 | + } |
|
66 | 66 | |
67 | - if ($this->isAssociativeArray($val)) { |
|
67 | + if ($this->isAssociativeArray($val)) { |
|
68 | 68 | if ($keyDataType && 'map' == $keyDataType) { |
69 | - foreach ($val as $arrayKey => $arrayItem) { |
|
70 | - $this->modelData[$key][$arrayKey] = |
|
69 | + foreach ($val as $arrayKey => $arrayItem) { |
|
70 | + $this->modelData[$key][$arrayKey] = |
|
71 | 71 | new $keyType($arrayItem); |
72 | - } |
|
72 | + } |
|
73 | 73 | } else { |
74 | - $this->modelData[$key] = new $keyType($val); |
|
74 | + $this->modelData[$key] = new $keyType($val); |
|
75 | 75 | } |
76 | - } else if (is_array($val)) { |
|
76 | + } else if (is_array($val)) { |
|
77 | 77 | $arrayObject = array(); |
78 | 78 | foreach ($val as $arrayIndex => $arrayItem) { |
79 | - $arrayObject[$arrayIndex] = new $keyType($arrayItem); |
|
79 | + $arrayObject[$arrayIndex] = new $keyType($arrayItem); |
|
80 | 80 | } |
81 | 81 | $this->modelData[$key] = $arrayObject; |
82 | - } |
|
83 | - $this->processed[$key] = true; |
|
82 | + } |
|
83 | + $this->processed[$key] = true; |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | return isset($this->modelData[$key]) ? $this->modelData[$key] : null; |
87 | - } |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Initialize this object's properties from an array. |
|
91 | - * |
|
92 | - * @param array $array Used to seed this object's properties. |
|
93 | - * @return void |
|
94 | - */ |
|
95 | - protected function mapTypes($array) |
|
96 | - { |
|
89 | + /** |
|
90 | + * Initialize this object's properties from an array. |
|
91 | + * |
|
92 | + * @param array $array Used to seed this object's properties. |
|
93 | + * @return void |
|
94 | + */ |
|
95 | + protected function mapTypes($array) |
|
96 | + { |
|
97 | 97 | // Hard initialise simple types, lazy load more complex ones. |
98 | 98 | foreach ($array as $key => $val) { |
99 | - if ($keyType = $this->keyType($key)) { |
|
99 | + if ($keyType = $this->keyType($key)) { |
|
100 | 100 | $dataType = $this->dataType($key); |
101 | 101 | if ($dataType == 'array' || $dataType == 'map') { |
102 | - $this->$key = array(); |
|
103 | - foreach ($val as $itemKey => $itemVal) { |
|
102 | + $this->$key = array(); |
|
103 | + foreach ($val as $itemKey => $itemVal) { |
|
104 | 104 | if ($itemVal instanceof $keyType) { |
105 | - $this->{$key}[$itemKey] = $itemVal; |
|
105 | + $this->{$key}[$itemKey] = $itemVal; |
|
106 | 106 | } else { |
107 | - $this->{$key}[$itemKey] = new $keyType($itemVal); |
|
107 | + $this->{$key}[$itemKey] = new $keyType($itemVal); |
|
108 | + } |
|
108 | 109 | } |
109 | - } |
|
110 | 110 | } elseif ($val instanceof $keyType) { |
111 | - $this->$key = $val; |
|
111 | + $this->$key = $val; |
|
112 | 112 | } else { |
113 | - $this->$key = new $keyType($val); |
|
113 | + $this->$key = new $keyType($val); |
|
114 | 114 | } |
115 | 115 | unset($array[$key]); |
116 | - } elseif (property_exists($this, $key)) { |
|
117 | - $this->$key = $val; |
|
118 | - unset($array[$key]); |
|
119 | - } elseif (property_exists($this, $camelKey = $this->camelCase($key))) { |
|
120 | - // This checks if property exists as camelCase, leaving it in array as snake_case |
|
121 | - // in case of backwards compatibility issues. |
|
122 | - $this->$camelKey = $val; |
|
123 | - } |
|
116 | + } elseif (property_exists($this, $key)) { |
|
117 | + $this->$key = $val; |
|
118 | + unset($array[$key]); |
|
119 | + } elseif (property_exists($this, $camelKey = $this->camelCase($key))) { |
|
120 | + // This checks if property exists as camelCase, leaving it in array as snake_case |
|
121 | + // in case of backwards compatibility issues. |
|
122 | + $this->$camelKey = $val; |
|
123 | + } |
|
124 | 124 | } |
125 | 125 | $this->modelData = $array; |
126 | - } |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Blank initialiser to be used in subclasses to do post-construction initialisation - this |
|
130 | - * avoids the need for subclasses to have to implement the variadics handling in their |
|
131 | - * constructors. |
|
132 | - */ |
|
133 | - protected function gapiInit() |
|
134 | - { |
|
128 | + /** |
|
129 | + * Blank initialiser to be used in subclasses to do post-construction initialisation - this |
|
130 | + * avoids the need for subclasses to have to implement the variadics handling in their |
|
131 | + * constructors. |
|
132 | + */ |
|
133 | + protected function gapiInit() |
|
134 | + { |
|
135 | 135 | return; |
136 | - } |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * Create a simplified object suitable for straightforward |
|
140 | - * conversion to JSON. This is relatively expensive |
|
141 | - * due to the usage of reflection, but shouldn't be called |
|
142 | - * a whole lot, and is the most straightforward way to filter. |
|
143 | - */ |
|
144 | - public function toSimpleObject() |
|
145 | - { |
|
138 | + /** |
|
139 | + * Create a simplified object suitable for straightforward |
|
140 | + * conversion to JSON. This is relatively expensive |
|
141 | + * due to the usage of reflection, but shouldn't be called |
|
142 | + * a whole lot, and is the most straightforward way to filter. |
|
143 | + */ |
|
144 | + public function toSimpleObject() |
|
145 | + { |
|
146 | 146 | $object = new stdClass(); |
147 | 147 | |
148 | 148 | // Process all other data. |
149 | 149 | foreach ($this->modelData as $key => $val) { |
150 | - $result = $this->getSimpleValue($val); |
|
151 | - if ($result !== null) { |
|
150 | + $result = $this->getSimpleValue($val); |
|
151 | + if ($result !== null) { |
|
152 | 152 | $object->$key = $this->nullPlaceholderCheck($result); |
153 | - } |
|
153 | + } |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | // Process all public properties. |
157 | 157 | $reflect = new ReflectionObject($this); |
158 | 158 | $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC); |
159 | 159 | foreach ($props as $member) { |
160 | - $name = $member->getName(); |
|
161 | - $result = $this->getSimpleValue($this->$name); |
|
162 | - if ($result !== null) { |
|
160 | + $name = $member->getName(); |
|
161 | + $result = $this->getSimpleValue($this->$name); |
|
162 | + if ($result !== null) { |
|
163 | 163 | $name = $this->getMappedName($name); |
164 | 164 | $object->$name = $this->nullPlaceholderCheck($result); |
165 | - } |
|
165 | + } |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | return $object; |
169 | - } |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * Handle different types of values, primarily |
|
173 | - * other objects and map and array data types. |
|
174 | - */ |
|
175 | - private function getSimpleValue($value) |
|
176 | - { |
|
171 | + /** |
|
172 | + * Handle different types of values, primarily |
|
173 | + * other objects and map and array data types. |
|
174 | + */ |
|
175 | + private function getSimpleValue($value) |
|
176 | + { |
|
177 | 177 | if ($value instanceof Google_Model) { |
178 | - return $value->toSimpleObject(); |
|
178 | + return $value->toSimpleObject(); |
|
179 | 179 | } else if (is_array($value)) { |
180 | - $return = array(); |
|
181 | - foreach ($value as $key => $a_value) { |
|
180 | + $return = array(); |
|
181 | + foreach ($value as $key => $a_value) { |
|
182 | 182 | $a_value = $this->getSimpleValue($a_value); |
183 | 183 | if ($a_value !== null) { |
184 | - $key = $this->getMappedName($key); |
|
185 | - $return[$key] = $this->nullPlaceholderCheck($a_value); |
|
184 | + $key = $this->getMappedName($key); |
|
185 | + $return[$key] = $this->nullPlaceholderCheck($a_value); |
|
186 | + } |
|
186 | 187 | } |
187 | - } |
|
188 | - return $return; |
|
188 | + return $return; |
|
189 | 189 | } |
190 | 190 | return $value; |
191 | - } |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * Check whether the value is the null placeholder and return true null. |
|
195 | - */ |
|
196 | - private function nullPlaceholderCheck($value) |
|
197 | - { |
|
193 | + /** |
|
194 | + * Check whether the value is the null placeholder and return true null. |
|
195 | + */ |
|
196 | + private function nullPlaceholderCheck($value) |
|
197 | + { |
|
198 | 198 | if ($value === self::NULL_VALUE) { |
199 | - return null; |
|
199 | + return null; |
|
200 | 200 | } |
201 | 201 | return $value; |
202 | - } |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * If there is an internal name mapping, use that. |
|
206 | - */ |
|
207 | - private function getMappedName($key) |
|
208 | - { |
|
204 | + /** |
|
205 | + * If there is an internal name mapping, use that. |
|
206 | + */ |
|
207 | + private function getMappedName($key) |
|
208 | + { |
|
209 | 209 | if (isset($this->internal_gapi_mappings, $this->internal_gapi_mappings[$key])) { |
210 | - $key = $this->internal_gapi_mappings[$key]; |
|
210 | + $key = $this->internal_gapi_mappings[$key]; |
|
211 | 211 | } |
212 | 212 | return $key; |
213 | - } |
|
213 | + } |
|
214 | 214 | |
215 | - /** |
|
216 | - * Returns true only if the array is associative. |
|
217 | - * @param array $array |
|
218 | - * @return bool True if the array is associative. |
|
219 | - */ |
|
220 | - protected function isAssociativeArray($array) |
|
221 | - { |
|
215 | + /** |
|
216 | + * Returns true only if the array is associative. |
|
217 | + * @param array $array |
|
218 | + * @return bool True if the array is associative. |
|
219 | + */ |
|
220 | + protected function isAssociativeArray($array) |
|
221 | + { |
|
222 | 222 | if (!is_array($array)) { |
223 | - return false; |
|
223 | + return false; |
|
224 | 224 | } |
225 | 225 | $keys = array_keys($array); |
226 | 226 | foreach ($keys as $key) { |
227 | - if (is_string($key)) { |
|
227 | + if (is_string($key)) { |
|
228 | 228 | return true; |
229 | - } |
|
229 | + } |
|
230 | 230 | } |
231 | 231 | return false; |
232 | - } |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Verify if $obj is an array. |
|
236 | - * @throws Google_Exception Thrown if $obj isn't an array. |
|
237 | - * @param array $obj Items that should be validated. |
|
238 | - * @param string $method Method expecting an array as an argument. |
|
239 | - */ |
|
240 | - public function assertIsArray($obj, $method) |
|
241 | - { |
|
234 | + /** |
|
235 | + * Verify if $obj is an array. |
|
236 | + * @throws Google_Exception Thrown if $obj isn't an array. |
|
237 | + * @param array $obj Items that should be validated. |
|
238 | + * @param string $method Method expecting an array as an argument. |
|
239 | + */ |
|
240 | + public function assertIsArray($obj, $method) |
|
241 | + { |
|
242 | 242 | if ($obj && !is_array($obj)) { |
243 | - throw new Google_Exception( |
|
244 | - "Incorrect parameter type passed to $method(). Expected an array." |
|
245 | - ); |
|
243 | + throw new Google_Exception( |
|
244 | + "Incorrect parameter type passed to $method(). Expected an array." |
|
245 | + ); |
|
246 | + } |
|
246 | 247 | } |
247 | - } |
|
248 | 248 | |
249 | - public function offsetExists($offset) |
|
250 | - { |
|
249 | + public function offsetExists($offset) |
|
250 | + { |
|
251 | 251 | return isset($this->$offset) || isset($this->modelData[$offset]); |
252 | - } |
|
252 | + } |
|
253 | 253 | |
254 | - public function offsetGet($offset) |
|
255 | - { |
|
254 | + public function offsetGet($offset) |
|
255 | + { |
|
256 | 256 | return isset($this->$offset) ? |
257 | 257 | $this->$offset : |
258 | 258 | $this->__get($offset); |
259 | - } |
|
259 | + } |
|
260 | 260 | |
261 | - public function offsetSet($offset, $value) |
|
262 | - { |
|
261 | + public function offsetSet($offset, $value) |
|
262 | + { |
|
263 | 263 | if (property_exists($this, $offset)) { |
264 | - $this->$offset = $value; |
|
264 | + $this->$offset = $value; |
|
265 | 265 | } else { |
266 | - $this->modelData[$offset] = $value; |
|
267 | - $this->processed[$offset] = true; |
|
266 | + $this->modelData[$offset] = $value; |
|
267 | + $this->processed[$offset] = true; |
|
268 | + } |
|
268 | 269 | } |
269 | - } |
|
270 | 270 | |
271 | - public function offsetUnset($offset) |
|
272 | - { |
|
271 | + public function offsetUnset($offset) |
|
272 | + { |
|
273 | 273 | unset($this->modelData[$offset]); |
274 | - } |
|
274 | + } |
|
275 | 275 | |
276 | - protected function keyType($key) |
|
277 | - { |
|
276 | + protected function keyType($key) |
|
277 | + { |
|
278 | 278 | $keyType = $key . "Type"; |
279 | 279 | |
280 | 280 | // ensure keyType is a valid class |
281 | 281 | if (property_exists($this, $keyType) && class_exists($this->$keyType)) { |
282 | - return $this->$keyType; |
|
282 | + return $this->$keyType; |
|
283 | + } |
|
283 | 284 | } |
284 | - } |
|
285 | 285 | |
286 | - protected function dataType($key) |
|
287 | - { |
|
286 | + protected function dataType($key) |
|
287 | + { |
|
288 | 288 | $dataType = $key . "DataType"; |
289 | 289 | |
290 | 290 | if (property_exists($this, $dataType)) { |
291 | - return $this->$dataType; |
|
291 | + return $this->$dataType; |
|
292 | + } |
|
292 | 293 | } |
293 | - } |
|
294 | 294 | |
295 | - public function __isset($key) |
|
296 | - { |
|
295 | + public function __isset($key) |
|
296 | + { |
|
297 | 297 | return isset($this->modelData[$key]); |
298 | - } |
|
298 | + } |
|
299 | 299 | |
300 | - public function __unset($key) |
|
301 | - { |
|
300 | + public function __unset($key) |
|
301 | + { |
|
302 | 302 | unset($this->modelData[$key]); |
303 | - } |
|
303 | + } |
|
304 | 304 | |
305 | - /** |
|
306 | - * Convert a string to camelCase |
|
307 | - * @param string $value |
|
308 | - * @return string |
|
309 | - */ |
|
310 | - private function camelCase($value) |
|
311 | - { |
|
305 | + /** |
|
306 | + * Convert a string to camelCase |
|
307 | + * @param string $value |
|
308 | + * @return string |
|
309 | + */ |
|
310 | + private function camelCase($value) |
|
311 | + { |
|
312 | 312 | $value = ucwords(str_replace(array('-', '_'), ' ', $value)); |
313 | 313 | $value = str_replace(' ', '', $value); |
314 | 314 | $value[0] = strtolower($value[0]); |
315 | 315 | return $value; |
316 | - } |
|
316 | + } |
|
317 | 317 | } |
@@ -254,8 +254,7 @@ |
||
254 | 254 | public function offsetGet($offset) |
255 | 255 | { |
256 | 256 | return isset($this->$offset) ? |
257 | - $this->$offset : |
|
258 | - $this->__get($offset); |
|
257 | + $this->$offset : $this->__get($offset); |
|
259 | 258 | } |
260 | 259 | |
261 | 260 | public function offsetSet($offset, $value) |
@@ -27,35 +27,35 @@ discard block |
||
27 | 27 | */ |
28 | 28 | class Google_AccessToken_Revoke |
29 | 29 | { |
30 | - /** |
|
31 | - * @var GuzzleHttp\ClientInterface The http client |
|
32 | - */ |
|
33 | - private $http; |
|
30 | + /** |
|
31 | + * @var GuzzleHttp\ClientInterface The http client |
|
32 | + */ |
|
33 | + private $http; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Instantiates the class, but does not initiate the login flow, leaving it |
|
37 | - * to the discretion of the caller. |
|
38 | - */ |
|
39 | - public function __construct(ClientInterface $http = null) |
|
40 | - { |
|
35 | + /** |
|
36 | + * Instantiates the class, but does not initiate the login flow, leaving it |
|
37 | + * to the discretion of the caller. |
|
38 | + */ |
|
39 | + public function __construct(ClientInterface $http = null) |
|
40 | + { |
|
41 | 41 | $this->http = $http; |
42 | - } |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Revoke an OAuth2 access token or refresh token. This method will revoke the current access |
|
46 | - * token, if a token isn't provided. |
|
47 | - * |
|
48 | - * @param string|array $token The token (access token or a refresh token) that should be revoked. |
|
49 | - * @return boolean Returns True if the revocation was successful, otherwise False. |
|
50 | - */ |
|
51 | - public function revokeToken($token) |
|
52 | - { |
|
44 | + /** |
|
45 | + * Revoke an OAuth2 access token or refresh token. This method will revoke the current access |
|
46 | + * token, if a token isn't provided. |
|
47 | + * |
|
48 | + * @param string|array $token The token (access token or a refresh token) that should be revoked. |
|
49 | + * @return boolean Returns True if the revocation was successful, otherwise False. |
|
50 | + */ |
|
51 | + public function revokeToken($token) |
|
52 | + { |
|
53 | 53 | if (is_array($token)) { |
54 | - if (isset($token['refresh_token'])) { |
|
54 | + if (isset($token['refresh_token'])) { |
|
55 | 55 | $token = $token['refresh_token']; |
56 | - } else { |
|
56 | + } else { |
|
57 | 57 | $token = $token['access_token']; |
58 | - } |
|
58 | + } |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | $body = Psr7\stream_for(http_build_query(array('token' => $token))); |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | 'POST', |
64 | 64 | Google_Client::OAUTH2_REVOKE_URI, |
65 | 65 | [ |
66 | - 'Cache-Control' => 'no-store', |
|
67 | - 'Content-Type' => 'application/x-www-form-urlencoded', |
|
66 | + 'Cache-Control' => 'no-store', |
|
67 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
|
68 | 68 | ], |
69 | 69 | $body |
70 | 70 | ); |
@@ -74,5 +74,5 @@ discard block |
||
74 | 74 | $response = $httpHandler($request); |
75 | 75 | |
76 | 76 | return $response->getStatusCode() == 200; |
77 | - } |
|
77 | + } |
|
78 | 78 | } |
@@ -31,55 +31,55 @@ discard block |
||
31 | 31 | */ |
32 | 32 | class Google_AccessToken_Verify |
33 | 33 | { |
34 | - const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs'; |
|
35 | - const OAUTH2_ISSUER = 'accounts.google.com'; |
|
36 | - const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com'; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var GuzzleHttp\ClientInterface The http client |
|
40 | - */ |
|
41 | - private $http; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var Psr\Cache\CacheItemPoolInterface cache class |
|
45 | - */ |
|
46 | - private $cache; |
|
47 | - |
|
48 | - /** |
|
49 | - * Instantiates the class, but does not initiate the login flow, leaving it |
|
50 | - * to the discretion of the caller. |
|
51 | - */ |
|
52 | - public function __construct( |
|
53 | - ClientInterface $http = null, |
|
54 | - CacheItemPoolInterface $cache = null, |
|
55 | - $jwt = null |
|
56 | - ) { |
|
34 | + const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs'; |
|
35 | + const OAUTH2_ISSUER = 'accounts.google.com'; |
|
36 | + const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com'; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var GuzzleHttp\ClientInterface The http client |
|
40 | + */ |
|
41 | + private $http; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var Psr\Cache\CacheItemPoolInterface cache class |
|
45 | + */ |
|
46 | + private $cache; |
|
47 | + |
|
48 | + /** |
|
49 | + * Instantiates the class, but does not initiate the login flow, leaving it |
|
50 | + * to the discretion of the caller. |
|
51 | + */ |
|
52 | + public function __construct( |
|
53 | + ClientInterface $http = null, |
|
54 | + CacheItemPoolInterface $cache = null, |
|
55 | + $jwt = null |
|
56 | + ) { |
|
57 | 57 | if (null === $http) { |
58 | - $http = new Client(); |
|
58 | + $http = new Client(); |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | if (null === $cache) { |
62 | - $cache = new MemoryCacheItemPool; |
|
62 | + $cache = new MemoryCacheItemPool; |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | $this->http = $http; |
66 | 66 | $this->cache = $cache; |
67 | 67 | $this->jwt = $jwt ?: $this->getJwtService(); |
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Verifies an id token and returns the authenticated apiLoginTicket. |
|
72 | - * Throws an exception if the id token is not valid. |
|
73 | - * The audience parameter can be used to control which id tokens are |
|
74 | - * accepted. By default, the id token must have been issued to this OAuth2 client. |
|
75 | - * |
|
76 | - * @param $audience |
|
77 | - * @return array the token payload, if successful |
|
78 | - */ |
|
79 | - public function verifyIdToken($idToken, $audience = null) |
|
80 | - { |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Verifies an id token and returns the authenticated apiLoginTicket. |
|
72 | + * Throws an exception if the id token is not valid. |
|
73 | + * The audience parameter can be used to control which id tokens are |
|
74 | + * accepted. By default, the id token must have been issued to this OAuth2 client. |
|
75 | + * |
|
76 | + * @param $audience |
|
77 | + * @return array the token payload, if successful |
|
78 | + */ |
|
79 | + public function verifyIdToken($idToken, $audience = null) |
|
80 | + { |
|
81 | 81 | if (empty($idToken)) { |
82 | - throw new LogicException('id_token cannot be null'); |
|
82 | + throw new LogicException('id_token cannot be null'); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | // set phpseclib constants if applicable |
@@ -88,15 +88,15 @@ discard block |
||
88 | 88 | // Check signature |
89 | 89 | $certs = $this->getFederatedSignOnCerts(); |
90 | 90 | foreach ($certs as $cert) { |
91 | - $bigIntClass = $this->getBigIntClass(); |
|
92 | - $rsaClass = $this->getRsaClass(); |
|
93 | - $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); |
|
94 | - $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); |
|
91 | + $bigIntClass = $this->getBigIntClass(); |
|
92 | + $rsaClass = $this->getRsaClass(); |
|
93 | + $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); |
|
94 | + $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); |
|
95 | 95 | |
96 | - $rsa = new $rsaClass(); |
|
97 | - $rsa->loadKey(array('n' => $modulus, 'e' => $exponent)); |
|
96 | + $rsa = new $rsaClass(); |
|
97 | + $rsa->loadKey(array('n' => $modulus, 'e' => $exponent)); |
|
98 | 98 | |
99 | - try { |
|
99 | + try { |
|
100 | 100 | $payload = $this->jwt->decode( |
101 | 101 | $idToken, |
102 | 102 | $rsa->getPublicKey(), |
@@ -104,63 +104,63 @@ discard block |
||
104 | 104 | ); |
105 | 105 | |
106 | 106 | if (property_exists($payload, 'aud')) { |
107 | - if ($audience && $payload->aud != $audience) { |
|
107 | + if ($audience && $payload->aud != $audience) { |
|
108 | 108 | return false; |
109 | - } |
|
109 | + } |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | // support HTTP and HTTPS issuers |
113 | 113 | // @see https://developers.google.com/identity/sign-in/web/backend-auth |
114 | 114 | $issuers = array(self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS); |
115 | 115 | if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) { |
116 | - return false; |
|
116 | + return false; |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | return (array) $payload; |
120 | - } catch (ExpiredException $e) { |
|
120 | + } catch (ExpiredException $e) { |
|
121 | 121 | return false; |
122 | - } catch (ExpiredExceptionV3 $e) { |
|
122 | + } catch (ExpiredExceptionV3 $e) { |
|
123 | 123 | return false; |
124 | - } catch (SignatureInvalidException $e) { |
|
124 | + } catch (SignatureInvalidException $e) { |
|
125 | 125 | // continue |
126 | - } catch (DomainException $e) { |
|
126 | + } catch (DomainException $e) { |
|
127 | 127 | // continue |
128 | - } |
|
128 | + } |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | return false; |
132 | - } |
|
132 | + } |
|
133 | 133 | |
134 | - private function getCache() |
|
135 | - { |
|
134 | + private function getCache() |
|
135 | + { |
|
136 | 136 | return $this->cache; |
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Retrieve and cache a certificates file. |
|
141 | - * |
|
142 | - * @param $url string location |
|
143 | - * @throws Google_Exception |
|
144 | - * @return array certificates |
|
145 | - */ |
|
146 | - private function retrieveCertsFromLocation($url) |
|
147 | - { |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Retrieve and cache a certificates file. |
|
141 | + * |
|
142 | + * @param $url string location |
|
143 | + * @throws Google_Exception |
|
144 | + * @return array certificates |
|
145 | + */ |
|
146 | + private function retrieveCertsFromLocation($url) |
|
147 | + { |
|
148 | 148 | // If we're retrieving a local file, just grab it. |
149 | 149 | if (0 !== strpos($url, 'http')) { |
150 | - if (!$file = file_get_contents($url)) { |
|
150 | + if (!$file = file_get_contents($url)) { |
|
151 | 151 | throw new Google_Exception( |
152 | 152 | "Failed to retrieve verification certificates: '" . |
153 | 153 | $url . "'." |
154 | 154 | ); |
155 | - } |
|
155 | + } |
|
156 | 156 | |
157 | - return json_decode($file, true); |
|
157 | + return json_decode($file, true); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | $response = $this->http->get($url); |
161 | 161 | |
162 | 162 | if ($response->getStatusCode() == 200) { |
163 | - return json_decode((string) $response->getBody(), true); |
|
163 | + return json_decode((string) $response->getBody(), true); |
|
164 | 164 | } |
165 | 165 | throw new Google_Exception( |
166 | 166 | sprintf( |
@@ -169,105 +169,105 @@ discard block |
||
169 | 169 | ), |
170 | 170 | $response->getStatusCode() |
171 | 171 | ); |
172 | - } |
|
172 | + } |
|
173 | 173 | |
174 | - // Gets federated sign-on certificates to use for verifying identity tokens. |
|
175 | - // Returns certs as array structure, where keys are key ids, and values |
|
176 | - // are PEM encoded certificates. |
|
177 | - private function getFederatedSignOnCerts() |
|
178 | - { |
|
174 | + // Gets federated sign-on certificates to use for verifying identity tokens. |
|
175 | + // Returns certs as array structure, where keys are key ids, and values |
|
176 | + // are PEM encoded certificates. |
|
177 | + private function getFederatedSignOnCerts() |
|
178 | + { |
|
179 | 179 | $certs = null; |
180 | 180 | if ($cache = $this->getCache()) { |
181 | - $cacheItem = $cache->getItem('federated_signon_certs_v3'); |
|
182 | - $certs = $cacheItem->get(); |
|
181 | + $cacheItem = $cache->getItem('federated_signon_certs_v3'); |
|
182 | + $certs = $cacheItem->get(); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | |
186 | 186 | if (!$certs) { |
187 | - $certs = $this->retrieveCertsFromLocation( |
|
188 | - self::FEDERATED_SIGNON_CERT_URL |
|
189 | - ); |
|
187 | + $certs = $this->retrieveCertsFromLocation( |
|
188 | + self::FEDERATED_SIGNON_CERT_URL |
|
189 | + ); |
|
190 | 190 | |
191 | - if ($cache) { |
|
191 | + if ($cache) { |
|
192 | 192 | $cacheItem->expiresAt(new DateTime('+1 hour')); |
193 | 193 | $cacheItem->set($certs); |
194 | 194 | $cache->save($cacheItem); |
195 | - } |
|
195 | + } |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | if (!isset($certs['keys'])) { |
199 | - throw new InvalidArgumentException( |
|
200 | - 'federated sign-on certs expects "keys" to be set' |
|
201 | - ); |
|
199 | + throw new InvalidArgumentException( |
|
200 | + 'federated sign-on certs expects "keys" to be set' |
|
201 | + ); |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | return $certs['keys']; |
205 | - } |
|
205 | + } |
|
206 | 206 | |
207 | - private function getJwtService() |
|
208 | - { |
|
207 | + private function getJwtService() |
|
208 | + { |
|
209 | 209 | $jwtClass = 'JWT'; |
210 | 210 | if (class_exists('\Firebase\JWT\JWT')) { |
211 | - $jwtClass = 'Firebase\JWT\JWT'; |
|
211 | + $jwtClass = 'Firebase\JWT\JWT'; |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) { |
215 | - // Ensures JWT leeway is at least 1 |
|
216 | - // @see https://github.com/google/google-api-php-client/issues/827 |
|
217 | - $jwtClass::$leeway = 1; |
|
215 | + // Ensures JWT leeway is at least 1 |
|
216 | + // @see https://github.com/google/google-api-php-client/issues/827 |
|
217 | + $jwtClass::$leeway = 1; |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | return new $jwtClass; |
221 | - } |
|
221 | + } |
|
222 | 222 | |
223 | - private function getRsaClass() |
|
224 | - { |
|
223 | + private function getRsaClass() |
|
224 | + { |
|
225 | 225 | if (class_exists('phpseclib\Crypt\RSA')) { |
226 | - return 'phpseclib\Crypt\RSA'; |
|
226 | + return 'phpseclib\Crypt\RSA'; |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | return 'Crypt_RSA'; |
230 | - } |
|
230 | + } |
|
231 | 231 | |
232 | - private function getBigIntClass() |
|
233 | - { |
|
232 | + private function getBigIntClass() |
|
233 | + { |
|
234 | 234 | if (class_exists('phpseclib\Math\BigInteger')) { |
235 | - return 'phpseclib\Math\BigInteger'; |
|
235 | + return 'phpseclib\Math\BigInteger'; |
|
236 | 236 | } |
237 | 237 | |
238 | 238 | return 'Math_BigInteger'; |
239 | - } |
|
239 | + } |
|
240 | 240 | |
241 | - private function getOpenSslConstant() |
|
242 | - { |
|
241 | + private function getOpenSslConstant() |
|
242 | + { |
|
243 | 243 | if (class_exists('phpseclib\Crypt\RSA')) { |
244 | - return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; |
|
244 | + return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | if (class_exists('Crypt_RSA')) { |
248 | - return 'CRYPT_RSA_MODE_OPENSSL'; |
|
248 | + return 'CRYPT_RSA_MODE_OPENSSL'; |
|
249 | 249 | } |
250 | 250 | |
251 | 251 | throw new \Exception('Cannot find RSA class'); |
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * phpseclib calls "phpinfo" by default, which requires special |
|
256 | - * whitelisting in the AppEngine VM environment. This function |
|
257 | - * sets constants to bypass the need for phpseclib to check phpinfo |
|
258 | - * |
|
259 | - * @see phpseclib/Math/BigInteger |
|
260 | - * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85 |
|
261 | - */ |
|
262 | - private function setPhpsecConstants() |
|
263 | - { |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * phpseclib calls "phpinfo" by default, which requires special |
|
256 | + * whitelisting in the AppEngine VM environment. This function |
|
257 | + * sets constants to bypass the need for phpseclib to check phpinfo |
|
258 | + * |
|
259 | + * @see phpseclib/Math/BigInteger |
|
260 | + * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85 |
|
261 | + */ |
|
262 | + private function setPhpsecConstants() |
|
263 | + { |
|
264 | 264 | if (filter_var(getenv('GAE_VM'), FILTER_VALIDATE_BOOLEAN)) { |
265 | - if (!defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { |
|
265 | + if (!defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { |
|
266 | 266 | define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); |
267 | - } |
|
268 | - if (!defined('CRYPT_RSA_MODE')) { |
|
267 | + } |
|
268 | + if (!defined('CRYPT_RSA_MODE')) { |
|
269 | 269 | define('CRYPT_RSA_MODE', constant($this->getOpenSslConstant())); |
270 | - } |
|
270 | + } |
|
271 | + } |
|
271 | 272 | } |
272 | - } |
|
273 | 273 | } |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | return false; |
117 | 117 | } |
118 | 118 | |
119 | - return (array) $payload; |
|
119 | + return (array)$payload; |
|
120 | 120 | } catch (ExpiredException $e) { |
121 | 121 | return false; |
122 | 122 | } catch (ExpiredExceptionV3 $e) { |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | $response = $this->http->get($url); |
161 | 161 | |
162 | 162 | if ($response->getStatusCode() == 200) { |
163 | - return json_decode((string) $response->getBody(), true); |
|
163 | + return json_decode((string)$response->getBody(), true); |
|
164 | 164 | } |
165 | 165 | throw new Google_Exception( |
166 | 166 | sprintf( |
@@ -30,49 +30,49 @@ discard block |
||
30 | 30 | */ |
31 | 31 | class Google_Http_Batch |
32 | 32 | { |
33 | - const BATCH_PATH = 'batch'; |
|
33 | + const BATCH_PATH = 'batch'; |
|
34 | 34 | |
35 | - private static $CONNECTION_ESTABLISHED_HEADERS = array( |
|
35 | + private static $CONNECTION_ESTABLISHED_HEADERS = array( |
|
36 | 36 | "HTTP/1.0 200 Connection established\r\n\r\n", |
37 | 37 | "HTTP/1.1 200 Connection established\r\n\r\n", |
38 | - ); |
|
38 | + ); |
|
39 | 39 | |
40 | - /** @var string Multipart Boundary. */ |
|
41 | - private $boundary; |
|
40 | + /** @var string Multipart Boundary. */ |
|
41 | + private $boundary; |
|
42 | 42 | |
43 | - /** @var array service requests to be executed. */ |
|
44 | - private $requests = array(); |
|
43 | + /** @var array service requests to be executed. */ |
|
44 | + private $requests = array(); |
|
45 | 45 | |
46 | - /** @var Google_Client */ |
|
47 | - private $client; |
|
46 | + /** @var Google_Client */ |
|
47 | + private $client; |
|
48 | 48 | |
49 | - private $rootUrl; |
|
49 | + private $rootUrl; |
|
50 | 50 | |
51 | - private $batchPath; |
|
51 | + private $batchPath; |
|
52 | 52 | |
53 | - public function __construct( |
|
54 | - Google_Client $client, |
|
55 | - $boundary = false, |
|
56 | - $rootUrl = null, |
|
57 | - $batchPath = null |
|
58 | - ) { |
|
53 | + public function __construct( |
|
54 | + Google_Client $client, |
|
55 | + $boundary = false, |
|
56 | + $rootUrl = null, |
|
57 | + $batchPath = null |
|
58 | + ) { |
|
59 | 59 | $this->client = $client; |
60 | 60 | $this->boundary = $boundary ?: mt_rand(); |
61 | 61 | $this->rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/'); |
62 | 62 | $this->batchPath = $batchPath ?: self::BATCH_PATH; |
63 | - } |
|
63 | + } |
|
64 | 64 | |
65 | - public function add(RequestInterface $request, $key = false) |
|
66 | - { |
|
65 | + public function add(RequestInterface $request, $key = false) |
|
66 | + { |
|
67 | 67 | if (false == $key) { |
68 | - $key = mt_rand(); |
|
68 | + $key = mt_rand(); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | $this->requests[$key] = $request; |
72 | - } |
|
72 | + } |
|
73 | 73 | |
74 | - public function execute() |
|
75 | - { |
|
74 | + public function execute() |
|
75 | + { |
|
76 | 76 | $body = ''; |
77 | 77 | $classes = array(); |
78 | 78 | $batchHttpTemplate = <<<EOF |
@@ -90,38 +90,38 @@ discard block |
||
90 | 90 | |
91 | 91 | /** @var Google_Http_Request $req */ |
92 | 92 | foreach ($this->requests as $key => $request) { |
93 | - $firstLine = sprintf( |
|
94 | - '%s %s HTTP/%s', |
|
95 | - $request->getMethod(), |
|
96 | - $request->getRequestTarget(), |
|
97 | - $request->getProtocolVersion() |
|
98 | - ); |
|
99 | - |
|
100 | - $content = (string) $request->getBody(); |
|
101 | - |
|
102 | - $headers = ''; |
|
103 | - foreach ($request->getHeaders() as $name => $values) { |
|
104 | - $headers .= sprintf("%s:%s\r\n", $name, implode(', ', $values)); |
|
105 | - } |
|
106 | - |
|
107 | - $body .= sprintf( |
|
108 | - $batchHttpTemplate, |
|
109 | - $this->boundary, |
|
110 | - $key, |
|
111 | - $firstLine, |
|
112 | - $headers, |
|
113 | - $content ? "\n".$content : '' |
|
114 | - ); |
|
115 | - |
|
116 | - $classes['response-' . $key] = $request->getHeaderLine('X-Php-Expected-Class'); |
|
93 | + $firstLine = sprintf( |
|
94 | + '%s %s HTTP/%s', |
|
95 | + $request->getMethod(), |
|
96 | + $request->getRequestTarget(), |
|
97 | + $request->getProtocolVersion() |
|
98 | + ); |
|
99 | + |
|
100 | + $content = (string) $request->getBody(); |
|
101 | + |
|
102 | + $headers = ''; |
|
103 | + foreach ($request->getHeaders() as $name => $values) { |
|
104 | + $headers .= sprintf("%s:%s\r\n", $name, implode(', ', $values)); |
|
105 | + } |
|
106 | + |
|
107 | + $body .= sprintf( |
|
108 | + $batchHttpTemplate, |
|
109 | + $this->boundary, |
|
110 | + $key, |
|
111 | + $firstLine, |
|
112 | + $headers, |
|
113 | + $content ? "\n".$content : '' |
|
114 | + ); |
|
115 | + |
|
116 | + $classes['response-' . $key] = $request->getHeaderLine('X-Php-Expected-Class'); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | $body .= "--{$this->boundary}--"; |
120 | 120 | $body = trim($body); |
121 | 121 | $url = $this->rootUrl . '/' . $this->batchPath; |
122 | 122 | $headers = array( |
123 | - 'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), |
|
124 | - 'Content-Length' => strlen($body), |
|
123 | + 'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), |
|
124 | + 'Content-Length' => strlen($body), |
|
125 | 125 | ); |
126 | 126 | |
127 | 127 | $request = new Request( |
@@ -134,95 +134,95 @@ discard block |
||
134 | 134 | $response = $this->client->execute($request); |
135 | 135 | |
136 | 136 | return $this->parseResponse($response, $classes); |
137 | - } |
|
137 | + } |
|
138 | 138 | |
139 | - public function parseResponse(ResponseInterface $response, $classes = array()) |
|
140 | - { |
|
139 | + public function parseResponse(ResponseInterface $response, $classes = array()) |
|
140 | + { |
|
141 | 141 | $contentType = $response->getHeaderLine('content-type'); |
142 | 142 | $contentType = explode(';', $contentType); |
143 | 143 | $boundary = false; |
144 | 144 | foreach ($contentType as $part) { |
145 | - $part = explode('=', $part, 2); |
|
146 | - if (isset($part[0]) && 'boundary' == trim($part[0])) { |
|
145 | + $part = explode('=', $part, 2); |
|
146 | + if (isset($part[0]) && 'boundary' == trim($part[0])) { |
|
147 | 147 | $boundary = $part[1]; |
148 | - } |
|
148 | + } |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | $body = (string) $response->getBody(); |
152 | 152 | if (!empty($body)) { |
153 | - $body = str_replace("--$boundary--", "--$boundary", $body); |
|
154 | - $parts = explode("--$boundary", $body); |
|
155 | - $responses = array(); |
|
156 | - $requests = array_values($this->requests); |
|
153 | + $body = str_replace("--$boundary--", "--$boundary", $body); |
|
154 | + $parts = explode("--$boundary", $body); |
|
155 | + $responses = array(); |
|
156 | + $requests = array_values($this->requests); |
|
157 | 157 | |
158 | - foreach ($parts as $i => $part) { |
|
158 | + foreach ($parts as $i => $part) { |
|
159 | 159 | $part = trim($part); |
160 | 160 | if (!empty($part)) { |
161 | - list($rawHeaders, $part) = explode("\r\n\r\n", $part, 2); |
|
162 | - $headers = $this->parseRawHeaders($rawHeaders); |
|
161 | + list($rawHeaders, $part) = explode("\r\n\r\n", $part, 2); |
|
162 | + $headers = $this->parseRawHeaders($rawHeaders); |
|
163 | 163 | |
164 | - $status = substr($part, 0, strpos($part, "\n")); |
|
165 | - $status = explode(" ", $status); |
|
166 | - $status = $status[1]; |
|
164 | + $status = substr($part, 0, strpos($part, "\n")); |
|
165 | + $status = explode(" ", $status); |
|
166 | + $status = $status[1]; |
|
167 | 167 | |
168 | - list($partHeaders, $partBody) = $this->parseHttpResponse($part, false); |
|
169 | - $response = new Response( |
|
170 | - $status, |
|
171 | - $partHeaders, |
|
172 | - Psr7\stream_for($partBody) |
|
173 | - ); |
|
168 | + list($partHeaders, $partBody) = $this->parseHttpResponse($part, false); |
|
169 | + $response = new Response( |
|
170 | + $status, |
|
171 | + $partHeaders, |
|
172 | + Psr7\stream_for($partBody) |
|
173 | + ); |
|
174 | 174 | |
175 | - // Need content id. |
|
176 | - $key = $headers['content-id']; |
|
175 | + // Need content id. |
|
176 | + $key = $headers['content-id']; |
|
177 | 177 | |
178 | - try { |
|
178 | + try { |
|
179 | 179 | $response = Google_Http_REST::decodeHttpResponse($response, $requests[$i-1]); |
180 | - } catch (Google_Service_Exception $e) { |
|
180 | + } catch (Google_Service_Exception $e) { |
|
181 | 181 | // Store the exception as the response, so successful responses |
182 | 182 | // can be processed. |
183 | 183 | $response = $e; |
184 | - } |
|
184 | + } |
|
185 | 185 | |
186 | - $responses[$key] = $response; |
|
186 | + $responses[$key] = $response; |
|
187 | + } |
|
187 | 188 | } |
188 | - } |
|
189 | 189 | |
190 | - return $responses; |
|
190 | + return $responses; |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | return null; |
194 | - } |
|
194 | + } |
|
195 | 195 | |
196 | - private function parseRawHeaders($rawHeaders) |
|
197 | - { |
|
196 | + private function parseRawHeaders($rawHeaders) |
|
197 | + { |
|
198 | 198 | $headers = array(); |
199 | 199 | $responseHeaderLines = explode("\r\n", $rawHeaders); |
200 | 200 | foreach ($responseHeaderLines as $headerLine) { |
201 | - if ($headerLine && strpos($headerLine, ':') !== false) { |
|
201 | + if ($headerLine && strpos($headerLine, ':') !== false) { |
|
202 | 202 | list($header, $value) = explode(': ', $headerLine, 2); |
203 | 203 | $header = strtolower($header); |
204 | 204 | if (isset($headers[$header])) { |
205 | - $headers[$header] .= "\n" . $value; |
|
205 | + $headers[$header] .= "\n" . $value; |
|
206 | 206 | } else { |
207 | - $headers[$header] = $value; |
|
207 | + $headers[$header] = $value; |
|
208 | + } |
|
208 | 209 | } |
209 | - } |
|
210 | 210 | } |
211 | 211 | return $headers; |
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Used by the IO lib and also the batch processing. |
|
216 | - * |
|
217 | - * @param $respData |
|
218 | - * @param $headerSize |
|
219 | - * @return array |
|
220 | - */ |
|
221 | - private function parseHttpResponse($respData, $headerSize) |
|
222 | - { |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Used by the IO lib and also the batch processing. |
|
216 | + * |
|
217 | + * @param $respData |
|
218 | + * @param $headerSize |
|
219 | + * @return array |
|
220 | + */ |
|
221 | + private function parseHttpResponse($respData, $headerSize) |
|
222 | + { |
|
223 | 223 | // check proxy header |
224 | 224 | foreach (self::$CONNECTION_ESTABLISHED_HEADERS as $established_header) { |
225 | - if (stripos($respData, $established_header) !== false) { |
|
225 | + if (stripos($respData, $established_header) !== false) { |
|
226 | 226 | // existed, remove it |
227 | 227 | $respData = str_ireplace($established_header, '', $respData); |
228 | 228 | // Subtract the proxy header size unless the cURL bug prior to 7.30.0 |
@@ -233,21 +233,21 @@ discard block |
||
233 | 233 | // $headerSize -= strlen($established_header); |
234 | 234 | // } |
235 | 235 | break; |
236 | - } |
|
236 | + } |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | if ($headerSize) { |
240 | - $responseBody = substr($respData, $headerSize); |
|
241 | - $responseHeaders = substr($respData, 0, $headerSize); |
|
240 | + $responseBody = substr($respData, $headerSize); |
|
241 | + $responseHeaders = substr($respData, 0, $headerSize); |
|
242 | 242 | } else { |
243 | - $responseSegments = explode("\r\n\r\n", $respData, 2); |
|
244 | - $responseHeaders = $responseSegments[0]; |
|
245 | - $responseBody = isset($responseSegments[1]) ? $responseSegments[1] : |
|
243 | + $responseSegments = explode("\r\n\r\n", $respData, 2); |
|
244 | + $responseHeaders = $responseSegments[0]; |
|
245 | + $responseBody = isset($responseSegments[1]) ? $responseSegments[1] : |
|
246 | 246 | null; |
247 | 247 | } |
248 | 248 | |
249 | 249 | $responseHeaders = $this->parseRawHeaders($responseHeaders); |
250 | 250 | |
251 | 251 | return array($responseHeaders, $responseBody); |
252 | - } |
|
252 | + } |
|
253 | 253 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | $request->getProtocolVersion() |
98 | 98 | ); |
99 | 99 | |
100 | - $content = (string) $request->getBody(); |
|
100 | + $content = (string)$request->getBody(); |
|
101 | 101 | |
102 | 102 | $headers = ''; |
103 | 103 | foreach ($request->getHeaders() as $name => $values) { |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | $key, |
111 | 111 | $firstLine, |
112 | 112 | $headers, |
113 | - $content ? "\n".$content : '' |
|
113 | + $content ? "\n" . $content : '' |
|
114 | 114 | ); |
115 | 115 | |
116 | 116 | $classes['response-' . $key] = $request->getHeaderLine('X-Php-Expected-Class'); |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
151 | - $body = (string) $response->getBody(); |
|
151 | + $body = (string)$response->getBody(); |
|
152 | 152 | if (!empty($body)) { |
153 | 153 | $body = str_replace("--$boundary--", "--$boundary", $body); |
154 | 154 | $parts = explode("--$boundary", $body); |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | $key = $headers['content-id']; |
177 | 177 | |
178 | 178 | try { |
179 | - $response = Google_Http_REST::decodeHttpResponse($response, $requests[$i-1]); |
|
179 | + $response = Google_Http_REST::decodeHttpResponse($response, $requests[$i - 1]); |
|
180 | 180 | } catch (Google_Service_Exception $e) { |
181 | 181 | // Store the exception as the response, so successful responses |
182 | 182 | // can be processed. |
@@ -242,8 +242,7 @@ discard block |
||
242 | 242 | } else { |
243 | 243 | $responseSegments = explode("\r\n\r\n", $respData, 2); |
244 | 244 | $responseHeaders = $responseSegments[0]; |
245 | - $responseBody = isset($responseSegments[1]) ? $responseSegments[1] : |
|
246 | - null; |
|
245 | + $responseBody = isset($responseSegments[1]) ? $responseSegments[1] : null; |
|
247 | 246 | } |
248 | 247 | |
249 | 248 | $responseHeaders = $this->parseRawHeaders($responseHeaders); |
@@ -27,23 +27,23 @@ discard block |
||
27 | 27 | */ |
28 | 28 | class Google_Http_REST |
29 | 29 | { |
30 | - /** |
|
31 | - * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries |
|
32 | - * when errors occur. |
|
33 | - * |
|
34 | - * @param Google_Client $client |
|
35 | - * @param Psr\Http\Message\RequestInterface $req |
|
36 | - * @return array decoded result |
|
37 | - * @throws Google_Service_Exception on server side error (ie: not authenticated, |
|
38 | - * invalid or malformed post body, invalid url) |
|
39 | - */ |
|
40 | - public static function execute( |
|
41 | - ClientInterface $client, |
|
42 | - RequestInterface $request, |
|
43 | - $expectedClass = null, |
|
44 | - $config = array(), |
|
45 | - $retryMap = null |
|
46 | - ) { |
|
30 | + /** |
|
31 | + * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries |
|
32 | + * when errors occur. |
|
33 | + * |
|
34 | + * @param Google_Client $client |
|
35 | + * @param Psr\Http\Message\RequestInterface $req |
|
36 | + * @return array decoded result |
|
37 | + * @throws Google_Service_Exception on server side error (ie: not authenticated, |
|
38 | + * invalid or malformed post body, invalid url) |
|
39 | + */ |
|
40 | + public static function execute( |
|
41 | + ClientInterface $client, |
|
42 | + RequestInterface $request, |
|
43 | + $expectedClass = null, |
|
44 | + $config = array(), |
|
45 | + $retryMap = null |
|
46 | + ) { |
|
47 | 47 | $runner = new Google_Task_Runner( |
48 | 48 | $config, |
49 | 49 | sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), |
@@ -52,35 +52,35 @@ discard block |
||
52 | 52 | ); |
53 | 53 | |
54 | 54 | if (null !== $retryMap) { |
55 | - $runner->setRetryMap($retryMap); |
|
55 | + $runner->setRetryMap($retryMap); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | return $runner->run(); |
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Executes a Psr\Http\Message\RequestInterface |
|
63 | - * |
|
64 | - * @param Google_Client $client |
|
65 | - * @param Psr\Http\Message\RequestInterface $request |
|
66 | - * @return array decoded result |
|
67 | - * @throws Google_Service_Exception on server side error (ie: not authenticated, |
|
68 | - * invalid or malformed post body, invalid url) |
|
69 | - */ |
|
70 | - public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) |
|
71 | - { |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Executes a Psr\Http\Message\RequestInterface |
|
63 | + * |
|
64 | + * @param Google_Client $client |
|
65 | + * @param Psr\Http\Message\RequestInterface $request |
|
66 | + * @return array decoded result |
|
67 | + * @throws Google_Service_Exception on server side error (ie: not authenticated, |
|
68 | + * invalid or malformed post body, invalid url) |
|
69 | + */ |
|
70 | + public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) |
|
71 | + { |
|
72 | 72 | try { |
73 | - $httpHandler = HttpHandlerFactory::build($client); |
|
74 | - $response = $httpHandler($request); |
|
73 | + $httpHandler = HttpHandlerFactory::build($client); |
|
74 | + $response = $httpHandler($request); |
|
75 | 75 | } catch (RequestException $e) { |
76 | - // if Guzzle throws an exception, catch it and handle the response |
|
77 | - if (!$e->hasResponse()) { |
|
76 | + // if Guzzle throws an exception, catch it and handle the response |
|
77 | + if (!$e->hasResponse()) { |
|
78 | 78 | throw $e; |
79 | - } |
|
79 | + } |
|
80 | 80 | |
81 | - $response = $e->getResponse(); |
|
82 | - // specific checking for Guzzle 5: convert to PSR7 response |
|
83 | - if ($response instanceof \GuzzleHttp\Message\ResponseInterface) { |
|
81 | + $response = $e->getResponse(); |
|
82 | + // specific checking for Guzzle 5: convert to PSR7 response |
|
83 | + if ($response instanceof \GuzzleHttp\Message\ResponseInterface) { |
|
84 | 84 | $response = new Response( |
85 | 85 | $response->getStatusCode(), |
86 | 86 | $response->getHeaders() ?: [], |
@@ -88,34 +88,34 @@ discard block |
||
88 | 88 | $response->getProtocolVersion(), |
89 | 89 | $response->getReasonPhrase() |
90 | 90 | ); |
91 | - } |
|
91 | + } |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | return self::decodeHttpResponse($response, $request, $expectedClass); |
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Decode an HTTP Response. |
|
99 | - * @static |
|
100 | - * @throws Google_Service_Exception |
|
101 | - * @param Psr\Http\Message\RequestInterface $response The http response to be decoded. |
|
102 | - * @param Psr\Http\Message\ResponseInterface $response |
|
103 | - * @return mixed|null |
|
104 | - */ |
|
105 | - public static function decodeHttpResponse( |
|
106 | - ResponseInterface $response, |
|
107 | - RequestInterface $request = null, |
|
108 | - $expectedClass = null |
|
109 | - ) { |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Decode an HTTP Response. |
|
99 | + * @static |
|
100 | + * @throws Google_Service_Exception |
|
101 | + * @param Psr\Http\Message\RequestInterface $response The http response to be decoded. |
|
102 | + * @param Psr\Http\Message\ResponseInterface $response |
|
103 | + * @return mixed|null |
|
104 | + */ |
|
105 | + public static function decodeHttpResponse( |
|
106 | + ResponseInterface $response, |
|
107 | + RequestInterface $request = null, |
|
108 | + $expectedClass = null |
|
109 | + ) { |
|
110 | 110 | $code = $response->getStatusCode(); |
111 | 111 | |
112 | 112 | // retry strategy |
113 | 113 | if (intVal($code) >= 400) { |
114 | - // if we errored out, it should be safe to grab the response body |
|
115 | - $body = (string) $response->getBody(); |
|
114 | + // if we errored out, it should be safe to grab the response body |
|
115 | + $body = (string) $response->getBody(); |
|
116 | 116 | |
117 | - // Check if we received errors, and add those to the Exception for convenience |
|
118 | - throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body)); |
|
117 | + // Check if we received errors, and add those to the Exception for convenience |
|
118 | + throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body)); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | // Ensure we only pull the entire body into memory if the request is not |
@@ -123,60 +123,60 @@ discard block |
||
123 | 123 | $body = self::decodeBody($response, $request); |
124 | 124 | |
125 | 125 | if ($expectedClass = self::determineExpectedClass($expectedClass, $request)) { |
126 | - $json = json_decode($body, true); |
|
126 | + $json = json_decode($body, true); |
|
127 | 127 | |
128 | - return new $expectedClass($json); |
|
128 | + return new $expectedClass($json); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | return $response; |
132 | - } |
|
132 | + } |
|
133 | 133 | |
134 | - private static function decodeBody(ResponseInterface $response, RequestInterface $request = null) |
|
135 | - { |
|
134 | + private static function decodeBody(ResponseInterface $response, RequestInterface $request = null) |
|
135 | + { |
|
136 | 136 | if (self::isAltMedia($request)) { |
137 | - // don't decode the body, it's probably a really long string |
|
138 | - return ''; |
|
137 | + // don't decode the body, it's probably a really long string |
|
138 | + return ''; |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | return (string) $response->getBody(); |
142 | - } |
|
142 | + } |
|
143 | 143 | |
144 | - private static function determineExpectedClass($expectedClass, RequestInterface $request = null) |
|
145 | - { |
|
144 | + private static function determineExpectedClass($expectedClass, RequestInterface $request = null) |
|
145 | + { |
|
146 | 146 | // "false" is used to explicitly prevent an expected class from being returned |
147 | 147 | if (false === $expectedClass) { |
148 | - return null; |
|
148 | + return null; |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | // if we don't have a request, we just use what's passed in |
152 | 152 | if (null === $request) { |
153 | - return $expectedClass; |
|
153 | + return $expectedClass; |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | // return what we have in the request header if one was not supplied |
157 | 157 | return $expectedClass ?: $request->getHeaderLine('X-Php-Expected-Class'); |
158 | - } |
|
158 | + } |
|
159 | 159 | |
160 | - private static function getResponseErrors($body) |
|
161 | - { |
|
160 | + private static function getResponseErrors($body) |
|
161 | + { |
|
162 | 162 | $json = json_decode($body, true); |
163 | 163 | |
164 | 164 | if (isset($json['error']['errors'])) { |
165 | - return $json['error']['errors']; |
|
165 | + return $json['error']['errors']; |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | return null; |
169 | - } |
|
169 | + } |
|
170 | 170 | |
171 | - private static function isAltMedia(RequestInterface $request = null) |
|
172 | - { |
|
171 | + private static function isAltMedia(RequestInterface $request = null) |
|
172 | + { |
|
173 | 173 | if ($request && $qs = $request->getUri()->getQuery()) { |
174 | - parse_str($qs, $query); |
|
175 | - if (isset($query['alt']) && $query['alt'] == 'media') { |
|
174 | + parse_str($qs, $query); |
|
175 | + if (isset($query['alt']) && $query['alt'] == 'media') { |
|
176 | 176 | return true; |
177 | - } |
|
177 | + } |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | return false; |
181 | - } |
|
181 | + } |
|
182 | 182 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | ) { |
47 | 47 | $runner = new Google_Task_Runner( |
48 | 48 | $config, |
49 | - sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), |
|
49 | + sprintf('%s %s', $request->getMethod(), (string)$request->getUri()), |
|
50 | 50 | array(get_class(), 'doExecute'), |
51 | 51 | array($client, $request, $expectedClass) |
52 | 52 | ); |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | // retry strategy |
113 | 113 | if (intVal($code) >= 400) { |
114 | 114 | // if we errored out, it should be safe to grab the response body |
115 | - $body = (string) $response->getBody(); |
|
115 | + $body = (string)$response->getBody(); |
|
116 | 116 | |
117 | 117 | // Check if we received errors, and add those to the Exception for convenience |
118 | 118 | throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body)); |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | return ''; |
139 | 139 | } |
140 | 140 | |
141 | - return (string) $response->getBody(); |
|
141 | + return (string)$response->getBody(); |
|
142 | 142 | } |
143 | 143 | |
144 | 144 | private static function determineExpectedClass($expectedClass, RequestInterface $request = null) |